hiashutoshsingh
hiashutoshsingh

Reputation: 1020

putting recyclerview in other Listview

enter image description hereThis is the file i am inflating in other recyclerview and i have also attached another recyclerview in it, but i can't set it.

I have also searched some references but can't find the right one. Help me out here how to do it.

Thank you.

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="250dp"
    android:layout_margin="10dp"
    android:elevation="3dp"
    app:cardBackgroundColor="#b7a2d6"
    app:cardCornerRadius="10dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/lesson_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="LessonName"
            android:textColor="#fff"
            android:textSize="25sp"
            android:textStyle="bold" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerViewChapters"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/lesson_name"
            android:padding="10dp" />
    </RelativeLayout>
</android.support.v7.widget.CardView>

SubjectActivity:

public class SubjectActivity extends AppCompatActivity {

    private RecyclerView lessonNamerecyclerView, recyclerViewChapters;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_subject);

        lessonNamerecyclerView = (RecyclerView) findViewById(R.id.recyclerViewLesson);
        recyclerViewChapters = (RecyclerView) findViewById(R.id.recyclerViewChapters);

        lessonNamerecyclerView.setLayoutManager(new LinearLayoutManager(this));
        lessonNamerecyclerView.setAdapter(new LessonAdapter());

        recyclerViewChapters.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true));
        recyclerViewChapters.setAdapter(new DemoVideoAdapter());


    }

}

Upvotes: 1

Views: 771

Answers (1)

Mayank Bhatnagar
Mayank Bhatnagar

Reputation: 1336

I have implemented something similar i made a main listview instead of recycle view and made horizontal recycleview as row of llistview. Adding some code snippets Hope it help.

Main Activity - only simple list view
 listview= (ListView) findViewById(R.id.listView);
    ListAdapter adapter=new ListAdapter(this);
    listview.setAdapter(adapter);

List Adapter
public class ListAdapter extends BaseAdapter {
private Context context;


private int lengt=4;
private String[] names={"Item 1","Item 2","Item 3 ","Item 4","Item 5"};
LayoutInflater layoutInflater;
RecyclerAdapter recyclerAdapter;
public ListAdapter(Context context){
    this.context=context;
    layoutInflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    recyclerAdapter=new RecyclerAdapter(context);
}



@Override
public int getCount() {
    return lengt;
}

@Override
public Object getItem(int i) {
    return null;
}

@Override
public long getItemId(int i) {
    return 0;
}

@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
    View view1=layoutInflater.inflate(R.layout.recyclelayout,null,false);
    // here was text view 
   // TextView tittle=(TextView)view1.findViewById(R.id.textView);
   // tittle.setText(names[i]);
   // TextView se=(TextView)view1.findViewById(R.id.textView2);
   // se.setText("See All >");
    RecyclerView recyclerView=(RecyclerView)view1.findViewById(R.id.recycler);
    recyclerView.setAdapter(recyclerAdapter);

    LinearLayoutManager linearLayoutManager=new LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false);
    recyclerView.setLayoutManager(linearLayoutManager);
    return view1;
}

}

xml of row of listview that is recyclelayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:clipToPadding="false">
<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:id="@+id/recycler">
</android.support.v7.widget.RecyclerView>

And the Recycleview adapter class is this.

class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.RecyclerHolder> {
public int[] image={R.drawable.ic_close_24dp,R.drawable.ic_more_vert_24dp,R.drawable.ic_play_arrow_24dp,R.drawable.ic_skip_next_24dp};
private Context context;
public RecyclerAdapter(Context context){
    this.context=context;
}
@Override
public RecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclercontent,parent,false);
    RecyclerHolder vh=new RecyclerHolder(v);
    return vh;
}

@Override
public void onBindViewHolder(RecyclerHolder holder, int position) {
    holder.imageView.setImageResource(image[position]);
}

@Override
public int getItemCount() {
    return image.length;
}

public class RecyclerHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    public ImageView imageView;
    public CardView cardView;
    public RecyclerHolder(View itemView) {
        super(itemView);
        imageView=(ImageView)itemView.findViewById(R.id.imageView2);
        cardView=(CardView)itemView.findViewById(R.id.carview);
        imageView.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == imageView.getId()) {
            Toast.makeText(view.getContext(),"Item Pressed =" + String.valueOf(getAdapterPosition()),Toast.LENGTH_SHORT).show();
         //   Intent intent=new Intent(context,ItemInfo.class);
           // context.startActivity(intent);
        }
    }
}

}

It Looks Like this with each row have recycleview Any doubt just let me know. Hope it helps you.

EDIT- adding remaining xml files

recyclercontent.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff"
android:outlineProvider="bounds">
<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    android:translationZ="10dp"
    android:id="@+id/carview">
    <ImageView
        android:layout_width="150dp"
        android:layout_height="145dp"
        android:id="@+id/imageView2"
        android:layout_marginLeft="0dp" />
</android.support.v7.widget.CardView>

swipelayout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/imageView"
    android:layout_gravity="center_vertical" />

Upvotes: 2

Related Questions