我正在谈论的活动必须显示由CardViews填充的RecyclerView作为项目.我的目标是在每个CardView中显示一个RecyclerView.
这里我的Activity的基本的xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".ConjActivity" > <android.support.v7.widget.RecyclerView android:id="@+id/conjCardList" android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="false" /> </LinearLayout>
这里是我的CardView的RecyclerView的布局:
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/card_analysis" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/activity_vertical_margin" android:layout_marginBottom="@dimen/activity_vertical_margin" android:layout_marginLeft="@dimen/activity_horizontal_margin" android:layout_marginRight="@dimen/activity_horizontal_margin" android:padding="5dp" card_view:cardCornerRadius="5dp" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <android.support.v7.widget.RecyclerView android:id="@+id/item_mode" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingTop="5dp" android:paddingLeft="@dimen/activity_horizontal_margin" > </android.support.v7.widget.RecyclerView> </LinearLayout> </android.support.v7.widget.CardView>
所以,我大胆地做了我的第一次尝试,实现两个RecyclerView.Adapters,一个(我们称之为)“主要”RecyclerView,一个在每个CardView中单个:
以下是两段代码:
“主”RecyclerView(w / ViewHolders):
public class ConjCardAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{ private static Context context; private LinearLayoutManager llm; private static ConjFormAdapter formAdapt; private ArrayList<String> adapterList; private List<Object> items; private static final int HEADER = 0,CONJTYPE = 1,ITEM = 2; private Paradigma par; private String sel_vb; public ConjCardAdapter(List<Object> items){ this.items = items; context = ConjActivity.context; adapterList = new ArrayList<String>(); formAdapt = new ConjFormAdapter(adapterList); } public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup,int viewType){ switch(viewType){ case HEADER: return new HeaderHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_header,viewGroup,false)); case CONJTYPE: return new ConjTypeHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_conjtext,false)); case ITEM: return new ItemModeHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_item,false)); default: return null; } } public void onBindViewHolder(RecyclerView.ViewHolder vvh,final int pos) { HeaderItem headerItem; String stringItem; ArrayList<String> listItem; HeaderHolder hh; ConjTypeHolder cjh; ItemModeHolder imh; switch(getItemViewType(pos)){ case HEADER: // it doesn't really matter... break; case CONJTYPE: // it doesn't really matter... break; case ITEM: listItem = (ArrayList<String>) items.get(pos); imh = (ItemModeHolder) vvh; llm = new LinearLayoutManager(context); imh.rv_mode.setLayoutManager(llm); adapterList.addAll(listItem); imh.rv_mode.setAdapter(formAdapt); formAdapt.notifyDataSetChanged(); break; } } @Override public int getItemCount() { return items.size(); } @Override public int getItemViewType(int position) { switch(position){ case 0: return HEADER; case 1: return CONJTYPE; default: return ITEM; } } static class HeaderHolder extends RecyclerView.ViewHolder{ TextView verbo,paradigma,analisi; View divider; public HeaderHolder(View itemView) { super(itemView); verbo = (TextView) itemView.findViewById(R.id.verb); paradigma = (TextView) itemView.findViewById(R.id.paradigm); analisi = (TextView) itemView.findViewById(R.id.analysis); divider = (View) itemView.findViewById(R.id.divider); } } static class ConjTypeHolder extends RecyclerView.ViewHolder{ TextView type; public ConjTypeHolder(View itemView) { super(itemView); type = (TextView) itemView.findViewById(R.id.conj_type); } } : static class ItemModeHolder extends RecyclerView.ViewHolder{ RecyclerView rv_mode; public ItemModeHolder(View itemView) { super(itemView); rv_mode = (RecyclerView) itemView.findViewById(R.id.item_mode); } } }
项目RecyclerView(w / ViewHolder):
public class ConjFormAdapter extends RecyclerView.Adapter<ConjFormAdapter.FormHolder>{ private Context context; private ArrayList<String> list; private Paradigma par; private String sel_vb; private ArrayList<Long> ids; private HashMap<String,Long> mIdMap; private StringAnalisi analisi; private final int IND_MODE_TYPE=0,ELSE_MODE_TYPE=1,TENSE_TYPE=2,FORM_TYPE=3; public ConjFormAdapter(ArrayList<String> list){ this.list = list; // Constructor implementation } // other methods @Override public FormHolder onCreateViewHolder(ViewGroup viewGroup,int itemType) { switch(itemType){ case IND_MODE_TYPE: return new FormHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_mode_ind,false)); case ELSE_MODE_TYPE: return new FormHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_mode_else,false)); case TENSE_TYPE: return new FormHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_tense,false)); default: return new FormHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_form,false)); } } @Override public void onBindViewHolder(FormHolder fh,int pos) { fh.txt.setText(list.get(pos)); // other implementation } @Override public int getItemCount() { return list.size(); } @Override public int getItemViewType(int position) { String item = (String) list.get(position); if(item.equals(context.getResources().getString(R.string.ind))){ return IND_MODE_TYPE; } else if(item.equals(context.getResources().getString(R.string.subj))||item.equals(context.getResources().getString(R.string.imp))||item.equals(context.getResources().getString(R.string.inf))||item.equals(context.getResources().getString(R.string.pt))||item.equals(context.getResources().getString(R.string.ger))||item.equals(context.getResources().getString(R.string.gerv))||item.equals(context.getResources().getString(R.string.sup))){ return ELSE_MODE_TYPE; } else if(item.equals(context.getResources().getString(R.string.pres))||item.equals(context.getResources().getString(R.string.impf))||item.equals(context.getResources().getString(R.string.fut))||item.equals(context.getResources().getString(R.string.pf))||item.equals(context.getResources().getString(R.string.ppf))||item.equals(context.getResources().getString(R.string.futant))){ return TENSE_TYPE; } else { return FORM_TYPE; } } @Override public long getItemId(int position) { String item = list.get(position); return mIdMap.get(item); } @Override public void setHasStableIds(boolean hasStableIds) { super.setHasStableIds(true); } static class FormHolder extends RecyclerView.ViewHolder{ TextView txt; View divider1,divider2,divider3; public FormHolder(View itemView) { super(itemView); txt = (TextView) itemView.findViewById(R.id.text1); divider1 = (View) itemView.findViewById(R.id.conj_divider1); divider2 = (View) itemView.findViewById(R.id.conj_divider2); divider3 = (View) itemView.findViewById(R.id.conj_divider3); } } }
正如你所看到的,我认为在第一个实例化第二个适配器是正确的,并且每个项目的RecyclerView附加一个新的LayoutManager并设置第二个适配器是对的.但是,您可以看到我的“主要”Rec.View的前两个项目不包含另一个Rec.View工作,但其他的没有.
如何解决问题?请,我没有想法.
解决方法
我面对完全一样的问题.您必须在CardView中指定RecyclerView的布局高度.将其从wrap_content更改为dp中的特定值.如果父滚动为VERTICAL,则Nested RecyclerView不会包含其高度的内容,同样在父滚动为HORIZONTAL时也不会将内容包含在宽度上.
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/card_analysis" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/activity_vertical_margin" android:layout_marginBottom="@dimen/activity_vertical_margin" android:layout_marginLeft="@dimen/activity_horizontal_margin" android:layout_marginRight="@dimen/activity_horizontal_margin" android:padding="5dp" card_view:cardCornerRadius="5dp" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <android.support.v7.widget.RecyclerView android:id="@+id/item_mode" android:layout_width="wrap_content" android:layout_height="100dp" android:paddingTop="5dp" android:paddingLeft="@dimen/activity_horizontal_margin" > </android.support.v7.widget.RecyclerView> </LinearLayout> </android.support.v7.widget.CardView>