RecyclerView:在Adapter中为LinearLayout增加另一行

时间:2019-10-03 13:21:31

标签: android android-layout android-recyclerview android-linearlayout layout-inflater

我试图在必要时将2行LinearLayout填充到我的自定义适配器中。为避免膨胀的LinearLayout在ListView的边缘被切除,我想将每行的膨胀图像数限制为5。

BLOCKLAYOUT.XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">

</LinearLayout>

BlockAdapter.java

public BlockAdapter(Context context,List<Cscore> blocks, int pos) {
        this.mContext = context;
        this.blocks = blocks;
        this.currentPosition = pos;
    }

    @Override
    public int getItemCount() {
        return 1;
    }

    @Override
    public BlockAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.block_layout, parent, false));
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        if(blocks != null) {
            holder.bind(blocks.get(currentPosition),null);
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private final List<ImageView> imageViews = new ArrayList<>();
        private final ViewGroup container;

        public ViewHolder(View v) {
            super(v);
            container = (ViewGroup) itemView;
        }

        public void bind(Cscore block) {
            recycleImageViews();
            if(block !=null) {

                for (int i = 0; i < block.getMaxscore(); ++i) {
                    final ImageView imageView = getRecycledImageViewOrCreate();
                    Picasso.get().load(block.getImgurl()).into(imageView);
                    imageViews.add(imageView);
                    container.addView(imageView);


                }
            }

        }

        private ImageView getRecycledImageViewOrCreate() {
            if (imageViewPool.isEmpty()) {
                return (ImageView)LayoutInflater.from(container.getContext()).inflate(R.layout.imageView, container, false);
            }
            return imageViewPool.remove(0);
        }

        public void recycleImageViews() {
            imageViewPool.addAll(imageViews);
            imageViews.clear();
            container.removeAllViews();
        }
    }

    @Override
    public void onViewRecycled(ViewHolder holder) {
        super.onViewRecycled(holder);
        holder.recycleImageViews();
    }
}

此BlockAdapter仅放大图像,并且是另一个显示文本和其他内容的Adapter的子代。因此,我的getItemCount()返回1,因为我只希望在父适配器内部显示1个RecyclerView。

R.layout.imageView是一个简单的imageView:

<ImageView android:id="@+id/block"
    android:layout_width="@dimen/icon_l"
    android:layout_height="@dimen/icon_l" />

我尝试使用bind方法:

public void bind(Cscore block) {
recycleImageViews();
if(block !=null) { ...}  
imageViews.add(imageView);
container.addView(imageView);
}

当我到达ImagesList中的第5项时,我应该用水平方向为LinearLayout的另一行/行充气...显然从第6项而不是第1项开始

0 个答案:

没有答案