Android RecyclerView 子条目(item)嵌套 RecyclerView

要实现的效果:
Android RecyclerView 子条目(item)嵌套 RecyclerView_第1张图片

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 布局:




    

    


Adapter 适配器:外层RecyclerView

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

public class MyRecyclerViewAdapter extends RecyclerView.Adapter {

    private Context mContext;
    private List mDatas;
    private ItemViewHolder holder;
    private ItemRecyclerViewAdapter mItemRecyclerViewAdapter;

    public MyRecyclerViewAdapter (Context pContext,List 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




    

    

    


Adapter 适配器:内层RecyclerView

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

public class ItemRecyclerViewAdapter extends
        RecyclerView.Adapter implements View.OnClickListener {

    private int mInt;
    private Home mHome;
    private List 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



    
    


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