Android 将多个可展开的CardView添加到布局中
我正在使用为我的布局创建Android 将多个可展开的CardView添加到布局中,android,android-layout,android-fragments,kotlin,android-cardview,Android,Android Layout,Android Fragments,Kotlin,Android Cardview,我正在使用为我的布局创建可展开的CardView。按预期工作,但现在我想在同一个片段布局中添加多个cardwiews,动态绑定到加载的一些异步列表数据。那怎么可能呢 按照库上的演示进行操作。以下是我如何在布局中添加卡片: <LinearLayout xmlns:tools="http://schemas.android.com/tools" android:id="@+id/watchlist_holder" android:layout_width="match_
可展开的CardView
。按预期工作,但现在我想在同一个片段布局中添加多个cardwiews
,动态绑定到加载的一些异步列表数据。那怎么可能呢
按照库上的演示进行操作。以下是我如何在布局中添加卡片:
<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\u layout
是我想重复的一个布局,它包含一些textView
和一个recyclerView
来显示列表数据。任何帮助或指导都会很好。您需要为每个RecyclerView
创建不同的Adapter
和内行XML布局文件
例如:在main活动
内的RecyclerView
中为布局充气。您正在使用一个适配器
类,该类根据您的列表对行进行膨胀。在onBindViewHolder
内部,您应该获得父视图行布局中存在的内部RecyclerView
的对象。拥有对象后,创建另一个列表并为内部recyclerview初始化另一个适配器。使用新适配器填充其中的数据(类似于第一个recyclerview)
请记住,每个recyclerview的过程都是一样的
步骤:
<?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>
<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>
<?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>
<?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>
项目布局行.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>
<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>
<?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>
<?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>
物品\u回收商\u查看\u收藏夹.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>
<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>
<?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>
<?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>
监视列表_inner.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>
<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>
<?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>
<?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);
}
}
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);
}
}
}
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);
}
}
}
public类MainActivity扩展了AppCompatActivity{
List stringsList=新的ArrayList();
回收视图回收视图布局;
内部适配器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerViewLayout=findViewById(R.id.recylcerViewParent);
//在列表中添加10个对象
对于(int i=0;i<10;i++){
stringsList.add(String.valueOf(i));
}
populateRecyclerView();
}
/**
*在“回收器”视图中创建N个项目
*/
私有void populateRecyclerView(){
//初始化适配器
setLayoutManager(新的LinearLayoutManager(this));
recyclerViewLayout.setHasFixedSize(false);
适配器=新的内部适配器(recyclerViewLayout,this);
adapter.setData(this.stringsList);
设置适配器(适配器);
}
}
InnerAdapter.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);
}
}
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);
}
}
}
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);
}
}
}
公共类InnerAdapter扩展了RecyclerView.Adapter{
私人名单数据;
私人回收站;
私有整数i=0;
私有上下文;
公共InnerAdapter(RecyclerView RecyclerView,上下文){
this.recyclerView=recyclerView;
this.mContext=上下文;
}
@非空
@凌驾
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent,int viewType){
//充气器从给定布局文件创建行
LayoutInflater充气器=LayoutInflater.from(parent.getContext());
视图v=充气机。充气(R.layout.item_layout_row,parent,false);
返回新的视图持有者(v);
}
@凌驾
public void onBindViewHolder(@NonNull ViewHolder,int位置){
//方法对基于位置的单个行执行操作。我们稍后将返回到此操作
//更改CardView的标题
持有者.main_profile_card.setTitle(String.valueOf(position));
//再次创建虚拟适配器以填充内部recyclerview
List innerData=new ArrayList();
对于(int i=0;i<10;i++){
add(String.valueOf(i));
}
//初始化内部适配器
holder.recycler\u view\u favorite.setLayoutManager(新的LinearLayoutManager(mContext));
holder.recycler\u view\u favorite.setHasFixedSize(false);
InnerFavaAdapter=新的InnerFavaAdapter(holder.recycler\u view\u favorite,mContext);
adapter.setData(innerData);
holder.recycler\u view\u favorite.setAdapter(适配器);
}
@凌驾
public int getItemCount(){
返回data.size();
}
公共无效设置数据(列表数据){
这个数据=数据;
}
类ViewHolder扩展了RecyclerView.ViewHolder{
RecyclerView recycler\u view\u favorite;
可扩展卡查看主配置文件卡;
ViewHolder(视图项视图){
超级(项目视图);
//从行布局中获取视图的对象
main\u profile\u card=itemView.findviewbyd(R.id.main\u profile\u card);
recycler\u view\u favorite=itemView.findViewById(R.id.recycler\u view\u favorite);
}
}
}
InnerFavAdapter.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);
}
}
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);
}
}
}
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);
}
}
}
公共类InnerFavAdapter扩展了RecyclerView.Adapter{
私人名单数据;
私人回收站;
私有整数i=0;
私有上下文;
公共InnerFavAdapter(RecyclerView RecyclerView,上下文){
this.recyclerView=recyclerView;
this.mContext=上下文;
}
@非空
@凌驾
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent,int viewType){
//充气机创建行fr