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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章