在Android中实现水平布局的回收视图,可以通过使用RecyclerView和自定义LayoutManager来实现。下面是一个示例代码,演示如何实现水平布局的回收视图。
public class CustomLayoutManager extends LinearLayoutManager {
public CustomLayoutManager(Context context) {
super(context);
}
public CustomLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
super.onLayoutChildren(recycler, state);
scrollHorizontallyBy(0, recycler, state);
}
private int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) {
int scrolled = super.scrollHorizontallyBy(dx, recycler, state);
fill(recycler);
return scrolled;
}
private void fill(RecyclerView.Recycler recycler) {
int leftEdge = getWidth() - getPaddingRight();
int rightEdge = getWidth() - getPaddingRight() + getMaxChildWidth();
int topEdge = getPaddingTop();
int bottomEdge = getHeight() - getPaddingBottom();
int itemCount = getItemCount();
int childCount = getChildCount();
int lastPosition = childCount > 0 ? getPosition(getChildAt(childCount - 1)) : -1;
while (rightEdge > leftEdge && lastPosition < itemCount - 1) {
lastPosition++;
View view = recycler.getViewForPosition(lastPosition);
addView(view);
measureChildWithMargins(view, 0, 0);
int width = getDecoratedMeasuredWidth(view);
int height = getDecoratedMeasuredHeight(view);
layoutDecoratedWithMargins(view, rightEdge - width, topEdge, rightEdge, topEdge + height);
rightEdge -= width;
}
while (getChildCount() > 0 && getDecoratedRight(getChildAt(0)) < getPaddingLeft()) {
removeAndRecycleView(getChildAt(0), recycler);
}
}
private int getMaxChildWidth() {
int maxChildWidth = 0;
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
int width = getDecoratedMeasuredWidth(child);
if (width > maxChildWidth) {
maxChildWidth = width;
}
}
return maxChildWidth;
}
}
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new CustomLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
List dataList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
dataList.add("Item " + (i + 1));
}
Adapter adapter = new Adapter(dataList);
recyclerView.setAdapter(adapter);
}
}
public class Adapter extends RecyclerView.Adapter {
private List dataList;
public Adapter(List dataList) {
this.dataList = dataList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
String data = dataList.get(position);
holder.textView.setText(data);
}
@Override
public int getItemCount() {
return dataList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView textView;
上一篇:Android 双向数据绑定与 Double(Kotlin)
下一篇:Android 数据绑定:将包含布局文件中 RadioGroup 的选中 RadioButton 索引绑定到 MutableLiveData<Integer>