• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to use both List View and Recycler View???

I have a List View which has layout:
list.xml:
HTML:
<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:
HTML:
<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, use it with ViewPager and it works fine. Now i want setup Adapter for RecyclerView which will be Horizontal orientation. Something like this in my list Adapter
Java:
public View getView(int i, View convertView, ViewGroup viewGroup) {
       View view = convertView;
       if (convertView == null) {
           view = inflater.inflate(R.layout.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;
   }
RecyclerView adapter:
Java:
    @Override
    public TrackingPlanHorizontalViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.tracking_plan_list_item_horizontal, parent, false);
        return new TrackingPlanHorizontalViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(TrackingPlanHorizontalViewHolder holder, int position) {
        holder.textView.setText(horizontalList.get(position));
    }

The problem is it will cause this error:

Java:
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 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??
 
You need to create a new LayoutManager for each list item. You cannot re-attach the same layout manager to multiple views. See here

http://stackoverflow.com/questions/...nside-a-parent-recyclerview/32086918#32086918

I would also create a new list adapter.
So you would need to write

Code:
recyclerView.setLayoutManager(new TrackingPlanLayoutManager());
recyclerView.setAdapter(new TrackingPlanHorizontalAdapter());
 
Last edited by a moderator:
Back
Top Bottom