I have a custom ExpandableList.
list_group.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp"
android:background="#drawable/rounded_corner_layout">
<TextView
android:id="#+id/lblListHeader"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
android:textSize="17dp"
android:textColor="#ffffff" />
</LinearLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="65dip"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="315dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_marginTop="0dp"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/imageView2"
android:layout_width="290dp"
android:layout_height="50dp"
android:layout_marginLeft="12dp"
android:src="#drawable/buton" />
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/imageView1" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/lblListItem"
android:layout_width="189dp"
android:layout_height="30dp"
android:layout_marginBottom="-5dp"
android:layout_marginLeft="10dp"
android:paddingTop="5dp"
android:textColor="#000000"
android:textSize="17dip" />
</LinearLayout>
</RelativeLayout>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="0dp"
android:layout_marginLeft="10dp"
android:text="#string/descripcion"
android:textColor="#color/d_gray" />
</LinearLayout>
</RelativeLayout>
<ImageView
android:id="#+id/imageView1"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignLeft="#+id/imageView2"
android:layout_centerVertical="true"
android:src="#drawable/pixel" />
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
ListAdapter.java
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
#Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
#Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
#Override
public int getGroupCount() {
return this._listDataHeader.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
itm.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
>
<ImageView
android:id="#+id/icon"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/title_item"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toRightOf="#id/icon"
android:textColor="#FAFAFA"
android:gravity="center_vertical"
android:paddingRight="40dp"
android:paddingTop="15dp"
android:paddingBottom="15dp"/>
</RelativeLayout>
MainActivity.java
public class MainActivity extends Fragment {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//.....
ListView listView =
(ListView) root.findViewById(R.id.myListView);
//......
// get the listview
expListView = (ExpandableListView) root.findViewById(R.id.lvExp);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(getActivity(), listDataHeader,
listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
// Listview Group click listener
expListView.setOnGroupClickListener(new OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// Toast.makeText(getApplicationContext(),
// "Group Clicked " + listDataHeader.get(groupPosition),
// Toast.LENGTH_SHORT).show();
return false;
}
});
// Listview Group expanded listener
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
/*
Toast.makeText(getActivity().getApplicationContext(),
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();
*/
}
});
// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int groupPosition) {
/*
Toast.makeText(getActivity().getApplicationContext(),
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();
*/
}
});
// Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// toast
/*Toast.makeText( getActivity().getApplicationContext(),
listDataHeader.get(groupPosition) + " : " +
listDataChild.get( listDataHeader.get(groupPosition)).get(childPosition), Toast.LENGTH_SHORT).show();*/
// fin toast
return false;
}
});
return root;
}
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("Group1");
listDataHeader.add("Group2");
listDataHeader.add("Group3");
// Adding child data
List<String> G1 = new ArrayList<String>();
G1.add("Café con leche.... 1.20€");
G1.add("Manchado.... 1.20€");
G1.add("Cortado.... 1.20€");
List<String> G2 = new ArrayList<String>();
G2.add("item 1 Group 2");
G2.add("item 2 Group 2");
G2.add("item 3 Group 2");
List<String> G3 = new ArrayList<String>();
G3.add("item 1 Group 3");
G3.add("item 2 Group 3");
G3.add("item 3 Group 3");
listDataChild.put(listDataHeader.get(0), G1); // Header, Child data
listDataChild.put(listDataHeader.get(1), G2);
listDataChild.put(listDataHeader.get(2), G3);
}
}
okay, that it works perfect.
My problem!!
I need two TextView on Child items, I can't implement the second Textview, I don't know how I can add the second TextView on Adapter and later populate manually with a String.
I need do that.
Group item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="8dp"
android:background="#drawable/rounded_corner_layout">
<TextView
android:id="#+id/lblListHeader"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
android:textSize="17dp"
android:textColor="#ffffff" />
<TextView
android:id="#+id/tv2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
android:textSize="17dp"
android:textColor="#ffffff" />
</LinearLayout>
Notice that you should change layout orientation to horizontal!
Adapter:
#Override
public View getGroupView(int groupPosition, boolean isLastChild, View view,
ViewGroup parent) {
LayoutInflater inf = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (view == null) {
view = inf.inflate(R.layout.list_group, null);
}
TextView heading = (TextView) view.findViewById(R.id.lblListHeader);
TextView textView2 = (TextView) view.findViewById(R.id.tv2);
return view;
}
Here you can set text of both text views and set click listeners
In code:
lista.setAdapter(listAdapter);
listAdapter.notifyDataSetChanged();
You can create 2 objects (group and child) and use it in adapter.
public class Child{
String field1;
String field2;
}
public class Group{
String groupName;
ArrayList<Child> children;
}
public class Adapter extensextends BaseExpandableListAdapter {
ArrayList<Group> groups;
...
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
...
groupName = groups.get(groupPosition).getGroupName();
...
return convertView;
}
...
#Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
...
chieldField = groups.get(groupPosition).getChildren().get(childPosition).getField();
...
return convertView;
}
...
Related
I am able to retrieve data from SQLite database(to be precise 3 columns) and display them in Expandable Listview as Child(s). But I want to display them as:
Due date: 10/23/2016
late sub: YES
Penalty: 10%
Above, Left side text, I am displaying from Textview text(Child xml) and right side text after the colon, I am retrieving from database.
How do I handle this in getChildView method in my Adapter?
Here is my Adapter class code:
public class AssignmentDisplayAdapter extends BaseExpandableListAdapter{
private Context mContext;
private List<String> expandableListTitle;
private HashMap<String, List<CourseDisplay>> expandableListDetail;
private LayoutInflater mInflater;
public AssignmentDisplayAdapter(Context context, List<String> expandableListTitle,
HashMap<String, List<CourseDisplay>> expandableListDetail) {
this.mContext = context;
this.expandableListTitle = expandableListTitle;
this.expandableListDetail = expandableListDetail;
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public Object getChild(int listPosition, int expandedListPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.get(expandedListPosition);
}
#Override
public long getChildId(int listPosition, int expandedListPosition) {
return expandedListPosition;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public View getChildView(int listPosition, final int expandedListPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String expandedListText = (String) getChild(listPosition, expandedListPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.custom_listview_expandable_child, null);
}
TextView expandedListTextView = (TextView) convertView
.findViewById(R.id.assign_duedate_display);
expandedListTextView.setText(expandedListText);
return convertView;
}
#Override
public int getChildrenCount(int listPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.size();
}
#Override
public Object getGroup(int listPosition) {
return this.expandableListTitle.get(listPosition);
}
#Override
public int getGroupCount() {
return this.expandableListTitle.size();
}
#Override
public long getGroupId(int listPosition) {
return listPosition;
}
#Override
public View getGroupView(int listPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String listTitle = (String) getGroup(listPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.mContext.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.custom_listview_expandable_parent, null);
}
TextView listTitleTextView = (TextView) convertView
.findViewById(R.id.parent_text);
listTitleTextView.setTypeface(null, Typeface.BOLD);
listTitleTextView.setText(listTitle);
return convertView;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}//end class
Here is my parent xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/parent_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
android:textColor="#color/primary"
android:paddingTop="10dp"
android:paddingBottom="10dp"/>
</LinearLayout>
Here is my child xml code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/assign_duedatetext_display"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Due Date: "
android:layout_weight="3"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
android:paddingTop="10dp"
android:paddingBottom="10dp"/>
<TextView
android:id="#+id/assign_duedate_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_toRightOf="#id/assign_duedatetext_display"
android:layout_marginLeft="16dp"
android:textColor="#color/primary"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
android:paddingTop="10dp"
android:paddingBottom="10dp"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/assign_latesubtext_display"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Late Submission: "
android:layout_weight="3"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
android:paddingTop="10dp"
android:paddingBottom="10dp"/>
<TextView
android:id="#+id/assign_latesub_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_toRightOf="#id/assign_latesubtext_display"
android:layout_marginLeft="16dp"
android:textColor="#color/primary"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
android:paddingTop="10dp"
android:paddingBottom="10dp"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/assign_penaltytext_display"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Penalty: "
android:layout_weight="3"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
android:paddingTop="10dp"
android:paddingBottom="10dp"/>
<TextView
android:id="#+id/assign_penalty_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_toRightOf="#id/assign_penaltytext_display"
android:layout_marginLeft="16dp"
android:textColor="#color/primary"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
android:paddingTop="10dp"
android:paddingBottom="10dp" />
</RelativeLayout>
</LinearLayout>
I believe something needs to be done in getChildView method but not sure how to handle this. Please guide me through this.
I am able to achieve this dynamically as I know the data format in the database is going to be the same for each parent.
Here is updated getChildView() Method:
#Override
public View getChildView(int listPosition, final int expandedListPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
String expandedListText = (String) getChild(listPosition, expandedListPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.custom_listview_expandable_child, null);
}
TextView expandedListTextView1 = (TextView) convertView
.findViewById(R.id.assign_duedate_display);
TextView expandedListTextView2 = (TextView) convertView
.findViewById(R.id.assign_duedatetext_display);
if (expandedListPosition == 0){
expandedListTextView2.setText("Due date: ");
}else if(expandedListPosition == 1){
expandedListTextView2.setText("late submission: ");
}else {
expandedListTextView2.setText("Penalty: ");
}
expandedListTextView1.setText(expandedListText);
return convertView;
}
EarBoxExpandableListAdapter
public class EarBoxExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public EarBoxExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
#Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.childrow, null);
}
// TextView txtListChild = (TextView) convertView
// .findViewById(R.id.lblListItem);
//
// txtListChild.setText(childText);
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
#Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
#Override
public int getGroupCount() {
return this._listDataHeader.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.parentrow, null);
}
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
Below is my xml layout code for the child row.Evwnthough I kept the width as match parent, in actual output which I attached as screen shot Im getting the wrap content of the child rows.
childrow.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:id="#+id/btnPlay"
android:src="#android:drawable/ic_media_play"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/round_corner_rect"
android:layout_weight="0"
android:layout_margin="10dp" />
<TextView
android:id="#+id/songCurrentDurationLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="left"
android:textColor="#747272"
android:textStyle="bold"
android:visibility="visible"
android:text="3.25"
android:layout_gravity="center_vertical" />
<SeekBar
android:id="#+id/songProgressBar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:layout_weight="1"
android:layout_gravity="center" />
<TextView
android:id="#+id/songTotalDurationLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="right"
android:textColor="#656262"
android:textStyle="bold"
android:visibility="visible"
android:text="42:52"
android:layout_gravity="center_vertical" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">
<Button
style="#style/earBoxbuttonStyle"
android:text="Save"
android:id="#+id/button"
android:layout_weight="1"
android:layout_width="match_parent" />
<Button
style="#style/earBoxbuttonStyle"
android:text="Reply"
android:id="#+id/button3"
android:layout_weight="1"
android:layout_width="match_parent" />
<Button
style="#style/earBoxbuttonStyle"
android:text="Delete"
android:id="#+id/button4"
android:layout_weight="1"
android:layout_width="match_parent" />
</LinearLayout>
</LinearLayout>
MessageInboxFragment
/**
* Created by sethu on 6/13/2016.
*/
public class MessageInboxFragment extends Fragment{
ExpandableListView expandableListView;
EarBoxExpandableListAdapter listAdapter;
static List<String> listDataHeader;
static HashMap<String, List<String>> listDataChild;
private int lastExpandedPosition = -1;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_messages_inbox, container, false);
expandableListView=(ExpandableListView) rootView.findViewById(R.id.expandableListView);
expandableListView.setDividerHeight(1);
// final ArrayList<Parent> dummyList = ExpandableListMain.buildDummyData();
prepareListData();
listAdapter = new EarBoxExpandableListAdapter(getActivity(), listDataHeader, listDataChild);
expandableListView.setAdapter(listAdapter);
expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
if (lastExpandedPosition != -1
&& groupPosition != lastExpandedPosition) {
expandableListView.collapseGroup(lastExpandedPosition);
}
lastExpandedPosition = groupPosition;
}
});
return rootView;
}
public static void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("Top 250");
listDataHeader.add("Now Showing");
listDataHeader.add("Coming Soon..");
// Adding child data
List<String> top250 = new ArrayList<String>();
top250.add("The Shawshank Redemption");
List<String> nowShowing = new ArrayList<String>();
nowShowing.add("The Conjuring");
List<String> comingSoon = new ArrayList<String>();
comingSoon.add("2 Guns");
listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
listDataChild.put(listDataHeader.get(1), nowShowing);
listDataChild.put(listDataHeader.get(2), comingSoon);
}
}
xml screenshot
device output screenshot
I m creating ExpandableList view in Fragment, the Group item display in list, but when I touch the group item, it is not expanding.
Below is my fragment
public class InstituteFragment extends Fragment {
ExpandableListView expandableListView;
ArrayList<String> subjectName;
HashMap<String,ArrayList<String>> topicName;
public InstituteFragment() {
}
public static InstituteFragment newInstance(){
InstituteFragment fragment = new InstituteFragment();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
subjectName = new ArrayList<>();
topicName = new HashMap<>();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_institute, container, false);
prepData();
expandableListView = (ExpandableListView)view.findViewById(R.id.expd_list_view);
SubjectAdapter adapter = new SubjectAdapter(getActivity(),subjectName,topicName);
expandableListView.setAdapter(adapter);
return view;
}
private void prepData() {
subjectName.add("Physics");
subjectName.add("Chemistry");
subjectName.add("Biology");
ArrayList<String> physics = new ArrayList<>();
physics.add("Demo");
physics.add("Demo1");
physics.add("Demo2");
physics.add("Demo3");
ArrayList<String> chemistry = new ArrayList<>();
chemistry.add("Demo");
chemistry.add("Demo1");
chemistry.add("Demo2");
chemistry.add("Demo3");
ArrayList<String> biology = new ArrayList<>();
biology.add("Demo");
biology.add("Demo1");
biology.add("Demo2");
biology.add("Demo3");
topicName.put(subjectName.get(0), physics);
topicName.put(subjectName.get(1),chemistry);
topicName.put(subjectName.get(2),biology);
}
}
below is adapter
public class SubjectAdapter extends BaseExpandableListAdapter {
Context context;
ArrayList<String> subjectName;
HashMap<String,ArrayList<String>> topicName;
public SubjectAdapter(Context context, ArrayList<String> subjectName, HashMap<String, ArrayList<String>> topicName) {
this.context = context;
this.topicName = topicName;
this.subjectName = subjectName;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return this.topicName.get(this.subjectName.get(groupPosition))
.get(childPosition);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.chapter_name, null);
TextView txtListChild = (TextView) convertView
.findViewById(R.id.chapter_name);
txtListChild.setText(childText);
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return this.topicName.get(this.subjectName.get(groupPosition)).size();
}
#Override
public int getGroupCount() {
return this.subjectName.size();
}
#Override
public Object getGroup(int groupPosition) {
return this.subjectName.get(groupPosition);
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String subjectName = (String)getGroup(groupPosition);
LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.subject_name,null);
TextView textView = (TextView)convertView.findViewById(R.id.subject_name);
textView.setText(subjectName);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
below is institute_fragment.xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragment.InstituteFragment">
<ExpandableListView
android:id="#+id/expd_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ExpandableListView>
</RelativeLayout>
below is my groupview
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/_10sdp"
android:layout_margin="#dimen/_5sdp"
android:background="#android:color/white"
android:id="#+id/subject_name_cardview">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/subject_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Subject Name"
android:textAppearance="?android:textAppearanceMedium"
android:padding="#dimen/_10sdp"
android:layout_marginTop="#dimen/_10sdp"
android:layout_marginBottom="#dimen/_10sdp"
android:layout_marginLeft="#dimen/_10sdp"
android:textColor="#android:color/black" />
<ImageButton
android:id="#+id/subject_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_keyboard_arrow_down_24dp"
android:background="#00000000"
android:layout_marginRight="#dimen/_20sdp"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
below is childView xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/_5sdp"
android:background="#color/colorPrimary"
android:id="#+id/chapter_name_cardview">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/chapter_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Subject Name"
android:textAppearance="?android:textAppearanceSmall"
android:padding="#dimen/_10sdp"
android:layout_marginTop="#dimen/_5sdp"
android:layout_marginBottom="#dimen/_5sdp"
android:layout_marginLeft="#dimen/_10sdp"
android:textColor="#android:color/black" />
<ImageButton
android:id="#+id/chapter_name_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_keyboard_arrow_right_24dp"
android:background="#00000000"
android:layout_marginRight="#dimen/_20sdp"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
Well, easy one.
You have imagebutton in groupview that is intercepting touch event. Add these parameters:
android:focusable="false"
android:focusableInTouchMode="false"
And do same for childview. You have exactly the same case there.
P.S. why are you using imagebuttons anyway? you could use imageviews instead
You need to do this one.
expandableListView = (ExpandableListView)view.findViewById(R.id.expd_list_view);
SubjectAdapter adapter = new SubjectAdapter(getActivity(),subjectName,topicName);
expandableListView.setAdapter(adapter);
expandableListView .setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView expandableListView, View view, int groupPosition, long l) {
expandableListView.expandGroup(groupPosition);
return false;
}
});
Hope this will help you.
Try to pass your ExpandableListView in the adapter:
SubjectAdapter adapter = new SubjectAdapter(getActivity(),subjectName,topicName, expandableListView);
Then youe adapter should look like:
...
ExpandableListView expandableListView;
public SubjectAdapter(Context context, ArrayList<String> subjectName, HashMap<String, ArrayList<String>> topicName, ExpandableListView expandableListView) {
this.context = context;
this.topicName = topicName;
this.subjectName = subjectName;
this.expandableListView = expandableListView;
}
In your group view add following:
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
expandableListView.expandGroup(groupPosition);
}
});
In My expandable list view first child item has two buttons.button1 and button2.in other child list item has check box and text.these check boxes are default disabled
in first time user expand the child list and after press button1 i want to enable all check boxes in particular group
As this point i can only enable first child list item checkbox.how can enable all check boxes by clicking button on the 1st child item.
Expandable list adapter
public class ExpandListAdapter extends BaseExpandableListAdapter {
private Context context;
private ArrayList<ExpandableListGroup> groups;
public ExpandListAdapter(Context context, ArrayList<ExpandableListGroup> groups) {
this.context = context;
this.groups = groups;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
ArrayList<ExpandableListChild> chList = groups.get(groupPosition).getItems();
return chList.get(childPosition);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final ExpandableListChild child = (ExpandableListChild) getChild(groupPosition, childPosition);
final ViewHolder holder;
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.btn_start_inspection = (Button) convertView.findViewById(R.id.btn_start_inspection);
holder.btn_complete_inspection = (Button) convertView.findViewById(R.id.btn_complete_inspection);
holder.cbx_complete_inspection = (CheckBox) convertView.findViewById(R.id.check_sub_task);
holder.txt_activity_name = (TextView) convertView.findViewById(R.id.txt_sub_task_title);
holder.txt_activity_name.setText(child.getName().toString());
//button for enable checkboxes
holder.btn_start_inspection.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.btn_complete_inspection.setBackgroundColor(Color.RED);
holder.itemEnabled = true;
notifyDataSetChanged();
}
});
//enable check boxes
if(holder.itemEnabled == true){
holder.cbx_complete_inspection.setEnabled(true);
}else{
holder.cbx_complete_inspection.setEnabled(false);
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
ArrayList<ExpandableListChild> chList = groups.get(groupPosition).getItems();
return chList.size();
}
#Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
#Override
public int getGroupCount() {
return groups.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
ExpandableListGroup group = (ExpandableListGroup) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater inf = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.list_group, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.txt_company_name);
tv.setText(group.getName());
TextView tvAddress = (TextView) convertView.findViewById(R.id.txt_company_address);
tvAddress.setText(group.getAddressLevel());
iv = (ImageView) convertView.findViewById(R.id.img_expand);
return convertView;
}
#Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
iv.setImageDrawable(context.getResources().getDrawable(R.drawable.exp_fold));
}
#Override
public void onGroupCollapsed(int groupPosition) {
super.onGroupCollapsed(groupPosition);
iv.setImageDrawable(context.getResources().getDrawable(R.drawable.exp_min));
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
static class ViewHolder {
Button btn_complete_inspection;
Button btn_start_inspection;
CheckBox cbx_complete_inspection;
TextView txt_activity_name;
boolean itemEnabled = false;
}
}
listchildItem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/lnrChild"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/lnr_task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="10dp">
<Button
android:id="#+id/btn_start_inspection"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_gravity="left"
android:layout_marginLeft="10dp"
android:background="#drawable/rounded_cornerexpandable_list_child"
android:gravity="center"
android:text="Start Inspection"
android:textAllCaps="false"
android:textColor="#color/color_white"
android:textSize="12sp"
></Button>
<Button
android:id="#+id/btn_complete_inspection"
android:layout_width="150dp"
android:layout_height="35dp"
android:layout_marginLeft="150dp"
android:src="#drawable/rounded_cornerexpandable_list_child_gray"
android:text="Complete Inspection"
android:textAllCaps="false"
android:textColor="#color/color_white"
android:textSize="12sp"
></Button>
<LinearLayout
android:id="#+id/lnr_active_sub_task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/btn_complete_inspection">
<LinearLayout
android:id="#+id/lnr_sub_task"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:gravity="center"
android:orientation="horizontal">
<CheckBox
android:id="#+id/check_sub_task"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:checked="false"
android:gravity="center"></CheckBox>
</LinearLayout>
<LinearLayout
android:id="#+id/textHolder"
android:layout_width="0.0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:gravity="left"
android:orientation="horizontal">
<TextView
android:id="#+id/txt_sub_task_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="exx"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:layout_width="0.0dp"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_weight="0.5"
android:gravity="right"
android:orientation="horizontal">
<ImageView
android:id="#+id/txt_sub_task_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_keyboard_arrow_right_black_24dp"
android:gravity="center"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
What I see is that after click button on 1st list item only 1st check box in the 1st child item is enabled, the other list item check boxes are not. What I want is that checkboxes in the child item rows are enabled. (extra lines added from comments)
my layout contain expandable listview and some ImageView. It's works fine but my expandablelistview is not open on click:-
here my xml:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/container_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
</LinearLayout>
<ExpandableListView
android:id="#+id/list_track_list_activity_sub_category"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:cacheColorHint="#00000000"/>
<ImageView
android:id="#+id/sub_banners1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"/>
<ImageView
android:id="#+id/sub_banners2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"/>
<ImageView
android:id="#+id/sub_banners3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"/>
<FrameLayout
android:id="#+id/container_body"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</ScrollView>
<fragment
android:id="#+id/fragment_navigation_drawer"
android:name="shoppingmazza.android.catalyst.com.shoppingmazza.activity.FragmentDrawer"
android:layout_width="#dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
tools:layout="#layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
Here my java:-
expListView = (ExpandableListView) findViewById(R.id.list_track_list_activity_sub_category);
listAdapter = new ExpandableListAdapterForMenu(this, listDataHeader, listDataChild);
expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
// Listview on child click listener
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
for (Map.Entry<Integer, String> entry : hmap.entrySet()) {
if (entry.getValue().equals(listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition))) {
sendMenu_id = String.valueOf(entry.getKey());
break;
}
}
Intent i = new Intent(SubCategory.this, ProductsCategory.class);
i.putExtra("category", sendMenu_id);
startActivity(i);
return false;
}
});
expListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
}
});
expListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int groupPosition) {
}
});
and My Adapter class:-
public class ExpandableListAdapterForMenu extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public ExpandableListAdapterForMenu(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
#Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
String content = childText;
content = content.replace("'", "");
content = content.replace("&","&");
txtListChild.setText(content);
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
#Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
#Override
public int getGroupCount() {
return this._listDataHeader.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
String content = headerTitle;
content = content.replace("'", "");
content = content.replace("&","&");
lblListHeader.setText(content);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
In my java code i change my imageview heigth programmatically.
Anyone tell me how can i solved this problem.Thanks is advance!
inside onGroupExpand add expListView.expandGroup(groupPosition);
and inside onGroupCollapse add expListView.collapseGroup(groupPosition);