【问题标题】:Add multiple expandable cardviews to layout在布局中添加多个可扩展的卡片视图
【发布时间】:2020-06-11 05:40:36
【问题描述】:

我正在使用AleSpero's library 为我的布局创建expandable cardviews。按预期工作,但现在我想在同一个片段布局中添加多个cardviews,动态绑定到一些加载的异步列表数据。这怎么可能?

关注图书馆的演示。以下是我在布局中添加卡片的方式:

<LinearLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/watchlist_holder"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="20dp"
    android:orientation="vertical"
    android:clipChildren="false"
    android:background="#FAFAFA">

    <com.alespero.expandablecardview.ExpandableCardView
         android:id="@+id/main_profile_card"
         android:layout_marginLeft="10dp"
         android:layout_marginRight="10dp"
         android:layout_marginTop="10dp"
         android:layout_marginBottom="10dp"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         app:title="My Watchlist"
         app:inner_view="@layout/watchlist_layout"
         app:expandOnClick="true" />
</LinearLayout>

@layout/watchlist_layout 是一个我想重复的布局,它包含一些textView 和一个recyclerView 来显示列表数据。任何帮助或指导都会很棒。

【问题讨论】:

  • 您需要使用RecylcerViewAdapter 来动态扩充包含单个expandablecardviewinner_layout 文件。如果你需要帮助,我可以让你看看怎么做
  • Atish,谢谢你。如果你能帮我解决这个问题,我将不胜感激。
  • 你还在吗?我可以帮助你。请回复。

标签: android android-layout android-fragments kotlin android-cardview


【解决方案1】:

您需要为每个RecyclerView 创建不同的Adapter 和Inner Row XML 布局文件。

例如:您在 RecyclerView 中的 MainActivity 中扩展布局。您正在使用 Adapter 类,它会根据您的列表对行进行膨胀。在onBindViewHolder 内部,您应该获得内部RecyclerView 的对象,该对象存在于父视图的行布局中。获得对象后,创建另一个列表并为内部 recyclerview 初始化另一个适配器。使用新的适配器在其中填充数据(类似于第一个 recyclerview)。

请记住,每个回收站视图的过程都是一样的

步骤:

  1. 在要显示列表的布局内创建 recyclerview
  2. 创建一个单独的 row_layout 以根据列表数据的数量在每一行中膨胀
  3. 创建一个Adapter类,它从父类接收数据并在recyclerview中膨胀布局(row_ayout)
  4. 对 N 个嵌套 RecyclerView 重复这些步骤

对于演示,我附上了带有此答案的示例代码,以帮助您理解我的概念。

activity_main.xml

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

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recylcerViewParent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

item_layout_row.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <com.alespero.expandablecardview.ExpandableCardView
        android:id="@+id/main_profile_card"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="10dp"
        app:expandOnClick="true"
        app:inner_view="@layout/watchlist_inner"
        app:title="My Watchlist" />

</RelativeLayout>

item_recycler_view_favorite.xml

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


    <TextView
        android:id="@+id/favorites_count"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/margin_small"
        android:layout_marginLeft="@dimen/margin_small"
        android:textSize="12sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/margin_small"
        android:layout_marginEnd="@dimen/margin_small"
        android:orientation="horizontal"
        android:weightSum="2">

        <Button
            android:id="@+id/btn_view_details"
            android:layout_width="0dp"
            android:layout_height="30dp"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:gravity="start|center_vertical"
            android:padding="5dp"
            android:text="Atish"
            android:textColor="@color/colorPrimaryDark" />

        <Button
            android:id="@+id/btn_add_symbols"
            android:layout_width="0dp"
            android:layout_height="30dp"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:gravity="end|center_vertical"
            android:padding="5dp"
            android:text="Agrawal"
            android:textColor="@color/colorPrimaryDark" />
    </LinearLayout>


</LinearLayout>

watchlist_inner.xml

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


    <TextView
        android:id="@+id/favorites_count"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/margin_small"
        android:layout_marginLeft="@dimen/margin_small"
        android:textSize="12sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/margin_small"
        android:layout_marginEnd="@dimen/margin_small"
        android:orientation="horizontal"
        android:weightSum="2">

        <Button
            android:id="@+id/btn_view_details"
            android:layout_width="0dp"
            android:layout_height="30dp"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:gravity="start|center_vertical"
            android:padding="5dp"
            android:text="VIEW DETAILS"
            android:textColor="@color/colorPrimaryDark" />

        <Button
            android:id="@+id/btn_add_symbols"
            android:layout_width="0dp"
            android:layout_height="30dp"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:gravity="end|center_vertical"
            android:padding="5dp"
            android:text="ADD SYMBOLS"
            android:textColor="@color/colorPrimaryDark" />
    </LinearLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view_favorite"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="@dimen/margin_small"
        android:layout_marginEnd="@dimen/margin_small"
        android:layout_marginBottom="@dimen/margin_small" />

</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {


    List<String> stringsList = new ArrayList<>();

    RecyclerView recyclerViewLayout;
    InnerAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        recyclerViewLayout = findViewById(R.id.recylcerViewParent);


        //Dummy add 10 objects in the list

        for (int i = 0; i < 10; i++) {
            stringsList.add(String.valueOf(i));
        }


        populateRecyclerView();
    }


    /**
     * Create N items in the recycler view
     */
    private void populateRecyclerView() {


        //Initialize Adapter

        recyclerViewLayout.setLayoutManager(new LinearLayoutManager(this));
        recyclerViewLayout.setHasFixedSize(false);

        adapter = new InnerAdapter(recyclerViewLayout, this);
        adapter.setData(this.stringsList);
        recyclerViewLayout.setAdapter(adapter);


    }
}

InnerAdapter.java

public class InnerAdapter extends RecyclerView.Adapter<InnerAdapter.ViewHolder> {
    private List<String> data;
    private RecyclerView recyclerView;
    private int i = 0;
    private Context mContext;

    public InnerAdapter(RecyclerView recyclerView, Context context) {
        this.recyclerView = recyclerView;
        this.mContext = context;

    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        //Inflater creates rows from a given layout file


        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View v = inflater.inflate(R.layout.item_layout_row, parent, false);
        return new ViewHolder(v);
    }

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

        //Method to perform actions on individual row based on the position. We will get back to this later


        //Change the title of the CardView
        holder.main_profile_card.setTitle(String.valueOf(position));


        //Creating a dummy adapter again to populate the inner recyclerview
        List<String> innerData = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            innerData.add(String.valueOf(i));
        }

        //Initialize Inner Adapter
        holder.recycler_view_favorite.setLayoutManager(new LinearLayoutManager(mContext));
        holder.recycler_view_favorite.setHasFixedSize(false);
        InnerFavAdapter adapter = new InnerFavAdapter(holder.recycler_view_favorite, mContext);
        adapter.setData(innerData);
        holder.recycler_view_favorite.setAdapter(adapter);

    }

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

    public void setData(List<String> data) {
        this.data = data;
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        RecyclerView recycler_view_favorite;
        ExpandableCardView main_profile_card;

        ViewHolder(View itemView) {
            super(itemView);

            //Get the object of the views from the row layout
            main_profile_card = itemView.findViewById(R.id.main_profile_card);
            recycler_view_favorite = itemView.findViewById(R.id.recycler_view_favorite);
        }
    }
}

InnerFavAdapter.java

public class InnerFavAdapter extends RecyclerView.Adapter<InnerFavAdapter.ViewHolder> {
    private List<String> data;
    private RecyclerView recyclerView;
    private int i = 0;
    private Context mContext;

    public InnerFavAdapter(RecyclerView recyclerView, Context context) {
        this.recyclerView = recyclerView;
        this.mContext = context;

    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        //Inflater creates rows from a given layout file


        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View v = inflater.inflate(R.layout.inner_recycler_view_favorite, parent, false);
        return new ViewHolder(v);
    }

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

        //Method to perform actions on individual row based on the position. We will get back to this later


    }

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

    public void setData(List<String> data) {
        this.data = data;
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        ViewHolder(View itemView) {
            super(itemView);


        }
    }
}

【讨论】:

  • 这正是我想要的。谢谢!
猜你喜欢
  • 2016-03-13
  • 1970-01-01
  • 2018-12-01
  • 2016-10-13
  • 1970-01-01
  • 1970-01-01
  • 2020-05-24
  • 2019-10-31
  • 1970-01-01
相关资源
最近更新 更多