购物车二级列表-双层RecylerView嵌套

HomeFragment
public class HomeFragment extends Fragment implements IView{

    @BindView(R.id.home_rv_group)
    RecyclerView mRecyclerView_group;

    private IPrecenterImpl mIPrecenter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = View.inflate(getActivity(), R.layout.fragment_home, null);

        ButterKnife.bind(this,view);
         mIPrecenter = new IPrecenterImpl(this);

        mIPrecenter.requestData(Apis.URL_FL,null, ShopCatBean.class,0,Apis.BASE_URL);


        return view;
    }


    @Override
    public void showData(Object data) {



        ShopCatBean shopCatBean = (ShopCatBean) data;
        List<ShopCatBean.DataBean> data1 = shopCatBean.getData();

        GroupAdapter groupAdapter = new GroupAdapter(getActivity(), data1);
        mRecyclerView_group.setAdapter(groupAdapter);
        mRecyclerView_group.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false));
    }
}

fragment_home.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <android.support.v7.widget.RecyclerView
        android:id="@+id/home_rv_group"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>



</LinearLayout>
GroupAdapter
public class GroupAdapter extends RecyclerView.Adapter<GroupAdapter.ViewHolder>{

    private Context mContext;
    private List<ShopCatBean.DataBean> list;

    public GroupAdapter(Context context, List<ShopCatBean.DataBean> list) {
        mContext = context;
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.item_group, null);

        ViewHolder holder = new ViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        holder.mTextView.setText(list.get(position).getSellerName());

        List<ShopCatBean.DataBean.ListBean> list = this.list.get(position).getList();
        ChildAdapter childAdapter = new ChildAdapter(mContext, list);
        holder.mRecyclerView.setAdapter(childAdapter);
        holder.mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL,false));
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.item_group_text)
        TextView mTextView;

        @BindView(R.id.home_rv_child)
        RecyclerView mRecyclerView;

        public ViewHolder(View itemView) {
            super(itemView);

            ButterKnife.bind(this,itemView);
        }
    }
}

item_group.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent">
        <TextView
            android:id="@+id/item_group_text"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:text="ff"
            android:gravity="center"
            android:layout_marginLeft="50dp"
            />
    </LinearLayout>


    <android.support.v7.widget.RecyclerView
        android:id="@+id/home_rv_child"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>
ChildAdapter
public class ChildAdapter extends RecyclerView.Adapter<ChildAdapter.ViewHolder>{

    private Context mContext;
    private List<ShopCatBean.DataBean.ListBean> list;

    public ChildAdapter(Context context, List<ShopCatBean.DataBean.ListBean> list) {
        mContext = context;
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.item_child, null);

        ViewHolder holder = new ViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        Log.e("ff",list.get(position).getTitle());
        holder.mTextView.setText(list.get(position).getTitle());
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.item_child_text)
        TextView mTextView;

        public ViewHolder(View itemView) {
            super(itemView);

            ButterKnife.bind(this,itemView);
        }
    }
}

item_child.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    >

    <ImageView
        android:id="@+id/item_child_img"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:layout_marginLeft="10dp"
       android:src="@mipmap/ic_launcher"/>

    <TextView
        android:id="@+id/item_child_text"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="ff"
        android:gravity="center"
        android:layout_marginLeft="50dp"/>
</LinearLayout>

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值