Skip to content

Instantly share code, notes, and snippets.

@gajerarajnit
Created August 22, 2017 12:08
Show Gist options
  • Save gajerarajnit/2e204822bd78bbec044d4719ca9aaab2 to your computer and use it in GitHub Desktop.
Save gajerarajnit/2e204822bd78bbec044d4719ca9aaab2 to your computer and use it in GitHub Desktop.
Expandable RecyclerView with parent and child item selection.
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**
* Created by rajnit on 22/08/17.
*/
public class Category implements Serializable {
private int id;
private String name;
public List<SubCategory> getSubCategoryList() {
return subCategoryList;
}
public void setSubCategoryList(List<SubCategory> subCategoryList) {
this.subCategoryList = subCategoryList;
}
private List<SubCategory> subCategoryList = new ArrayList<>();
public Category(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="4dp">
<LinearLayout
android:id="@+id/mRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="8dp">
<TextView
android:id="@+id/mPosition"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0" />
<TextView
android:id="@+id/mName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="32dp"
android:text="one" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/mSubRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f1f1f1"/>
</LinearLayout>
import android.content.Context;
import android.graphics.Typeface;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.rajnit.keylivemoney.R;
import java.util.List;
/**
* Created by rajnit on 22/08/17.
*/
public class CategoryAdatper extends RecyclerView.Adapter<CategoryAdatper.MyViewHolder> {
private List<Category> categoryList;
private Context context;
private int clickedPosition = -1;
private int subClickedPosition = -1;
public CategoryAdatper(Context context, List<Category> categoryList, int clickedPosition) {
this.categoryList = categoryList;
this.context = context;
this.clickedPosition = clickedPosition;
}
@Override
public CategoryAdatper.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.movie_list_row, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final CategoryAdatper.MyViewHolder holder, final int position) {
holder.mName.setText(categoryList.get(position).getName());
holder.mPosition.setText(String.valueOf(categoryList.get(position).getId()));
LinearLayoutManager manager = new LinearLayoutManager(context);
SubRecyclerViewAdapter adapter = new SubRecyclerViewAdapter(context, categoryList.get(position).getSubCategoryList(), -1, new SubRecyclerViewAdapter.SubSelectionInterface() {
@Override
public void onsubselection(int position) {
subClickedPosition = position;
}
});
holder.mSubRecyclerView.setLayoutManager(manager);
holder.mSubRecyclerView.setAdapter(adapter);
if (position == clickedPosition) {
holder.mSubRecyclerView.setVisibility(View.VISIBLE);
holder.mName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
holder.mName.setTypeface(null, Typeface.BOLD);
holder.mPosition.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
holder.mPosition.setTypeface(null, Typeface.BOLD);
} else {
holder.mSubRecyclerView.setVisibility(View.GONE);
holder.mName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
holder.mName.setTypeface(null, Typeface.NORMAL);
holder.mPosition.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
holder.mPosition.setTypeface(null, Typeface.NORMAL);
}
holder.row.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (clickedPosition != position) {
clickedPosition = position;
subClickedPosition = -1;
notifyDataSetChanged();
} else {
holder.mSubRecyclerView.setVisibility(View.GONE);
clickedPosition = -1;
subClickedPosition = -1;
notifyDataSetChanged();
}
}
});
}
@Override
public int getItemCount() {
return categoryList.size();
}
public int getCategoryPosition() {
return clickedPosition;
}
public int getSubCategoryPosition() {
return subClickedPosition;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView mPosition, mName;
public RecyclerView mSubRecyclerView;
public LinearLayout row;
public MyViewHolder(View view) {
super(view);
mPosition = (TextView) view.findViewById(R.id.mPosition);
mName = (TextView) view.findViewById(R.id.mName);
mSubRecyclerView = (RecyclerView) view.findViewById(R.id.mSubRecyclerView);
row = (LinearLayout) view.findViewById(R.id.mRow);
}
}
}
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.example.rajnit.keylivemoney.R;
import java.util.ArrayList;
import java.util.List;
public class ExpandableRecyclerViewActivity extends AppCompatActivity {
private static final String TAG = "ExpandableRecyclerViewA";
RecyclerView recyclerView;
Button mShowSelection;
List<Category> categoryList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_expandable_recycler_view);
recyclerView = (RecyclerView) findViewById(R.id.mRecyclerView);
mShowSelection = (Button) findViewById(R.id.mShowSelection);
createDummyData();
LinearLayoutManager manager = new LinearLayoutManager(this);
final RecyclerViewAdapter adapter = new RecyclerViewAdapter(this, categoryList, -1);
recyclerView.setLayoutManager(manager);
recyclerView.setAdapter(adapter);
mShowSelection.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: " + adapter.getCategoryPosition() + " -> " + adapter.getSubCategoryPosition());
}
});
}
private void createDummyData() {
categoryList.add(new Category(1, "one"));
categoryList.get(0).getSubCategoryList().add(new SubCategory(1, "sub one"));
categoryList.get(0).getSubCategoryList().add(new SubCategory(2, "sub two"));
categoryList.get(0).getSubCategoryList().add(new SubCategory(3, "sub three"));
categoryList.add(new Category(2, "two"));
categoryList.get(1).getSubCategoryList().add(new SubCategory(1, "sub one"));
categoryList.get(1).getSubCategoryList().add(new SubCategory(2, "sub two"));
categoryList.get(1).getSubCategoryList().add(new SubCategory(3, "sub three"));
categoryList.add(new Category(3, "three"));
categoryList.get(2).getSubCategoryList().add(new SubCategory(1, "sub one"));
categoryList.get(2).getSubCategoryList().add(new SubCategory(2, "sub two"));
categoryList.get(2).getSubCategoryList().add(new SubCategory(3, "sub three"));
categoryList.add(new Category(4, "four"));
categoryList.get(3).getSubCategoryList().add(new SubCategory(1, "sub one"));
categoryList.get(3).getSubCategoryList().add(new SubCategory(2, "sub two"));
categoryList.get(3).getSubCategoryList().add(new SubCategory(3, "sub three"));
categoryList.add(new Category(5, "five"));
categoryList.get(4).getSubCategoryList().add(new SubCategory(1, "sub one"));
categoryList.get(4).getSubCategoryList().add(new SubCategory(2, "sub two"));
categoryList.get(4).getSubCategoryList().add(new SubCategory(3, "sub three"));
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="12dp">
<LinearLayout
android:id="@+id/mRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/mPosition"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0" />
<TextView
android:id="@+id/mName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="32dp"
android:text="one" />
</LinearLayout>
</LinearLayout>
import java.io.Serializable;
/**
* Created by rajnit on 22/08/17.
*/
public class SubCategory implements Serializable {
private int subId;
private String subName;
public SubCategory(int subId, String subName) {
this.subId = subId;
this.subName = subName;
}
public int getSubId() {
return subId;
}
public void setSubId(int subId) {
this.subId = subId;
}
public String getSubName() {
return subName;
}
public void setSubName(String subName) {
this.subName = subName;
}
}
import android.content.Context;
import android.graphics.Typeface;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.rajnit.keylivemoney.R;
import java.util.List;
/**
* Created by rajnit on 22/08/17.
*/
public class SubCategoryAdatper extends RecyclerView.Adapter<SubCategoryAdatper.MyViewHolder> {
SubSelectionInterface subSelectionInterface;
private List<SubCategory> categoryList;
private Context context;
private int clickedPosition = -1;
public SubCategoryAdatper(Context context, List<SubCategory> categoryList, int clickedPosition, SubSelectionInterface subSelectionInterface) {
this.categoryList = categoryList;
this.context = context;
this.clickedPosition = clickedPosition;
this.subSelectionInterface = subSelectionInterface;
}
@Override
public SubCategoryAdatper.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.sub_movie_list_row, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(SubCategoryAdatper.MyViewHolder holder, final int position) {
holder.mName.setText(categoryList.get(position).getSubName());
holder.mPosition.setText(String.valueOf(categoryList.get(position).getSubId()));
if (position == clickedPosition) {
holder.mName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
holder.mName.setTypeface(null, Typeface.BOLD);
holder.mPosition.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
holder.mPosition.setTypeface(null, Typeface.BOLD);
} else {
holder.mName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
holder.mName.setTypeface(null, Typeface.NORMAL);
holder.mPosition.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
holder.mPosition.setTypeface(null, Typeface.NORMAL);
}
holder.row.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (clickedPosition != position) {
clickedPosition = position;
subSelectionInterface.onsubselection(position);
notifyDataSetChanged();
}
}
});
}
@Override
public int getItemCount() {
return categoryList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView mPosition, mName;
public LinearLayout row;
public MyViewHolder(View view) {
super(view);
mPosition = (TextView) view.findViewById(R.id.mPosition);
mName = (TextView) view.findViewById(R.id.mName);
row = (LinearLayout) view.findViewById(R.id.mRow);
}
}
public interface SubSelectionInterface {
void onsubselection(int position);
}
}
@KALLL
Copy link

KALLL commented Aug 6, 2019

where is the activity_expandable_recycler_view.xml

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment