在“回收者视图"中重叠的项目 [英] Items overlapping in Recycler View

查看:61
本文介绍了在“回收者视图"中重叠的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户滚动时,回收者"视图中的项目重叠.请注意底部的重叠文本:

The Items in my recycler view are overlapping when user scrolls. Notice the overlapping text at the bottom:

以下是生成此视图的代码:

Here is the code generating this view:

        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是另一个字符串列表
    • mBitmaps is a set of images
    • contentList is a list of strings
    • uriList is another list of strings
    • 这是MyRecyclerAdapter类的代码:

      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课:

      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);
      }
      

      我从MyRecyclerAdapter类中的onBindViewHolder()方法中的holder.viewGroup中删除了所有视图.而且没有更多的重叠了:).

      I removed all views from the holder.viewGroup in the onBindViewHolder() method within MyRecyclerAdapter class. And there is no more overlapping :).

      这篇关于在“回收者视图"中重叠的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆