我如何在另一个recyclerview的商品中创建一个RecyclerView?

问题描述 投票:0回答:2

我想将recyclerview放在另一个recyclerview的项目中。,谢谢您,谢谢。

android android-activity android-recyclerview recycler-adapter
2个回答
0
投票

在第一个recyclerview的绑定视图持有者中使用它,如:

InnerRecyclerviewAdapter adapter=new InnerRecyclerviewAdapter(context,urlistArray);
holder.recyclerView.setAdapter(adapter);
holder.recyclerView.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(context, 
LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(layoutManager);

For further guidance click here


0
投票

尝试此代码

-MainRecyclerView项目

item_post.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/llItemPost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="@dimen/_5sdp">

        <TextView
            android:id="@+id/txtPostCname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Company Name"
            android:textColor="@color/Blue"
            android:textSize="@dimen/_21ssp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/_8sdp"
            android:layout_marginLeft="@dimen/_8sdp"
            android:layout_marginTop="@dimen/_8sdp"
            android:text="Post:-"
            android:textColor="@color/Blue"
            android:textSize="@dimen/_17ssp"
            android:textStyle="bold" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rvPostSub"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingTop="@dimen/_5sdp"
            android:paddingBottom="@dimen/_10sdp">

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

    </LinearLayout>

</LinearLayout>

-MainRecyclerView适配器

PostAdapter.class

public class PostAdapter extends RecyclerView.Adapter<PostAdapter.ViewHolder> {

    private static final String TAG = "PostAdapter";

    private PostSubAdapter postSubAdapter;
    private ArrayList<String> arrayList;
    private Context mContext;
    private ArrayList<String> yourSubArrayList;

    public PostAdapter(ArrayList<String> arrayList, ArrayList<String> yourSubArrayList, Context mContext) {
        this.arrayList = arrayList;
        this.yourSubArrayList = yourSubArrayList;
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_post, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, int i) {

        viewHolder.txtPostCname.setText(arrayList.get(i));

        viewHolder.rvPostSub.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        postSubAdapter = new PostSubAdapter(yourSubArrayList, mContext);
        viewHolder.rvPostSub.setAdapter(postSubAdapter);
        postSubAdapter.notifyDataSetChanged();

    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        RecyclerView rvPostSub;
        TextView txtPostCname;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            txtPostCname = itemView.findViewById(R.id.txtPostCname);
            rvPostSub = itemView.findViewById(R.id.rvPostSub);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
}

-SubRecyclerView项目

item_post_sub.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/txtPost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/_12sdp"
        android:layout_marginEnd="@dimen/_12sdp"
        android:text="Post"
        android:textColor="#000000"
        android:textSize="@dimen/_13ssp" />

    <View
        android:visibility="visible"
        android:id="@+id/viewDivider"
        android:layout_width="match_parent"
        android:layout_height="@dimen/_2sdp"
        android:layout_marginLeft="@dimen/_8sdp"
        android:layout_marginTop="@dimen/_5sdp"
        android:layout_marginRight="@dimen/_8sdp"
        android:layout_marginBottom="@dimen/_5sdp"
        android:backgroundTint="@color/Blue"
        android:background="@drawable/social_media_divider" />

</LinearLayout>

-SubRecyclerView适配器

PostSubAdapter.class

public class PostSubAdapter extends RecyclerView.Adapter<PostSubAdapter.ViewHolder> {
    private ArrayList<String> arrayList;
    Context mContext;

    public PostSubAdapter(ArrayList<String> arrayList, Context mContext) {
        this.arrayList = arrayList;
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_post_sub, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        String post=arrayList.get(i).replace("\\n","\n");
        viewHolder.txtPost.setText(post);
        if(i==arrayList.size()-1)
        {
            viewHolder.viewDivider.setVisibility(View.GONE);
        }
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView txtPost;
        View viewDivider;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            txtPost=itemView.findViewById(R.id.txtPost);
            viewDivider=itemView.findViewById(R.id.viewDivider);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
}

我希望这可以帮助您!

谢谢。

© www.soinside.com 2019 - 2024. All rights reserved.