Android 如何将列表映射到RecyclerView

Android 如何将列表映射到RecyclerView,android,xml,list,android-layout,android-recyclerview,Android,Xml,List,Android Layout,Android Recyclerview,我正在尝试将一个列表映射到Android中的RecyclerView,但结果完全是一团糟。 例如,我有一个这样的列表(只是为了解释,不是我的真实案例): 但结果与上述顺序并不完全相同。有些元素被复制,有些元素消失。例如: |----d-----| |----e-----| |----c-----| |=======| |----d-----| |----e-----| |=======| |----f-----| |----a-----| 下面是我的一段代码

我正在尝试将一个
列表映射到Android中的RecyclerView,但结果完全是一团糟。
例如,我有一个这样的列表(只是为了解释,不是我的真实案例):

但结果与上述顺序并不完全相同。有些元素被复制,有些元素消失。例如:

|----d-----|  
|----e-----|  
|----c-----|  
|=======|  
|----d-----|  
|----e-----|  
|=======|  
|----f-----|  
|----a-----|  
下面是我的一段代码:

    @Override
public void onBindViewHolder(ViewHolder holder, int position) {
    List<Event> list = eventList.get(position);
    if (holder.rows < list.size()) {
        holder.rowViewGroup.removeAllViews();
        holder.rows = 0;
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        TextView startView = new TextView(context);
        startView.setLayoutParams(layoutParams);
        Event headEvent = list.get(0);
        Calendar startCal = headEvent.getStartCal();
        startView.setText(String.format("%tD", startCal));
        holder.rowViewGroup.addView(startView);

    for (final Event event : list) {
        View element = LayoutInflater.from(context).inflate(R.layout.element, null);
        //start time view
        TextView startTimeView = (TextView)element.findViewById(R.id.eventStartTimeInElement);
        Calendar startTimeCal = event.getStartCal();
        startTimeView.setText(String.format("%tl:%tM %tp", startTimeCal, startTimeCal, startTimeCal));
        //end date time view
        TextView endView = (TextView)element.findViewById(R.id.eventEndTimeInElement);
        Calendar endCal = event.getEndCal();
        endView.setText(String.format("%tD  %tl:%tM %tp", endCal, endCal, endCal, endCal));
        //title view
        TextView title = (TextView)element.findViewById(R.id.eventTitleInElement);
        title.setText(event.getTitle());
        //member name

        Drawable divider = context.getResources().getDrawable(R.drawable.divider);
        ImageView dividerView = new ImageView(context);
        dividerView.setImageDrawable(divider);
        holder.rowViewGroup.addView(dividerView);
        holder.rowViewGroup.addView(element);
        holder.rows++;
        }
    }
} 
@覆盖
公共无效onBindViewHolder(ViewHolder,int位置){
List List=eventList.get(位置);
if(holder.rows
以下是my row.xml:

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

    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/cardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        card_view:cardCornerRadius="0dp"
        card_view:cardElevation="2sp"
        card_view:cardUseCompatPadding="true"
        >

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/rowView"
            android:paddingLeft="20dp"
            android:paddingRight="20dp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/eventStartTimeInRow"
                />

            </LinearLayout>

    </android.support.v7.widget.CardView>   

和element.xml(由row.xml包含):



我知道这不是一个很好的实施方法,所以有谁能告诉我一个更好的实施方法吗?谢谢…

我想你最好的办法是将你的列表平铺成一个列表

这只是为了
列表视图
的目的。您的适配器将起到将所有列表串成一个长列表的作用

首先,为嵌套列表的所有项创建一个列表索引

例如:

    List<List<String>> listOfLists = ... // your original data
    List<String> listForView = new ArrayList<String>();

    for (List<String> innerList : listOfLists) {
        for (String str : innerList) {
            listForView.add(str);
        }
        listForView.add("---"); // e.g. marker value for divider
    }
列表列表=…//您的原始数据
List listForView=新建ArrayList();
for(列表innerList:listOfLists){
for(字符串str:innerList){
listForView.add(str);
}
listForView.add(“--”)//例如,分隔符的标记值
}
现在,在适配器的
getItem()
中,只需返回
listForView.get(position)

只要嵌套列表结构发生更改,就可以重新执行此展开操作并调用
notifyDataSetChanged()


如果给定位置,你必须找出一个项目在哪个列表中,事情会变得有点棘手,但是你可以总是以类似的方式对内部列表进行索引。

但是我真的想使用CardView来分隔每个组。如果我使用扁平化列表,有什么更好的方法来实现这一点?我希望做类似的事情。你有什么进展吗?我用不同类型的行来解决它。你应该试试。只需重写getItemType()并生成一组不同的行。在我的例子中,我使用子列表的大小来指示itemType,并使用For循环来添加不同大小的子行。也许会帮助你。
    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/recycleViewRow">

    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/cardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        card_view:cardCornerRadius="0dp"
        card_view:cardElevation="2sp"
        card_view:cardUseCompatPadding="true"
        >

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/rowView"
            android:paddingLeft="20dp"
            android:paddingRight="20dp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/eventStartTimeInRow"
                />

            </LinearLayout>

    </android.support.v7.widget.CardView>   
    <?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">



    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Small Text"
            android:id="@+id/eventStartTimeInElement"
            android:layout_weight="2" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Small Text"
            android:id="@+id/eventEndTimeInElement"
            android:gravity="end"
            android:layout_weight="1"/>
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="left">

        <TextView 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"                            
            android:textAppearance="?android:attr/textAppearanceMedium"                            
            android:text="Medium Text"
            android:id="@+id/eventTitleInElement"/>

            </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="right">

            <android.support.v7.widget.CardView
                xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                card_view:cardCornerRadius="2dp"
                card_view:cardElevation="2sp"
                card_view:cardUseCompatPadding="true">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Small Text"
                android:id="@+id/memberNameInElement"
                android:gravity="end"
                />
            </android.support.v7.widget.CardView>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

</LinearLayout>   
    List<List<String>> listOfLists = ... // your original data
    List<String> listForView = new ArrayList<String>();

    for (List<String> innerList : listOfLists) {
        for (String str : innerList) {
            listForView.add(str);
        }
        listForView.add("---"); // e.g. marker value for divider
    }