回收站视图中的项目重叠

2023-12-05

当用户滚动时,我的回收器视图中的项目会重叠。注意底部重叠的文本:

enter image description here

这是生成此视图的代码:

        ArrayList<Bitmap> drawables = mBitmaps;
        RecyclerView recyclerView = new RecyclerView(ctx);
        LinearLayoutManager llm = new LinearLayoutManager(ctx);
        recyclerView.setLayoutManager(llm);
        RecyclerView.Adapter adapter = new MyRecyclerAdapter(contentList, uriList, drawables);
        recyclerView.setAdapter(adapter);
        ((ViewGroup) rootView).addView(recyclerView);
  • mBitmaps是一组图像
  • contentList是一个字符串列表
  • uriList是另一个字符串列表

这是代码MyRecyclerAdapter class :

public class MyRecyclerAdapter extends RecyclerView.Adapter<MyViewHolder> {

    ArrayList<String> mContentList, mUriList;
    ArrayList<Bitmap> mBitmaps;
    MyRecyclerAdapter(ArrayList<String> contentList, ArrayList<String> uriList, ArrayList<Bitmap> drawables){
        mContentList = contentList;
        mUriList = uriList;
        mBitmaps = drawables;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final Context ctx = parent.getContext();
        CardView.LayoutParams params = new CardView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        CardView cardView = new CardView(ctx);
        cardView.setLayoutParams(params);

        // set edges round for the cardView
        int radius = 14;
        final float scale = ctx.getResources().getDisplayMetrics().density;
        int pixels = (int) (radius * scale + 0.5f);
        cardView.setRadius(pixels);

        MyViewHolder holder = new MyViewHolder(cardView);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Context ctx = holder.itemView.getContext();
        View view = getViewForCard(ctx, position);      // getView for the card
        holder.viewGroup.addView(view);
    }

    @Override
    public int getItemCount() {
        return mBitmaps.size();
    }

    public View getViewForCard(Context context, int pos){

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        Context ctx = context;
        LinearLayout ll = new LinearLayout(ctx);
        ll.setLayoutParams(params);
        ll.setOrientation(LinearLayout.VERTICAL);

        TextView tv = new TextView(ctx);
        tv.setText(mUriList.get(pos));
        TextView tv1 = new TextView(ctx);
        tv1.setText(mContentList.get(pos));

        // create an image view and add bitmap to it.
        ImageView imageView = new ImageView(ctx);
        imageView.setLayoutParams(params);
        imageView.getLayoutParams().height = 500;
        imageView.getLayoutParams().width = 500;
        imageView.setImageBitmap(mBitmaps.get(pos));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

        ll.addView(imageView);       
        ll.addView(tv);                 ll.addView(tv1);

        return ll;
    }

}

这是我的ViewHolder class:

public class MyViewHolder extends RecyclerView.ViewHolder {
    ViewGroup viewGroup;

    MyViewHolder(View view){
        super(view);
        viewGroup = (ViewGroup)itemView;
    }
}

为了重申这个问题,如何防止图像中出现重叠?


问题解决了:

 public void onBindViewHolder(MyViewHolder holder, int position) {
    Context ctx = holder.itemView.getContext();
    View view = getViewForCard(ctx, position);      // getView for the card

    holder.viewGroup.removeAllViews();     // **The problem solved here**

    holder.viewGroup.addView(view);
}

我删除了所有视图holder.viewGroup in the onBindViewHolder()内的方法MyRecyclerAdapter班级。并且不再有重叠:)。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

回收站视图中的项目重叠 的相关文章

随机推荐