使用列表视图和回收站视图

时间:2016-11-28 06:55:26

标签: android listview android-recyclerview

我有一个列表视图,其中包含布局: list.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

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

如何在列表项的旁边同时使用带有垂直滚动的列表视图和带有水平滚动的循环器视图?

1 个答案:

答案 0 :(得分:0)

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