|
1 | 1 | package com.rohitss.dynamicrecycler;
|
2 | 2 |
|
3 |
| -import android.support.v7.app.AppCompatActivity; |
| 3 | +import android.content.Context; |
4 | 4 | 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; |
5 | 18 |
|
6 | 19 | public class DynamicRecyclerActivity extends AppCompatActivity {
|
| 20 | + private RecyclerView mRecyclerView; |
| 21 | + private Context mContext; |
| 22 | + |
7 | 23 | @Override
|
8 | 24 | protected void onCreate(Bundle savedInstanceState) {
|
9 | 25 | super.onCreate(savedInstanceState);
|
10 | 26 | 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 | + } |
11 | 184 | }
|
12 | 185 | }
|
0 commit comments