二级列表-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 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



    



GroupAdapter
public class GroupAdapter extends RecyclerView.Adapter{

    private Context mContext;
    private List list;

    public GroupAdapter(Context context, List 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 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



    
        
    


    
ChildAdapter
public class ChildAdapter extends RecyclerView.Adapter{

    private Context mContext;
    private List list;

    public ChildAdapter(Context context, List 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



    

    

 

你可能感兴趣的:(二级列表-RecylerView嵌套)