Android RecyclerView 子条目(item)嵌套 RecyclerView

要实现的效果:
这是我们要实现的效果

MainActivity核心代码:

LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mTvRecycler.setLayoutManager(layoutManager);
mMyRecyclerViewAdapter = new MyRecyclerViewAdapter(TvStationActivity.this,mList);
//给RecyclerView设置适配器
mTvRecycler.setAdapter(mMyRecyclerViewAdapter);

activity_main.xml 布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/tv_recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="25dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        >

    </android.support.v7.widget.RecyclerView>

</android.support.constraint.ConstraintLayout>

Adapter 适配器:外层RecyclerView

/**
 * @author 小红妹
 * @date 2018/10/20.
 * @describe
 * @copyright 
 */

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ItemViewHolder> {

    private Context mContext;
    private List<String> mDatas;
    private ItemViewHolder holder;
    private ItemRecyclerViewAdapter mItemRecyclerViewAdapter;

    public MyRecyclerViewAdapter (Context pContext,List<String> data){
        this.mContext = pContext;
        this.mDatas = data;
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        TextView mTextView;
        RecyclerView mRecyclerView;

        ItemViewHolder(View itemView) {
            super(itemView);
            mTextView = itemView.findViewById(R.id.tv_recycler_name);
            mRecyclerView = itemView.findViewById(R.id.tv_recycler_view);
        }
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //LayoutInflater.from指定写法
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_content, parent, false);
        //holder = new ItemViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_content, parent, false));
        return new ItemViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
        holder.mTextView.setText(mDatas.get(position));
        holder.mRecyclerView.setHasFixedSize(true);
        holder.mRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 4,
                GridLayoutManager.VERTICAL, false));
        mItemRecyclerViewAdapter = new ItemRecyclerViewAdapter(position);
        holder.mRecyclerView.setAdapter(mItemRecyclerViewAdapter);
        drawRecyclerView();
    }

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

    /**
     * RecyclerView 内层点击事件方法
     */
    private void drawRecyclerView() {
        //RecyclerView点击事件
        mItemRecyclerViewAdapter.setOnItemClickListener(new ItemRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(mContext, ""+position, Toast.LENGTH_SHORT).show();
            }

        });
    }

}

RecyclerView外层布局item_home_content.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/tv_recycler_name"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="TextView"
        android:textSize="19sp"
        android:gravity="center_vertical"
        android:background="@color/whitesmoke"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/tv_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_recycler_name">

    </android.support.v7.widget.RecyclerView>

</android.support.constraint.ConstraintLayout>

Adapter 适配器:内层RecyclerView

/**
 * @author 小红妹
 * @date 2018/10/20.
 * @describe
 * @copyright
 */

public class ItemRecyclerViewAdapter extends
        RecyclerView.Adapter<ItemRecyclerViewAdapter.MyViewHolder> implements View.OnClickListener {

    private int mInt;
    private Home mHome;
    private List<Home> mHomes;
    private String[] localTV = new String[]{
            "湖南卫视", "江苏卫视", "浙江卫视", "东方卫视",
            "北京卫视", "安徽卫视", "山东卫视", "深圳卫视",
            "江西卫视", "黑龙江台", "广东卫视", "天津卫视",
            "湖北卫视", "河北卫视", "重庆卫视", "东南卫视",
            "甘肃卫视", "广西卫视", "贵州卫视", "河南卫视",
            "吉林卫视", "辽宁卫视", "旅游卫视", "内蒙古台",
            "宁夏卫视", "青海卫视", "四川卫视", "陕西卫视",
            "山西卫视", "新疆卫视", "西藏卫视", "云南卫视",
            "南方卫视", "香港卫视", "凤凰中文", "凤凰资讯"};

    private String[] cctv = new String[]{
            "CCTV1", "CCTV2", "CCTV3", "CCTV4",
            "CCTV5", "CCTV5+", "CCTV6", "CCTV7",
            "CCTV8", "CCTV9", "CCTV10", "CCTV11",
            "CCTV12", "CCTV13", "CCTV14", "CCTV15",
            "央视音乐", "央视军事", "央视新闻", "央视少儿"};

    private String[] mLocal = new String[]{
            "广州新闻", "广州经济", "广州少儿", "广州综合",
            "广东新闻", "南方卫视", "南方经济", "广东珠江",
            "南方影视", "南方综艺", "深圳都市", "深圳公共",
            "金华公共", "温州经济", "温州都市", "嘉兴综合"};

    private String[] mSports = new String[]{
            "CCTV5", "CCTV5+", "广东体育", "五星体育",
            "北京体育", "风云足球", "广州竞赛", "欧洲足球"};

    private String[] mMusic = new String[]{
            "华龙直播", "经典串烧", "伤感男人", "发烧慢摇",
            "汽车大碟", "酒吧慢摇", "激情舞曲", "电音舞曲"};

    private String[] mFilm = new String[]{
            "央视电影", "家庭剧场", "喜剧电影", "动作电影",
            "经典剧场", "热播电视", "都市剧场", "经典大片"};

    private String[] mCartoon = new String[]{
            "嘉佳卡通", "北京卡酷", "金鹰卡通", "炫动卡通"};

    //新建一个私有变量用于保存用户设置的监听器
    private OnItemClickListener mOnItemClickListener = null;

    //set方法:
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v,(int)v.getTag());
        }
    }

    //define interface 自定义一个接口
    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public ItemRecyclerViewAdapter(int pI){
        mHomes = new ArrayList<>();
        //holder.mView.setText(localTV[i]);
        if (pI == 0) {
            for (int i = 0; i < localTV.length; i++) {
                mHome = new Home();
                mHome.setName(localTV[i]);
                mHomes.add(mHome);
            }
        }if (pI == 1) {
            for (int i = 0; i < cctv.length; i++) {
                mHome = new Home();
                mHome.setName(cctv[i]);
                mHomes.add(mHome);
            }
        }if (pI == 2) {
            for (int i = 0; i < mLocal.length; i++) {
                mHome = new Home();
                mHome.setName(mLocal[i]);
                mHomes.add(mHome);
            }
        }if (pI == 3) {
            for (int i = 0; i < mSports.length; i++) {
                mHome = new Home();
                mHome.setName(mSports[i]);
                mHomes.add(mHome);
            }
        }if (pI == 4) {
            for (int i = 0; i < mMusic.length; i++) {
                mHome = new Home();
                mHome.setName(mMusic[i]);
                mHomes.add(mHome);
            }
        }if (pI == 5) {
            for (int i = 0; i < mFilm.length; i++) {
                mHome = new Home();
                mHome.setName(mFilm[i]);
                mHomes.add(mHome);
            }
        }if (pI == 6) {
            for (int i = 0; i < mCartoon.length; i++) {
                mHome = new Home();
                mHome.setName(mCartoon[i]);
                mHomes.add(mHome);
            }
        }

    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_head,null);
        //将创建的View注册点击事件
        view.setOnClickListener(this);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.mView.setText(mHomes.get(position).getName());
        //将position保存在itemView的Tag中,以便点击时进行获取
        holder.itemView.setTag(position);
    }

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

    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView mView;
        public MyViewHolder(View itemView) {
            super(itemView);
            mView = itemView.findViewById(R.id.tv_name);
        }
    }
}

RecyclerView内层布局item_home_head.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <!--android:background="@drawable/beautiful_linearlayout_border"-->
    <TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="48dp"
        android:layout_marginStart="0dp"
        android:layout_marginTop="0dp"
        android:gravity="center"
        android:text="TextView"
        android:textSize="19sp"

        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>
  • 6
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值