【问题标题】:Android - how to add view to LinearLayout with RecylcerView.ViewHolderAndroid - 如何使用 RecyclerView.ViewHolder 将视图添加到 LinearLayout
【发布时间】:2015-07-27 12:13:37
【问题描述】:

我有一个物品清单。每行都有一个 TextView 标题和一个包含子视图的 LinearLayout。例如:

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

标题 1

孩子_1:price_1 price_2 price_3 price_4 price_5

孩子_2:price_1 price_2 price_3 price_4 price_5

孩子_3:price_1 price_2 price_3 price_4 price_5

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

标题 2

孩子_1:price_1 price_2 price_3 price_4 price_5

孩子_2:price_1 price_2 price_3 price_4 price_5

孩子_3:price_1 price_2 price_3 price_4 price_5

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

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

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

标题 1

孩子_1:price_1 price_2 price_3 price_4 price_5

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

标题 2

孩子_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>

我的适配器

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
    }
}

【问题讨论】:

    标签: android android-recyclerview android-viewholder


    【解决方案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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-13
      • 1970-01-01
      • 1970-01-01
      • 2013-04-15
      • 1970-01-01
      相关资源
      最近更新 更多