简体   繁体   English

如何在另一个 recyclerview 的项目中创建 RecyclerView?

[英]How can i create a RecyclerView inside of the another recyclerview's item?

I want to make recyclerview inside of the another recyclerview's item.,Please help me,Thankyou.我想在另一个recyclerview的项目中制作recyclerview。请帮助我,谢谢。

Try this code试试这个代码

- MainRecyclerView Item - MainRecyclerView 项目

item_post.xml 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 Adapter - MainRecyclerView 适配器

PostAdapter.class后适配器.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 - SubRecyclerView 项目

item_post_sub.xml 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 Adapter - SubRecyclerView 适配器

PostSubAdapter.class 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;
    }
}

I hope this can help You!我希望这可以帮助你!

Thank You.谢谢你。

In the bind view holder of 1st recyclerview use it like:在第一个 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如需进一步指导,请单击此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在另一个RecyclerView的项目中实现RecyclerView - How can I implement a RecyclerView inside another RecyclerView's item 我如何将一个 recyclerview 创建到另一个 recyclerview android - How can i create one recyclerview into another recyclerview android Kotlin:如何在另一个 Recyclerview 内的 Recyclerview 中单击项目 - Kotlin : How to click on item in a Recyclerview Inside another Recyclerview 当我单击里面的图像视图时,如何摆脱recyclerview的单击? - How can I get rid of recyclerview item's click when I click on an imageview inside it? 如果我需要从另一个片段中删除项目,如何从 RecyclerView 适配器的 ArrayList 中删除项目 - How can I delete item from RecyclerView adapter's ArrayList, if I need to do it from another Fragment 如何访问片段内单击的 RecyclerView 项目 position? - How can I access clicked RecyclerView item position inside fragment? 如何更新RecyclerView中项目的位置? - How can I update the position of an item inside of a RecyclerView? 如何在 RecyclerView 列表项中有多个 TextView - How can I have multiple TextViews inside a RecyclerView list item 我如何检测是否选择了Card或RecyclerView的项目? - How can i detect if a Card or a RecyclerView's item is selected? 如何识别RecyclerView的最后一项是否在屏幕上可见? - How can I identify that RecyclerView's last item is visible on screen?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM