Android - 如何使用RecylcerView.ViewHolder

时间:2015-07-26 18:23:52

标签: android android-recyclerview android-viewholder

我有一个项目列表。每行都有一个TextView标题和一个包含子视图的LinearLayout。例如:

+++++++++++++++++++++++

标题1

Child_1:price_1 price_2 price_3 price_4 price_5

Child_2:price_1 price_2 price_3 price_4 price_5

Child_3:price_1 price_2 price_3 price_4 price_5

+++++++++++++++++++++++

标题2

Child_1:price_1 price_2 price_3 price_4 price_5

Child_2:price_1 price_2 price_3 price_4 price_5

Child_3:price_1 price_2 price_3 price_4 price_5

+++++++++++++++++++++++

我遇到的问题是LinearLayout没有显示所有子视图。它只显示child_1及其价格,但不显示child_2和child_3。

+++++++++++++++++++++++

标题1

Child_1:price_1 price_2 price_3 price_4 price_5

+++++++++++++++++++++++

标题2

Child_1:price_1 price_2 price_3 price_4 price_5

+++++++++++++++++++++++

layout_main.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

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

        <LinearLayout
            android:id="@+id/child_linear_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" />

    </LinearLayout>
</LinearLayout>

item_child.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:f="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:layout_marginTop="3dp"
        android:layout_marginBottom="3dp">
        <TextView
            android:id="@+id/child_text_view"
            android:layout_width="75dp"
            android:layout_height="wrap_content" />

        <org.apmem.tools.layouts.FlowLayout
            android:id="@+id/flowLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="5dip"
            android:gravity="fill"
            android:orientation="horizontal"
            android:layout_toRightOf="@id/child_text_view"
            f:debugDraw="true"
            f:layoutDirection="ltr"/>
    </RelativeLayout>

</RelativeLayout>

MyAdapter

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements RecyclerView.OnItemTouchListener {
    private MyViewHolder mMyViewHolder;
    private MyObj mMyObj;

    @Override
    RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i)  { }

    @Override
    public int getItemCount() { }

    @Override
    public int getItemViewType(int position) { }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        protected TextView mTitleTextView;
        protected LinearLayout mLinearLayout;

        public MyViewHolder(View itemView) {
            super(itemView);

            mTitleTextView = (TextView) itemView.findViewById(R.id.title_text_view);
            mLinearLayout = (LinearLayout) itemView.findViewById(R.id.child_linear_layout);
        }
    }

    @Override
    
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
    
        if (viewHolder instanceof MyViewHolder) {
    
            mMyViewHolder = (MyViewHolder) viewHolder;
    
            mMyObj = (MyObj) mItems.get(i);
    
            mMyViewHolder.mTitleTextView.setText(
    mMyObj.getTitle());
            mMyViewHolder.mLinearLayout.removeAllViews();
    
            for (Map.Entry<String, ArrayList<MyObj2>> entry : mMyObj.getNewMap().entrySet()) {
    
                mMyViewHolder.mLinearLayout.addView(makeChildView(entry));
    
            }
    
        } else if () { }
    
    }

    private View makeChildView(Map.Entry<String, ArrayList<MyObj2>> entry) {
        // Inflate child text view and price
    }
}

1 个答案:

答案 0 :(得分:1)

我设法解决了我的问题。好吧,我需要将android:orientation =“vertical”添加到layout_main.xml中的根LinearLayout。我花了一段时间,因为我使用它时没有使用RecyclerView.ViewHolder。

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"> // This line fix it

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

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

        <LinearLayout
            android:id="@+id/child_linear_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" />

    </LinearLayout>
</LinearLayout>