Android:如何在CardView中插入RecyclerView?

Android:如何在CardView中插入RecyclerView?,第1张

概述我正在谈论的活动必须显示由CardViews填充的RecyclerView作为项目.我的目标是在每个CardView中显示一个RecyclerView. 这里我的Activity的基本的xml: <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res 我正在谈论的活动必须显示由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.CardVIEwxmlns: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>
总结

以上是内存溢出为你收集整理的Android:如何在CardView中插入RecyclerView?全部内容,希望文章能够帮你解决Android:如何在CardView中插入RecyclerView?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1133750.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存