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