Skip to content

Commit

Permalink
added Expandable RecyclerView example
Browse files Browse the repository at this point in the history
  • Loading branch information
RohitWoxi committed Nov 30, 2017
1 parent 29eb589 commit 0ee1055
Show file tree
Hide file tree
Showing 9 changed files with 378 additions and 106 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
Expand All @@ -15,6 +16,7 @@ buildscript {
allprojects {
repositories {
jcenter()
google()
}
}

Expand Down
22 changes: 12 additions & 10 deletions dynamic-recycler-view/build.gradle
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "26.0.0"
compileSdkVersion 26
buildToolsVersion "26.0.2"
defaultConfig {
applicationId "com.rohitss.androiddesignpatterns"
minSdkVersion 16
targetSdkVersion 25
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
// testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
Expand All @@ -20,11 +20,13 @@ android {
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile fileTree(include: ['*.jar'], dir: 'libs')
/*androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})*/
compile 'com.android.support:appcompat-v7:26.1.0'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
// testCompile 'junit:junit:4.12'
compile 'com.android.support:design:26.1.0'
compile 'com.android.support:cardview-v7:26.1.0'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.rohitss.dynamicrecycler;

import java.io.Serializable;

/**
* <b></b>
* <p>This class is used to </p>
* Created by Rohit.
*/
class DummyChildDataItem implements Serializable {
private String childName;

public String getChildName() {
return childName;
}

public void setChildName(String childName) {
this.childName = childName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.rohitss.dynamicrecycler;

import java.io.Serializable;
import java.util.ArrayList;

/**
* <b></b>
* <p>This class is used to </p>
* Created by Rohit.
*/
class DummyParentDataItem implements Serializable {
private String parentName;
private ArrayList<DummyChildDataItem> childDataItems;

public String getParentName() {
return parentName;
}

public void setParentName(String parentName) {
this.parentName = parentName;
}

public ArrayList<DummyChildDataItem> getChildDataItems() {
return childDataItems;
}

public void setChildDataItems(ArrayList<DummyChildDataItem> childDataItems) {
this.childDataItems = childDataItems;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,189 @@
package com.rohitss.dynamicrecycler;

import android.support.v7.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private Context mContext;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = MainActivity.this;
mRecyclerView = findViewById(R.id.recyclerView);
RecyclerDataAdapter recyclerDataAdapter = new RecyclerDataAdapter(getDummyDataToPass());
mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
mRecyclerView.setAdapter(recyclerDataAdapter);
mRecyclerView.setHasFixedSize(true);
}

private ArrayList<DummyParentDataItem> getDummyDataToPass() {
ArrayList<DummyParentDataItem> dummyDataItems = new ArrayList<>();
ArrayList<DummyChildDataItem> dummyChildDataItems;
DummyParentDataItem dummyParentDataItem;
DummyChildDataItem dummyChildDataItem;
/////////
dummyParentDataItem = new DummyParentDataItem();
dummyParentDataItem.setParentName("Parent 1");
dummyChildDataItems = new ArrayList<>();
//
dummyChildDataItem = new DummyChildDataItem();
dummyChildDataItem.setChildName("Child Item 1");
dummyChildDataItems.add(dummyChildDataItem);
//
dummyParentDataItem.setChildDataItems(dummyChildDataItems);
dummyDataItems.add(dummyParentDataItem);
////////
dummyParentDataItem = new DummyParentDataItem();
dummyParentDataItem.setParentName("Parent 2");
dummyChildDataItems = new ArrayList<>();
//
dummyChildDataItem = new DummyChildDataItem();
dummyChildDataItem.setChildName("Child Item 1");
dummyChildDataItems.add(dummyChildDataItem);
//
dummyChildDataItem = new DummyChildDataItem();
dummyChildDataItem.setChildName("Child Item 2");
dummyChildDataItems.add(dummyChildDataItem);
//
dummyParentDataItem.setChildDataItems(dummyChildDataItems);
dummyDataItems.add(dummyParentDataItem);
////////
dummyParentDataItem = new DummyParentDataItem();
dummyParentDataItem.setParentName("Parent 3");
dummyChildDataItems = new ArrayList<>();
//
dummyChildDataItem = new DummyChildDataItem();
dummyChildDataItem.setChildName("Child Item 1");
dummyChildDataItems.add(dummyChildDataItem);
//
dummyChildDataItem = new DummyChildDataItem();
dummyChildDataItem.setChildName("Child Item 2");
dummyChildDataItems.add(dummyChildDataItem);
//
dummyChildDataItem = new DummyChildDataItem();
dummyChildDataItem.setChildName("Child Item 3");
dummyChildDataItems.add(dummyChildDataItem);
//
dummyChildDataItem = new DummyChildDataItem();
dummyChildDataItem.setChildName("Child Item 4");
dummyChildDataItems.add(dummyChildDataItem);
//
dummyChildDataItem = new DummyChildDataItem();
dummyChildDataItem.setChildName("Child Item 5");
dummyChildDataItems.add(dummyChildDataItem);
//
dummyParentDataItem.setChildDataItems(dummyChildDataItems);
dummyDataItems.add(dummyParentDataItem);
////////
return dummyDataItems;
}

private class RecyclerDataAdapter extends RecyclerView.Adapter<RecyclerDataAdapter.MyViewHolder> {
private ArrayList<DummyParentDataItem> dummyParentDataItems;

RecyclerDataAdapter(ArrayList<DummyParentDataItem> dummyParentDataItems) {
this.dummyParentDataItems = dummyParentDataItems;
int intMaxSize = 0;
for (int index = 0; index < dummyParentDataItems.size(); index++) {
int intMaxSizeTemp = dummyParentDataItems.get(index).getChildDataItems().size();
if (intMaxSizeTemp > intMaxSize) intMaxSize = intMaxSizeTemp;
}
}

@Override
public RecyclerDataAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_parent_child_listing, parent, false);
return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(RecyclerDataAdapter.MyViewHolder holder, int position) {
DummyParentDataItem dummyParentDataItem = dummyParentDataItems.get(position);
holder.tv_parentName.setText(dummyParentDataItem.getParentName());
//
int noOfTextViews = holder.ll_child_items.getChildCount();
int noOfSubModules = dummyParentDataItem.getChildDataItems().size();
if (noOfSubModules < noOfTextViews) {
for (int index = noOfSubModules; index < noOfTextViews; index++) {
TextView currentTextView = (TextView) holder.ll_child_items.getChildAt(index);
currentTextView.setVisibility(View.GONE);
}
}
for (int textViewIndex = 0; textViewIndex < noOfSubModules; textViewIndex++) {
TextView currentTextView = (TextView) holder.ll_child_items.getChildAt(textViewIndex);
currentTextView.setText(dummyParentDataItem.getChildDataItems().get(textViewIndex).getChildName());
/*currentTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(mContext, "" + ((TextView) view).getText().toString(), Toast.LENGTH_SHORT).show();
}
});*/
}
}

@Override
public int getItemCount() {
return dummyParentDataItems.size();
}

class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private Context context;
private TextView tv_parentName;
private LinearLayout ll_child_items;

MyViewHolder(View itemView) {
super(itemView);
context = itemView.getContext();
tv_parentName = itemView.findViewById(R.id.tv_parentName);
ll_child_items = itemView.findViewById(R.id.ll_child_items);
ll_child_items.setVisibility(View.GONE);
int intMaxSize = 0;
for (int index = 0; index < dummyParentDataItems.size(); index++) {
int intMaxSizeTemp = dummyParentDataItems.get(index).getChildDataItems().size();
if (intMaxSizeTemp > intMaxSize) intMaxSize = intMaxSizeTemp;
}
for (int indexView = 0; indexView < intMaxSize; indexView++) {
TextView textView = new TextView(context);
textView.setId(indexView);
textView.setPadding(0, 20, 0, 20);
textView.setGravity(Gravity.CENTER);
textView.setBackground(ContextCompat.getDrawable(context, R.drawable.background_sub_module_text));
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
textView.setOnClickListener(this);
ll_child_items.addView(textView, layoutParams);
}
tv_parentName.setOnClickListener(this);
}

@Override
public void onClick(View view) {
if (view.getId() == R.id.tv_parentName) {
if (ll_child_items.getVisibility() == View.VISIBLE) {
ll_child_items.setVisibility(View.GONE);
} else {
ll_child_items.setVisibility(View.VISIBLE);
}
} else {
TextView textViewClicked = (TextView) view;
Toast.makeText(context, "" + textViewClicked.getText().toString(), Toast.LENGTH_SHORT).show();
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="-2dp"
android:left="-2dp"
android:right="-2dp">
<shape android:shape="rectangle">
<stroke
android:width="1dp"
android:color="#FFBDBDBD"/>
</shape>
</item>
</layer-list>
10 changes: 5 additions & 5 deletions dynamic-recycler-view/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
android:layout_height="match_parent"
tools:context="com.rohitss.dynamicrecycler.MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
android:layout_margin="10dp"
app:cardCornerRadius="4dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:id="@+id/tv_parentName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"/>

<LinearLayout
android:id="@+id/ll_child_items"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
Loading

0 comments on commit 0ee1055

Please sign in to comment.