Skip to content

Commit cf56043

Browse files
author
-global
committed
added demo app for dynamic expandable recyclerview
2 parents ebf7d71 + 0ee1055 commit cf56043

File tree

9 files changed

+370
-110
lines changed

9 files changed

+370
-110
lines changed

build.gradle

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
buildscript {
44
repositories {
55
jcenter()
6-
maven {
7-
url 'https://maven.google.com/'
8-
name 'Google'
9-
}
6+
google()
107
}
118
dependencies {
129
classpath 'com.android.tools.build:gradle:2.3.3'
@@ -19,10 +16,7 @@ buildscript {
1916
allprojects {
2017
repositories {
2118
jcenter()
22-
maven {
23-
url 'https://maven.google.com/'
24-
name 'Google'
25-
}
19+
google()
2620
}
2721
}
2822

dynamic-recycler-view/build.gradle

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ android {
99
targetSdkVersion 26
1010
versionCode 1
1111
versionName "1.0"
12-
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
12+
// testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1313
}
1414
buildTypes {
1515
release {
@@ -20,11 +20,13 @@ android {
2020
}
2121

2222
dependencies {
23-
compile fileTree(dir: 'libs', include: ['*.jar'])
24-
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
25-
exclude group: 'com.android.support', module: 'support-annotations'
26-
})
23+
compile fileTree(include: ['*.jar'], dir: 'libs')
24+
/*androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
25+
exclude group: 'com.android.support', module: 'support-annotations'
26+
})*/
2727
compile 'com.android.support:appcompat-v7:26.1.0'
2828
compile 'com.android.support.constraint:constraint-layout:1.0.2'
29-
testCompile 'junit:junit:4.12'
29+
// testCompile 'junit:junit:4.12'
30+
compile 'com.android.support:design:26.1.0'
31+
compile 'com.android.support:cardview-v7:26.1.0'
3032
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.rohitss.dynamicrecycler;
2+
3+
import java.io.Serializable;
4+
5+
/**
6+
* <b></b>
7+
* <p>This class is used to </p>
8+
* Created by Rohit.
9+
*/
10+
class DummyChildDataItem implements Serializable {
11+
private String childName;
12+
13+
public String getChildName() {
14+
return childName;
15+
}
16+
17+
public void setChildName(String childName) {
18+
this.childName = childName;
19+
}
20+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.rohitss.dynamicrecycler;
2+
3+
import java.io.Serializable;
4+
import java.util.ArrayList;
5+
6+
/**
7+
* <b></b>
8+
* <p>This class is used to </p>
9+
* Created by Rohit.
10+
*/
11+
class DummyParentDataItem implements Serializable {
12+
private String parentName;
13+
private ArrayList<DummyChildDataItem> childDataItems;
14+
15+
public String getParentName() {
16+
return parentName;
17+
}
18+
19+
public void setParentName(String parentName) {
20+
this.parentName = parentName;
21+
}
22+
23+
public ArrayList<DummyChildDataItem> getChildDataItems() {
24+
return childDataItems;
25+
}
26+
27+
public void setChildDataItems(ArrayList<DummyChildDataItem> childDataItems) {
28+
this.childDataItems = childDataItems;
29+
}
30+
}
Lines changed: 174 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,185 @@
11
package com.rohitss.dynamicrecycler;
22

3-
import android.support.v7.app.AppCompatActivity;
3+
import android.content.Context;
44
import android.os.Bundle;
5+
import android.support.v4.content.ContextCompat;
6+
import android.support.v7.app.AppCompatActivity;
7+
import android.support.v7.widget.LinearLayoutManager;
8+
import android.support.v7.widget.RecyclerView;
9+
import android.view.Gravity;
10+
import android.view.LayoutInflater;
11+
import android.view.View;
12+
import android.view.ViewGroup;
13+
import android.widget.LinearLayout;
14+
import android.widget.TextView;
15+
import android.widget.Toast;
16+
17+
import java.util.ArrayList;
518

619
public class DynamicRecyclerActivity extends AppCompatActivity {
20+
private RecyclerView mRecyclerView;
21+
private Context mContext;
22+
723
@Override
824
protected void onCreate(Bundle savedInstanceState) {
925
super.onCreate(savedInstanceState);
1026
setContentView(R.layout.activity_dynamic_recycler);
27+
setContentView(R.layout.activity_dynamic_recycler);
28+
mContext = DynamicRecyclerActivity.this;
29+
mRecyclerView = findViewById(R.id.recyclerView);
30+
RecyclerDataAdapter recyclerDataAdapter = new RecyclerDataAdapter(getDummyDataToPass());
31+
mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
32+
mRecyclerView.setAdapter(recyclerDataAdapter);
33+
mRecyclerView.setHasFixedSize(true);
34+
}
35+
36+
private ArrayList<DummyParentDataItem> getDummyDataToPass() {
37+
ArrayList<DummyParentDataItem> dummyDataItems = new ArrayList<>();
38+
ArrayList<DummyChildDataItem> dummyChildDataItems;
39+
DummyParentDataItem dummyParentDataItem;
40+
DummyChildDataItem dummyChildDataItem;
41+
/////////
42+
dummyParentDataItem = new DummyParentDataItem();
43+
dummyParentDataItem.setParentName("Parent 1");
44+
dummyChildDataItems = new ArrayList<>();
45+
//
46+
dummyChildDataItem = new DummyChildDataItem();
47+
dummyChildDataItem.setChildName("Child Item 1");
48+
dummyChildDataItems.add(dummyChildDataItem);
49+
//
50+
dummyParentDataItem.setChildDataItems(dummyChildDataItems);
51+
dummyDataItems.add(dummyParentDataItem);
52+
////////
53+
dummyParentDataItem = new DummyParentDataItem();
54+
dummyParentDataItem.setParentName("Parent 2");
55+
dummyChildDataItems = new ArrayList<>();
56+
//
57+
dummyChildDataItem = new DummyChildDataItem();
58+
dummyChildDataItem.setChildName("Child Item 1");
59+
dummyChildDataItems.add(dummyChildDataItem);
60+
//
61+
dummyChildDataItem = new DummyChildDataItem();
62+
dummyChildDataItem.setChildName("Child Item 2");
63+
dummyChildDataItems.add(dummyChildDataItem);
64+
//
65+
dummyParentDataItem.setChildDataItems(dummyChildDataItems);
66+
dummyDataItems.add(dummyParentDataItem);
67+
////////
68+
dummyParentDataItem = new DummyParentDataItem();
69+
dummyParentDataItem.setParentName("Parent 3");
70+
dummyChildDataItems = new ArrayList<>();
71+
//
72+
dummyChildDataItem = new DummyChildDataItem();
73+
dummyChildDataItem.setChildName("Child Item 1");
74+
dummyChildDataItems.add(dummyChildDataItem);
75+
//
76+
dummyChildDataItem = new DummyChildDataItem();
77+
dummyChildDataItem.setChildName("Child Item 2");
78+
dummyChildDataItems.add(dummyChildDataItem);
79+
//
80+
dummyChildDataItem = new DummyChildDataItem();
81+
dummyChildDataItem.setChildName("Child Item 3");
82+
dummyChildDataItems.add(dummyChildDataItem);
83+
//
84+
dummyChildDataItem = new DummyChildDataItem();
85+
dummyChildDataItem.setChildName("Child Item 4");
86+
dummyChildDataItems.add(dummyChildDataItem);
87+
//
88+
dummyChildDataItem = new DummyChildDataItem();
89+
dummyChildDataItem.setChildName("Child Item 5");
90+
dummyChildDataItems.add(dummyChildDataItem);
91+
//
92+
dummyParentDataItem.setChildDataItems(dummyChildDataItems);
93+
dummyDataItems.add(dummyParentDataItem);
94+
////////
95+
return dummyDataItems;
96+
}
97+
98+
private class RecyclerDataAdapter extends RecyclerView.Adapter<RecyclerDataAdapter.MyViewHolder> {
99+
private ArrayList<DummyParentDataItem> dummyParentDataItems;
100+
101+
RecyclerDataAdapter(ArrayList<DummyParentDataItem> dummyParentDataItems) {
102+
this.dummyParentDataItems = dummyParentDataItems;
103+
}
104+
105+
@Override
106+
public RecyclerDataAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
107+
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_parent_child_listing, parent, false);
108+
return new MyViewHolder(itemView);
109+
}
110+
111+
@Override
112+
public void onBindViewHolder(RecyclerDataAdapter.MyViewHolder holder, int position) {
113+
DummyParentDataItem dummyParentDataItem = dummyParentDataItems.get(position);
114+
holder.textView_parentName.setText(dummyParentDataItem.getParentName());
115+
//
116+
int noOfChildTextViews = holder.linearLayout_childItems.getChildCount();
117+
int noOfChild = dummyParentDataItem.getChildDataItems().size();
118+
if (noOfChild < noOfChildTextViews) {
119+
for (int index = noOfChild; index < noOfChildTextViews; index++) {
120+
TextView currentTextView = (TextView) holder.linearLayout_childItems.getChildAt(index);
121+
currentTextView.setVisibility(View.GONE);
122+
}
123+
}
124+
for (int textViewIndex = 0; textViewIndex < noOfChild; textViewIndex++) {
125+
TextView currentTextView = (TextView) holder.linearLayout_childItems.getChildAt(textViewIndex);
126+
currentTextView.setText(dummyParentDataItem.getChildDataItems().get(textViewIndex).getChildName());
127+
/*currentTextView.setOnClickListener(new View.OnClickListener() {
128+
@Override
129+
public void onClick(View view) {
130+
Toast.makeText(mContext, "" + ((TextView) view).getText().toString(), Toast.LENGTH_SHORT).show();
131+
}
132+
});*/
133+
}
134+
}
135+
136+
@Override
137+
public int getItemCount() {
138+
return dummyParentDataItems.size();
139+
}
140+
141+
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
142+
private Context context;
143+
private TextView textView_parentName;
144+
private LinearLayout linearLayout_childItems;
145+
146+
MyViewHolder(View itemView) {
147+
super(itemView);
148+
context = itemView.getContext();
149+
textView_parentName = itemView.findViewById(R.id.tv_parentName);
150+
linearLayout_childItems = itemView.findViewById(R.id.ll_child_items);
151+
linearLayout_childItems.setVisibility(View.GONE);
152+
int intMaxNoOfChild = 0;
153+
for (int index = 0; index < dummyParentDataItems.size(); index++) {
154+
int intMaxSizeTemp = dummyParentDataItems.get(index).getChildDataItems().size();
155+
if (intMaxSizeTemp > intMaxNoOfChild) intMaxNoOfChild = intMaxSizeTemp;
156+
}
157+
for (int indexView = 0; indexView < intMaxNoOfChild; indexView++) {
158+
TextView textView = new TextView(context);
159+
textView.setId(indexView);
160+
textView.setPadding(0, 20, 0, 20);
161+
textView.setGravity(Gravity.CENTER);
162+
textView.setBackground(ContextCompat.getDrawable(context, R.drawable.background_sub_module_text));
163+
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
164+
textView.setOnClickListener(this);
165+
linearLayout_childItems.addView(textView, layoutParams);
166+
}
167+
textView_parentName.setOnClickListener(this);
168+
}
169+
170+
@Override
171+
public void onClick(View view) {
172+
if (view.getId() == R.id.tv_parentName) {
173+
if (linearLayout_childItems.getVisibility() == View.VISIBLE) {
174+
linearLayout_childItems.setVisibility(View.GONE);
175+
} else {
176+
linearLayout_childItems.setVisibility(View.VISIBLE);
177+
}
178+
} else {
179+
TextView textViewClicked = (TextView) view;
180+
Toast.makeText(context, "" + textViewClicked.getText().toString(), Toast.LENGTH_SHORT).show();
181+
}
182+
}
183+
}
11184
}
12185
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
3+
<item
4+
android:bottom="-2dp"
5+
android:left="-2dp"
6+
android:right="-2dp">
7+
<shape android:shape="rectangle">
8+
<stroke
9+
android:width="1dp"
10+
android:color="#FFBDBDBD"/>
11+
</shape>
12+
</item>
13+
</layer-list>

dynamic-recycler-view/src/main/res/layout/activity_dynamic_recycler.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
android:layout_height="match_parent"
88
tools:context="com.rohitss.dynamicrecycler.DynamicRecyclerActivity">
99

10-
<TextView
11-
android:layout_width="wrap_content"
12-
android:layout_height="wrap_content"
13-
android:text="Hello World!"
10+
<android.support.v7.widget.RecyclerView
11+
android:id="@+id/recyclerView"
12+
android:layout_width="match_parent"
13+
android:layout_height="match_parent"
14+
android:scrollbars="vertical"
1415
app:layout_constraintBottom_toBottomOf="parent"
1516
app:layout_constraintLeft_toLeftOf="parent"
1617
app:layout_constraintRight_toRightOf="parent"
1718
app:layout_constraintTop_toTopOf="parent"/>
18-
1919
</android.support.constraint.ConstraintLayout>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<android.support.v7.widget.CardView
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:app="http://schemas.android.com/apk/res-auto"
5+
android:layout_width="match_parent"
6+
android:layout_height="wrap_content"
7+
android:layout_margin="10dp"
8+
app:cardCornerRadius="4dp">
9+
10+
<LinearLayout
11+
android:layout_width="match_parent"
12+
android:layout_height="wrap_content"
13+
android:orientation="vertical">
14+
15+
<TextView
16+
android:id="@+id/tv_parentName"
17+
android:layout_width="match_parent"
18+
android:layout_height="wrap_content"
19+
android:padding="16dp"/>
20+
21+
<LinearLayout
22+
android:id="@+id/ll_child_items"
23+
android:layout_width="match_parent"
24+
android:layout_height="wrap_content"
25+
android:orientation="vertical">
26+
</LinearLayout>
27+
</LinearLayout>
28+
</android.support.v7.widget.CardView>

0 commit comments

Comments
 (0)