phongtran phongtran - 6 days ago 6
Android Question

Use both List View and Recycler View

I have a List View which has layout:
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>


I setup CustomAdapter for ListView and it works fine.
Now i want setup Adapter for RecyclerView which will be Horizontal orientation. Something like this in my list Adapter

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


The problem is it will cause this error:

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


If i commented the line recyclerView.setLayoutManager(linearLayoutManager); My recycler adapter will never execute the onCreateViewHolder or onBindViewHolder. I checked the data for this recycler already and it's not an empty array.

What should i do to have both List View with Vertical Scroll and Recycler View with Horizontal scroll in side a list item??

Answer

Change the linearLayoutManager from global to local, like this :

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