Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ExpandableListView using recyclerview. That also can have multiple adapters based on groups
ExpandableListAdapter.java |
---------------------------------------
import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import com.zeephr.one.R;
import com.zeephr.one.models.HeaderName;
import com.zeephr.one.models.PatientMedication;
import com.zeephr.one.models.PatientProblem;
import java.util.ArrayList;
import java.util.List;
/**
* Created by SYSTEM on 24-09-2015.
*/
public class ExpandableListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public static final int HEADER = 0;
public static final int CHILD = 1;
public static final int CHILD_MEDICATION = 1;
public static final int CHILD_PROBLEM = 2;
private List<Item> data;
public ExpandableListAdapter(List<Item> data) {
this.data = data;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int type) {
View view = null;
LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Context context = parent.getContext();
float dp = context.getResources().getDisplayMetrics().density;
int subItemPaddingLeft = (int) (18 * dp);
int subItemPaddingTopAndBottom = (int) (5 * dp);
switch (type) {
case HEADER:
view = inflater.inflate(R.layout.list_header, parent, false);
ListHeaderViewHolder header = new ListHeaderViewHolder(view);
return header;
case CHILD_MEDICATION:
view = inflater.inflate(R.layout.adpt_patient_medication, parent, false);
ViewHolderMedication childMedication = new ViewHolderMedication(view);
return childMedication;
case CHILD_PROBLEM:
view = inflater.inflate(R.layout.adpt_patient_problem, parent, false);
ViewHolderProblem childProblem = new ViewHolderProblem(view);
return childProblem;
}
return null;
}
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
final Item item = data.get(position);
switch (item.type) {
case HEADER:
final ListHeaderViewHolder itemController = (ListHeaderViewHolder) holder;
itemController.refferalItem = item;
//itemController.header_title.setText(item.text);
if (data.get(position).text instanceof HeaderName) {
HeaderName lclObj= (HeaderName) data.get(position).text;
itemController.header_title.setText(lclObj.getmName());
}
if (item.invisibleChildren == null) {
itemController.btn_expand_toggle.setImageResource(R.drawable.ic_launcher);
} else {
itemController.btn_expand_toggle.setImageResource(R.drawable.ic_launcher);
}
itemController.btn_expand_toggle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (item.invisibleChildren == null) {
item.invisibleChildren = new ArrayList<Item>();
int count = 0;
int pos = data.indexOf(itemController.refferalItem);
while (data.size() > pos + 1 && data.get(pos + 1).type == CHILD_MEDICATION ||
data.size() > pos + 1 && data.get(pos + 1).type == CHILD_PROBLEM) {
item.invisibleChildren.add(data.remove(pos + 1));
count++;
}
notifyItemRangeRemoved(pos + 1, count);
itemController.btn_expand_toggle.setImageResource(R.drawable.ic_launcher);
} else {
int pos = data.indexOf(itemController.refferalItem);
int index = pos + 1;
for (Item i : item.invisibleChildren) {
data.add(index, i);
index++;
}
notifyItemRangeInserted(pos + 1, index - pos - 1);
itemController.btn_expand_toggle.setImageResource(R.drawable.ic_launcher);
item.invisibleChildren = null;
}
}
});
break;
case CHILD_MEDICATION:
ViewHolderMedication medicationController = (ViewHolderMedication) holder;
if (data.get(position).text instanceof PatientMedication) {
PatientMedication lclObj= (PatientMedication) data.get(position).text;
medicationController.txtTitleId.setText(lclObj.getMedicationname());
}
break;
case CHILD_PROBLEM:
ViewHolderProblem problemsController = (ViewHolderProblem) holder;
if (data.get(position).text instanceof PatientProblem) {
PatientProblem lclObj= (PatientProblem) data.get(position).text;
problemsController.txtTitleId.setText(lclObj.getProblem_name());
}
break;
}
}
@Override
public int getItemViewType(int position) {
return data.get(position).type;
}
@Override
public int getItemCount() {
return data.size();
}
private static class ListHeaderViewHolder extends RecyclerView.ViewHolder {
public TextView header_title;
public ImageView btn_expand_toggle;
public Item refferalItem;
public ListHeaderViewHolder(View itemView) {
super(itemView);
header_title = (TextView) itemView.findViewById(R.id.header_title);
btn_expand_toggle = (ImageView) itemView.findViewById(R.id.btn_expand_toggle);
}
}
public class ViewHolderMedication extends RecyclerView.ViewHolder {
public TextView txtTitleId, txtModifiedId, txtCauseId, txtFromId, txtToId;
public ImageButton btnEditId, btnDeleteId;
public CardView card_view;
public ViewHolderMedication(View itemLayoutView) {
super(itemLayoutView);
txtTitleId = (TextView) itemLayoutView.findViewById(R.id.txtTitleId);
txtModifiedId = (TextView) itemLayoutView.findViewById(R.id.txtModifiedId);
txtCauseId = (TextView) itemLayoutView.findViewById(R.id.txtCauseId);
txtFromId = (TextView) itemLayoutView.findViewById(R.id.txtFromId);
txtToId = (TextView) itemLayoutView.findViewById(R.id.txtToId);
btnEditId = (ImageButton) itemLayoutView.findViewById(R.id.btnEditId);
btnDeleteId = (ImageButton) itemLayoutView.findViewById(R.id.btnDeleteId);
card_view = (CardView) itemLayoutView.findViewById(R.id.card_view);
}
}
public class ViewHolderProblem extends RecyclerView.ViewHolder {
public TextView txtTitleId,txtIsActiveId,txtFromId,txtToId;
public ImageButton btnEditId,btnDeleteId;
public CardView card_view;
public ViewHolderProblem(View itemLayoutView) {
super(itemLayoutView);
txtTitleId = (TextView) itemLayoutView.findViewById(R.id.txtTitleId);
txtIsActiveId = (TextView) itemLayoutView.findViewById(R.id.txtIsActiveId);
txtFromId = (TextView) itemLayoutView.findViewById(R.id.txtFromId);
txtToId = (TextView) itemLayoutView.findViewById(R.id.txtToId);
btnEditId = (ImageButton) itemLayoutView.findViewById(R.id.btnEditId);
btnDeleteId = (ImageButton) itemLayoutView.findViewById(R.id.btnDeleteId);
card_view = (CardView) itemLayoutView.findViewById(R.id.card_view);
}
}
public static class Item {
public int type;
public Object text;
public List<Item> invisibleChildren;
public Item() {
}
public Item(int type, Object text) {
this.type = type;
this.text = text;
}
}
}
-----------------------------------------------------------------------------------------------------------------------------
FrgPatientFamilyFather.java |
---------------------------------------
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.zeephr.one.R;
import com.zeephr.one.adapters.ExpandableListAdapter;
import com.zeephr.one.models.HeaderName;
import com.zeephr.one.models.PatientMedication;
import com.zeephr.one.models.PatientProblem;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
/**
* Created by SYSTEM on 24-09-2015.
*/
public class FrgPatientFamilyFather extends Fragment {
private RecyclerView recyclerview;
public FrgPatientFamilyFather() {
}
public static FrgPatientFamilyFather newInstance() {
FrgPatientFamilyFather fragment = new FrgPatientFamilyFather();
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frg_patient_family_father, container, false);
return view;
}
@Override
public void onResume() {
super.onResume();
//((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(getString(R.string.screen_name_target));
recyclerview = (RecyclerView) getActivity().findViewById(R.id.recyclerview);
recyclerview.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
List<ExpandableListAdapter.Item> data = new ArrayList<>();
Object obj = null;
HeaderName mName;
mName=new HeaderName();
mName.setmName("Medication");
obj=new Object();
obj=mName;
data.add(new ExpandableListAdapter.Item(ExpandableListAdapter.HEADER, obj));
for(int i=0;i<2;i++){
obj=new Object();
PatientMedication mediCationObj=new PatientMedication();
mediCationObj.setMedicationname("Medication-"+i);
obj=mediCationObj;
data.add(new ExpandableListAdapter.Item(ExpandableListAdapter.CHILD_MEDICATION, obj));
}
mName=new HeaderName();
mName.setmName("Problems");
obj=new Object();
obj=mName;
data.add(new ExpandableListAdapter.Item(ExpandableListAdapter.HEADER, obj));
for(int i=0;i<2;i++){
obj=new Object();
PatientProblem problemObj=new PatientProblem();
problemObj.setProblem_name("Problems-" + i);
obj=problemObj;
data.add(new ExpandableListAdapter.Item(ExpandableListAdapter.CHILD_PROBLEM, obj));
}
recyclerview.setAdapter(new ExpandableListAdapter(data));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment