Android中RecyclerView加载不同子布局(记录)

经常看到新闻之类的列表里有好几种不同的布局样式,很简单,动手写了一下,做个记录。

效果图如下:

Android中RecyclerView加载不同子布局(记录)_第1张图片

主要思路:就是通过判断接收的子项的布局样式的不同,来加载不同的布局。

相应的Adapter适配器的代码如下:

public class MyRecyclerViewAdapter extends RecyclerView.Adapter {

    private Context mContext;
    private List typeList;

    private static final int ITEM_TYPE_ONE = 1;
    private static final int ITEM_TYPE_TWO = 2;
    private static final int ITEM_TYPE_THREE = 3;

    public MyRecyclerViewAdapter(Context mContext, List typeList) {
        this.mContext = mContext;
        this.typeList = typeList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == ITEM_TYPE_ONE) {
            View convertView = LayoutInflater.from(mContext).inflate(R.layout.layout_item_rview_type_one, parent, false);
            ViewHolderOne viewHolderOne = new ViewHolderOne(convertView);
            return viewHolderOne;
        } else if (viewType == ITEM_TYPE_TWO) {
            View convertView = LayoutInflater.from(mContext).inflate(R.layout.layout_item_rview_type_two, parent, false);
            ViewHolderTwo viewHolderTwo = new ViewHolderTwo(convertView);
            return viewHolderTwo;
        } else if (viewType == ITEM_TYPE_THREE) {
            View convertView = LayoutInflater.from(mContext).inflate(R.layout.layout_item_rview_type_three, parent, false);
            ViewHolderThree viewHolderThree = new ViewHolderThree(convertView);
            return viewHolderThree;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolderOne) {
            bindViewHolderOne((ViewHolderOne) holder);
        } else if (holder instanceof ViewHolderTwo) {
            bindViewHolderTwo((ViewHolderTwo) holder, position);
        } else if (holder instanceof ViewHolderThree) {
            bindViewHolderThree((ViewHolderThree) holder);
        }
    }

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

    @Override
    public int getItemViewType(int position) {
        if (typeList.get(position) == 1) {
            return ITEM_TYPE_ONE;
        } else if (typeList.get(position) == 2) {
            return ITEM_TYPE_TWO;
        } else if (typeList.get(position) == 3) {
            return ITEM_TYPE_THREE;
        }
        return 0;
    }

    static class ViewHolderOne extends RecyclerView.ViewHolder {

        private TextView tvTitle, tvContent;

        public ViewHolderOne(@NonNull View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.Layout_Item_RView_TypeOne_tvTitle);
            tvContent = itemView.findViewById(R.id.Layout_Item_RView_TypeOne_tvContent);
        }
    }

    static class ViewHolderTwo extends RecyclerView.ViewHolder {

        private TextView tvTitle;
        private GridView gViewPhoto;

        public ViewHolderTwo(@NonNull View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.Layout_Item_RView_TypeTwo_tvTitle);
            gViewPhoto = itemView.findViewById(R.id.Layout_Item_RView_TypeTwo_gViewPhoto);
        }
    }

    static class ViewHolderThree extends RecyclerView.ViewHolder {

        private TextView tvTitle;
        private ImageView ivThumb;

        public ViewHolderThree(@NonNull View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.Layout_Item_RView_TypeThree_tvTitle);
            ivThumb = itemView.findViewById(R.id.Layout_Item_RView_TypeThree_ivThumb);
        }
    }

    private void bindViewHolderOne(ViewHolderOne viewHolderOne) {
        viewHolderOne.tvTitle.setText("这是一条新闻的标题");
        viewHolderOne.tvContent.setText("新闻内容新闻内容新闻内容新闻内容新闻内容新闻内容新闻内容新闻内容新闻内容新闻内容");
    }

    private void bindViewHolderTwo(ViewHolderTwo viewHolderTwo, int position) {
        viewHolderTwo.tvTitle.setText("这是一条带有多张图片的新闻的标题");
        List photoList = new ArrayList<>();
        for (int i = 0; i < 3; i ++) {
            photoList.add(R.drawable.fengjing);
        }
        PhotoListAdapter photoListAdapter = new PhotoListAdapter(mContext, photoList);
        viewHolderTwo.gViewPhoto.setAdapter(photoListAdapter);
        viewHolderTwo.gViewPhoto.setTag(position);
        viewHolderTwo.gViewPhoto.setOnItemClickListener((parent, view, position1, id) -> {
            int mPosition = (int) (parent.getTag());
            Log.e("图片索引", mPosition + "-" + position1);
        });
    }

    private void bindViewHolderThree(ViewHolderThree viewHolderThree) {
        viewHolderThree.tvTitle.setText("这是一条带有单张图片新闻的标题");
    }
}

你可能感兴趣的:(Android开发)