【问题标题】:Use both List View and Recycler View同时使用列表视图和回收站视图
【发布时间】:2016-11-28 06:55:25
【问题描述】:

我有一个具有布局的列表视图: 列表.xml

<RelativeLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.phongponix.trackingbodybuilding.MainActivity$PlaceholderFragment">

    <ListView android:id="@+id/lvExerciseList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:paddingBottom="50dp"
       ></ListView>
    <View
        android:layout_width="match_parent"
        android:layout_height="100dp"></View>
</RelativeLayout>

list_item.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:padding="20dp">
    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TableRow>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical">
                <ImageView
                    android:id="@+id/imgExcercisePhoto"
                    android:layout_width="100dp"
                    android:layout_height="100dp" />

                <TextView android:text="aaa"
                    android:id="@+id/tvExerciseName"
                    android:layout_width="100dp"
                    android:layout_height="wrap_content" />
            </LinearLayout>
            <android.support.v7.widget.RecyclerView
                android:id="@+id/rvExcerciseRecords"
                android:layout_width="100dp"
                android:layout_height="match_parent"
                android:orientation="horizontal">
            </android.support.v7.widget.RecyclerView>
        </TableRow>
    </TableLayout>
</LinearLayout>

我为 ListView 设置了 CustomAdapter,它工作正常。 现在我想为 RecyclerView 设置适配器,它将是水平方向。我的列表适配器中有这样的东西

public View getView(int i, View convertView, ViewGroup viewGroup) {
        View view = convertView;
        if (convertView == null) {
            view = inflater.inflate(R.layout.tracking_plan_list_item, null);
            viewHolder = new ViewHolder();
            viewHolder.title = (TextView) view.findViewById(R.id.tvExerciseName);
            view.setTag(viewHolder);
            recyclerView = (RecyclerView)view.findViewById(R.id.rvExcerciseRecords);
            recyclerView.setLayoutManager(linearLayoutManager);
            recyclerView.setAdapter(trackingPlanHorizontalAdapter);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        if (data.size() > 0) {
            ExerciseModel exercise = (ExerciseModel) data.get(i);
            viewHolder.title.setText(exercise.getTitle());
        } else {
            viewHolder.title.setText("No data");
        }

        return view;
    }

问题是它会导致这个错误:

java.lang.IllegalArgumentException: LayoutManager android.support.v7.widget.LinearLayoutManager@48264ed is already attached to a RecyclerView: android.support.v7.widget

如果我评论了 recyclerView.setLayoutManager(linearLayoutManager);我的回收器适配器永远不会执行 onCreateViewHolder 或 onBindViewHolder。我已经检查了这个回收器的数据,它不是一个空数组。

我应该怎么做才能在列表项的一侧同时拥有带有垂直滚动的列表视图和带有水平滚动的回收器视图??

【问题讨论】:

  • 我假设这个问题的原因是 getview 将被重用,所以在这种情况下,它将采用相同的 recyclerview 已经初始化一次,所以它的抛出错误
  • 你想在 listview 项目中使用 recyclerview 吗?
  • 是的,拉加文德拉。我想显示具有垂直方向的项目列表。在每个项目中,我想要一个具有水平滚动的另一个列表。

标签: android listview android-recyclerview


【解决方案1】:

将 linearLayoutManager 从全局更改为本地,如下所示:

    public View getView(int i, View convertView, ViewGroup viewGroup) {
            View view = convertView;
            if (convertView == null) {
                view = inflater.inflate(R.layout.tracking_plan_list_item, null);
                viewHolder = new ViewHolder();
                viewHolder.title = (TextView) view.findViewById(R.id.tvExerciseName);
                view.setTag(viewHolder);
                recyclerView = (RecyclerView)view.findViewById(R.id.rvExcerciseRecords);
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
                recyclerView.setLayoutManager(linearLayoutManager);
                recyclerView.setAdapter(trackingPlanHorizontalAdapter);
            } else {
                viewHolder = (ViewHolder) view.getTag();
            }

            if (data.size() > 0) {
                ExerciseModel exercise = (ExerciseModel) data.get(i);
                viewHolder.title.setText(exercise.getTitle());
            } else {
                viewHolder.title.setText("No data");
            }

            return view;
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-06
    • 1970-01-01
    • 1970-01-01
    • 2016-09-22
    • 2019-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多