Tabhost creating trouble in getChildlView method of ExpandableList - android

I have put tabs insilde a expandablelistview adapter and due to its getCHildView method tabs are generating multiple times in on parent click. How to resolve this .
This is an example of what I need to implement.
http://i.stack.imgur.com/rvPZe.png
In left side image there is list of expandable view to be shown to user. Once user click on any of the expandable view i need to display tabs inside childlistview of expandable list view as shown in right image.
Please see below the entire code
My Activity Class
public class CardList extends AppCompatActivity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_card_list);
// get the listview
expListView = (ExpandableListView) findViewById(R.id.lvExp);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(
getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});
// Listview Group expanded listener
expListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();
}
});
expListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();
}
});
}
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
listDataHeader.add("Top 250");
listDataHeader.add("Now Showing");
listDataHeader.add("Coming Soon..");
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);
}
}
Activity 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="fill_parent"
android:orientation="vertical"
android:background="#f4f4f4" >
<ExpandableListView
android:id="#+id/lvExp"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
</LinearLayout>
ExpandableListAdapter
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private Button b;
LinearLayout l;
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(final 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);
}
final View views = convertView;
final ViewGroup parents =parent;
TabHost host = (TabHost)convertView.findViewById(R.id.tabHost);
host.setup();
TabHost.TabSpec spec = host.newTabSpec("Tab One");
spec.setContent(R.id.tab1);
spec.setIndicator("Tab One");
host.addTab(spec);
TabHost.TabSpec spec1 = host.newTabSpec("Tab Two");
spec1.setContent(R.id.tab2);
spec1.setIndicator("Tab Two");
host.addTab(spec1);
TabHost.TabSpec spec2 = host.newTabSpec("Tab Three");
spec2.setContent(R.id.tab3);
spec2.setIndicator("Tab Three");
host.addTab(spec2);
host.setFocusable(false);
host.setup();
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(final 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);
LinearLayout.LayoutParams firstParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 300);
//Random rnd = new Random();
int color = Color.argb(255, 104, 224, 201);
convertView = infalInflater.inflate(R.layout.list_group, null);
convertView.setLayoutParams(firstParam);
convertView.setBackgroundColor(color);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
ImageButton img = (ImageButton)convertView.findViewById(R.id.imageButton) ;
img.setFocusable(false);
System.out.println("headerTitle 123-------------------> "+headerTitle);
img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("headerTitle -------------------> "+(String) getGroup(groupPosition));
Intent i = new Intent(_context,DashboardActivity.class);
i.putExtra("feroz",(String) getGroup(groupPosition));
_context.startActivity(i);
}
});
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;
}
private Button addTextView(){
Button Button = new Button(_context);
Button.setText("submit");
return Button;
}
}
list_group.xml for parent view
<?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="100dp"
android:orientation="horizontal"
android:padding="8dp"
android:background="#000000"
android:weightSum="100"
> <TextView
android:id="#+id/lblListHeader"
android:layout_width="0dp"
android:layout_height="match_parent"
android:textSize="17dp"
android:text="hello "
android:textColor="#fff"
android:layout_weight="60"
/>
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="#+id/imageView"
android:src="#drawable/line"
android:layout_weight="10"/>
<ImageButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="#+id/imageButton"
android:layout_weight="30"
android:src="#drawable/launch"
android:background="#00000000"
android:clickable="true" />
</LinearLayout>
list_item for child view
<?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="100dip"
android:orientation="horizontal"
android:weightSum="100">
<TabHost
android:id="#+id/tabHost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"></TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffc916"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This is tab 1" />
</LinearLayout>
<LinearLayout
android:id="#+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#da8200"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This is tab 2" />
</LinearLayout>
<LinearLayout
android:id="#+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#5b89ff"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This is tab 3" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>

After doing little amount of research I got to know that the problem is with convert view which you are reusing for the child once it gets created once. But not for the TabHost. You are creating the TabHost every time even if the view is already gets created and also tabhost setup is also done for the same child.I have just edited adapter class and for research one method prepareListData(). SO here is the edited code:-
MyExpandableListAdapter.java
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private Button b;
LinearLayout l;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public MyExpandableListAdapter(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(final 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_child, null);
TabHost host = (TabHost) convertView.findViewById(R.id.tabHost);
host.setup();
Log.e("tab count",""+host.getTabWidget().getTabCount());
TabHost.TabSpec spec = host.newTabSpec("Tab One");
spec.setContent(R.id.tab1);
spec.setIndicator("Tab One");
host.addTab(spec);
TabHost.TabSpec spec1 = host.newTabSpec("Tab Two");
spec1.setContent(R.id.tab2);
spec1.setIndicator("Tab Two");
host.addTab(spec1);
TabHost.TabSpec spec2 = host.newTabSpec("Tab Three");
spec2.setContent(R.id.tab3);
spec2.setIndicator("Tab Three");
host.addTab(spec2);
host.setFocusable(false);
host.setup();
}
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(final 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);
LinearLayout.LayoutParams firstParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 300);
//Random rnd = new Random();
int color = Color.argb(255, 104, 224, 201);
convertView = infalInflater.inflate(R.layout.list_group, null);
convertView.setLayoutParams(firstParam);
convertView.setBackgroundColor(color);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
ImageButton img = (ImageButton)convertView.findViewById(R.id.imageButton) ;
img.setFocusable(false);
System.out.println("headerTitle 123-------------------> "+headerTitle);
img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("headerTitle -------------------> "+(String) getGroup(groupPosition));
Intent i = new Intent(_context,DashboardActivity.class);
i.putExtra("feroz",(String) getGroup(groupPosition));
_context.startActivity(i);
}
});
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;
}
private Button addTextView(){
Button Button = new Button(_context);
Button.setText("submit");
return Button;
}
}
prepareListData() method
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
listDataHeader.add("Top 250");
listDataHeader.add("Now Showing");
listDataHeader.add("Coming Soon..");
listDataHeader.add("Item 4..");
listDataHeader.add("Item 5..");
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);
listDataChild.put(listDataHeader.get(3), new ArrayList<String>());
listDataChild.put(listDataHeader.get(4), comingSoon);
}

Related

Child view in Expandable list view get Shrinks automatically to wrap content, Even though I mentioned the View to match parent

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

how to set the value at particular position in expandableListView

i have created a ExpandableListView.But There is a problem when i set the value of particular item same value set on all item i can not understand how to solve this.
this is my code.
direct_referal.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutfaq"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="25dp" >
<ExpandableListView
android:id="#+id/lvExp"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:padding="10dp"
android:cacheColorHint="#00000000"/>
</LinearLayout>
This is My Main Xml File.
DirectReferal.java
public class DirectReferalFragment extends Fragment
{
ExpandableDirectReferalListAdapter listAdapter;
ExpandableListView expListView;
private List<String> userID;
private List<String> joiningDate;
private HashMap<String, List<String>> slNo;
private HashMap<String, List<String>> name;
private HashMap<String, List<String>> city;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.direct_referal, container, false);
// get the listview
expListView = (ExpandableListView) view.findViewById(R.id.lvExp);
expListView.setGroupIndicator(null);
// preparing list data
prepareListData();
listAdapter = new ExpandableDirectReferalListAdapter(this.getActivity(), userID, joiningDate, slNo, name, city);
// 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) {
return false;
}
});
// Listview Group expanded listener
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
// TODO Auto-generated method stub
}
});
// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int groupPosition) {
// TODO Auto-generated method stub
}
});
// Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
return false;
}
});
return view;
}
/*
* Preparing the list data
*/
private void prepareListData() {
userID = new ArrayList<String>();
joiningDate = new ArrayList<String>();
slNo = new HashMap<String, List<String>>();
name = new HashMap<String, List<String>>();
city = new HashMap<String, List<String>>();
// Adding USER ID
userID.add("User ID : GLI000014");
userID.add("User ID : GLI000015");
userID.add("User ID : GLI000016");
userID.add("User ID : GLI000017");
userID.add("User ID : GLI000018");
// Adding JOINING DATE
joiningDate.add("Joining Date : 9 Nov,2015");
joiningDate.add("Joining Date : 9 Dec,2015");
joiningDate.add("Joining Date : 9 Jan,2016");
joiningDate.add("Joining Date : 9 Fab,2016");
joiningDate.add("Joining Date : 9 Mar,2016");
// Adding SL NO
List<String> no1 = new ArrayList<String>();
no1.add("SL No. : 1");
List<String> no2 = new ArrayList<String>();
no2.add("SL No. : 2");
List<String> no3 = new ArrayList<String>();
no3.add("SL No. : 3");
List<String> no4 = new ArrayList<String>();
no4.add("SL No. : 4");
List<String> no5 = new ArrayList<String>();
no5.add("SL No. : 5");
slNo.put(userID.get(0), no1); // Header, Child data
slNo.put(userID.get(1), no2);
slNo.put(userID.get(2), no3);
slNo.put(userID.get(3), no4);
slNo.put(userID.get(4), no5);
// Adding NAME
List<String> name1 = new ArrayList<String>();
name1.add("Name : Prabal Saxena");
List<String> name2 = new ArrayList<String>();
name2.add("Name : Chirag Mehta");
List<String> name3 = new ArrayList<String>();
name3.add("Name : Tarun Mehta");
List<String> name4 = new ArrayList<String>();
name4.add("Name : Prashant Gupta");
List<String> name5 = new ArrayList<String>();
name5.add("Name : Chotu");
name.put(userID.get(0), name1); // Header, Child data
name.put(userID.get(1), name2);
name.put(userID.get(2), name3);
name.put(userID.get(3), name4);
name.put(userID.get(4), name5);
// Adding CITY
List<String> city1 = new ArrayList<String>();
city1.add("City : Noida");
List<String> city2 = new ArrayList<String>();
city2.add("City : Gurgaun");
List<String> city3 = new ArrayList<String>();
city3.add("City : Delhi");
List<String> city4 = new ArrayList<String>();
city4.add("City : Pune");
List<String> city5 = new ArrayList<String>();
city5.add("SL No. : Delhi");
city.put(userID.get(0), city1); // Header, Child data
city.put(userID.get(1), city2);
city.put(userID.get(2), city3);
city.put(userID.get(3), city4);
city.put(userID.get(4), city5);
}
}
This is my main Java file. in this we set the value of all child and parent element and send the value via constructor.
list_item.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:background="#drawable/direct_referal_bg"
android:orientation="vertical" >
<TextView
android:id="#+id/lblListItemslno"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:gravity="left"
android:textColor="#000000" />
<TextView
android:id="#+id/lblListItemname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:gravity="left"
android:textColor="#000000" />
<TextView
android:id="#+id/lblListItemcity"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:gravity="left"
android:textColor="#000000" />
</LinearLayout>
list_group.xml
<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="#333333">
<TextView
android:id="#+id/lblListHeaderuserid"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="User ID : GLI000014"
android:textSize="12sp"
android:gravity="center"
android:textColor="#FFFFFF" />
<TextView
android:id="#+id/lblListHeaderjoindate"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="Joining Date : 9 Nov, 2015"
android:textSize="12sp"
android:gravity="center"
android:textColor="#FFFFFF" />
</LinearLayout>
ExpandableDirectReferalListAdapter.java
public class ExpandableDirectReferalListAdapter extends BaseExpandableListAdapter {
private Context _context;
List<String> _userID;
List<String> _joiningDate;
HashMap<String, List<String>> _slNo;
HashMap<String, List<String>> _name;
HashMap<String, List<String>> _city;
public ExpandableDirectReferalListAdapter(Context context, List<String> userID,List<String> joiningDate,
HashMap<String, List<String>> slNo,HashMap<String, List<String>> name,
HashMap<String, List<String>> city) {
this._context = context;
this._userID = userID;
this._joiningDate = joiningDate;
this._slNo = slNo;
this._name = name;
this._city = city;
}
#Override
public Object getChild(int groupPosition, int childPosititon) {
return this._slNo.get(this._userID.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 txtListChildslno = (TextView) convertView.findViewById(R.id.lblListItemslno);
TextView txtListChildname = (TextView) convertView.findViewById(R.id.lblListItemname);
TextView txtListChildcity = (TextView) convertView.findViewById(R.id.lblListItemcity);
txtListChildslno.setText(childText);
txtListChildname.setText(childText);
txtListChildcity.setText(childText);
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return this._slNo.get(this._userID.get(groupPosition))
.size();
}
#Override
public Object getGroup(int groupPosition) {
return this._userID.get(groupPosition);
}
#Override
public int getGroupCount() {
return this._userID.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 lblListHeaderuserid = (TextView) convertView.findViewById(R.id.lblListHeaderuserid);
TextView lblListHeaderjoindate = (TextView) convertView.findViewById(R.id.lblListHeaderjoindate);
lblListHeaderuserid.setText(headerTitle);
lblListHeaderjoindate.setText(headerTitle);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
This is my Adapter class.in this we want to set diffrent value of all child and parent field but same value set on all field.
you can see the the output in this image..
enter image description here
How can i solve this problem, I can not understant.
please help.
Please correct getChildView method gand etGroupView method. The code will be as follows.
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
String slNo = _slNo.get(_userID.get(groupPosition)).get(0);
String childName = _name.get(_userID.get(groupPosition)).get(0);
String childCity = _city.get(_userID.get(groupPosition)).get(0);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChildslno = (TextView) convertView.findViewById(R.id.lblListItemslno);
TextView txtListChildname = (TextView) convertView.findViewById(R.id.lblListItemname);
TextView txtListChildcity = (TextView) convertView.findViewById(R.id.lblListItemcity);
txtListChildslno.setText(slNo);
txtListChildname.setText(childName);
txtListChildcity.setText(childCity);
return convertView;
}
#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 lblListHeaderuserid = (TextView) convertView.findViewById(R.id.lblListHeaderuserid);
TextView lblListHeaderjoindate = (TextView) convertView.findViewById(R.id.lblListHeaderjoindate);
lblListHeaderuserid.setText(headerTitle);
lblListHeaderjoindate.setText(_joiningDate.get(groupPosition));
return convertView;
}
Hope that you can understand what was your mistake..
I tested the code, it will work.

how to add expandable listview inside material design navigation drawer in android?

I want to add expandable listview inside material design navigation drawer in android? like this application https://play.google.com/store/apps/details?id=com.rarlab.rar
ExpandablelistViewActivity.java:
/**********Expandable Listview Inside Navigation Drawer************/
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.FrameLayout;
import android.widget.Toast;
public class ExpandablelistViewActivity extends Activity {
private DrawerLayout mDrawerLayout;
FrameLayout actContent;
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_expandablelist_view);
/************* ExpandableListView **********/
expListView = (ExpandableListView) findViewById(R.id.lvExp);
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
prepareListData();
listAdapter = new ExpandableListAdapter(
ExpandablelistViewActivity.this, listDataHeader, listDataChild);
expListView.setAdapter(listAdapter);
//expandAll();
// Listview Group click listener
expListView.setOnGroupClickListener(new OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// mDrawerLayout.closeDrawers();
// 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) {
// mDrawerLayout.closeDrawers();
/*Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();*/
}
});
// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int groupPosition) {
mDrawerLayout.closeDrawers();
Toast.makeText(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) {
mDrawerLayout.closeDrawers();
// TODO Auto-generated method stub
Toast.makeText(
getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});
}
// method to expand all groups
private void expandAll() {
int count = listAdapter.getGroupCount();
for (int i = 0; i < count; i++) {
expListView.expandGroup(i);
}
}
/*
* Preparing the list data
*/
private 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");
top250.add("The Godfather");
top250.add("The Godfather: Part II");
top250.add("Pulp Fiction");
top250.add("The Good, the Bad and the Ugly");
top250.add("The Dark Knight");
top250.add("12 Angry Men");
List<String> nowShowing = new ArrayList<String>();
nowShowing.add("The Conjuring");
nowShowing.add("Despicable Me 2");
nowShowing.add("Turbo");
nowShowing.add("Grown Ups 2");
nowShowing.add("Red 2");
nowShowing.add("The Wolverine");
List<String> comingSoon = new ArrayList<String>();
comingSoon.add("2 Guns");
comingSoon.add("The Smurfs 2");
comingSoon.add("The Spectacular Now");
comingSoon.add("The Canyons");
comingSoon.add("Europa Report");
listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
listDataChild.put(listDataHeader.get(1), nowShowing);
listDataChild.put(listDataHeader.get(2), comingSoon);
}
}
ExpandablelistViewActivity Layout ::
/*********************ExpandablelistViewActivity Layout *****************************************/
<LinearLayout
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"
android:background="#drawable/tropical"
tools:context="com.example.localization.ExpandablelistViewActivity" >
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- Listview to display slider menu -->
<ExpandableListView
android:id="#+id/lvExp"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#color/list_divider"
android:dividerHeight="1dp" />
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
ExpandableListAdapter.java:
/****************************ExpandableListAdapter*******************************************/
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
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;
}
}
list_group layout :
/********************* list_group layout*****************************/
<?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" >
<TextView
android:id="#+id/lblListHeader"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
android:textColor="#color/black"
android:textSize="17dp" />
</LinearLayout>
list_item layout:
/********************* list_item layout*****************************/
<?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="55dip"
android:orientation="vertical" >
<TextView
android:id="#+id/lblListItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="17dip"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:textColor="#color/black"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" />
< / LinearLayout>
You can use DrawerLayout and inside it use AnimatedExpandableListView here
Example:
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
<AnimatedExpandableListView
android:id="#+id/left_drawer_user_courses"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/side_menu_color"
android:choiceMode="singleChoice"
android:divider="#drawable/line_bettwen_courses"
android:groupIndicator="#drawable/expandable_list_indicator" />
<LinearLayout
android:id="#+id/right_drawer_user_info"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="#color/side_menu_color"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:orientation="vertical">
<include layout="#layout/userinfo_side_menu"></include>
</LinearLayout>

How to add subitems in a Navigation Drawer

I want to add subitems in my navigation drawer but i am not able to execute it because i have no idea about the logic , i have tried some examples but i am able to display only the items.
i have tried the following code :
package com.example.demo_data1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class NavDrawer extends Activity {
String[] menu;
DrawerLayout dLayout;
ListView dList;
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.third_page);
menu = new String[]{"Home","Android","Windows","Linux","Raspberry Pi","WordPress","Videos","Contact Us"};
dLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
dList = (ListView) findViewById(R.id.drawerList);
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,menu);
dList.setAdapter(adapter);
dList.setSelector(android.R.color.holo_blue_dark);
dList.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
dLayout.closeDrawers();
Bundle args = new Bundle();
args.putString("Menu", menu[position]);
Intent i = new Intent(NavDrawer.this, NavDrawer.class);
switch(position){
case 0:
i = new Intent(NavDrawer.this, MainActivity.class);
break;
case 1:
i = new Intent(NavDrawer.this, Retrieve.class);
break;
}
i.putExtra(null, args);
startActivity(i);
}
});
}
}
Below is the xml layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/mainContent"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
<ListView android:id="#+id/drawerList"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#fff"></ListView>
</android.support.v4.widget.DrawerLayout>
I just want to add subitems to its respective item and the subitems should display on the right side of its parent.
So please anyone can help me with this m a bit new to android,
Thanks and regards.
You can add views to drawer in the xml. Try this:
<!-- Main layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/app_background"
android:orientation="vertical" >
<!-- Place your main screen items here -->
<TextView
android:id="#+id/tvText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Main" />
</LinearLayout>
<!-- Slider menu -->
<LinearLayout
android:id="#+id/menuDrawer"
android:layout_width="290dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="#color/app_background"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/btButton1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button2" />
<Button
android:id="#+id/btButton2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button2" />
</LinearLayout>
<ExpandableListView
android:id="#+id/elvItems"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Use Expandable listview in Drawer
Below is an example
public class MainActivity extends Activity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get the listview
expListView = (ExpandableListView) findViewById(R.id.lvExp);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this, 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(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(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) {
// TODO Auto-generated method stub
Toast.makeText(
getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});
}
/*
* Preparing the list data
*/
private 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");
top250.add("The Godfather");
top250.add("The Godfather: Part II");
top250.add("Pulp Fiction");
top250.add("The Good, the Bad and the Ugly");
top250.add("The Dark Knight");
top250.add("12 Angry Men");
List<String> nowShowing = new ArrayList<String>();
nowShowing.add("The Conjuring");
nowShowing.add("Despicable Me 2");
nowShowing.add("Turbo");
nowShowing.add("Grown Ups 2");
nowShowing.add("Red 2");
nowShowing.add("The Wolverine");
List<String> comingSoon = new ArrayList<String>();
comingSoon.add("2 Guns");
comingSoon.add("The Smurfs 2");
comingSoon.add("The Spectacular Now");
comingSoon.add("The Canyons");
comingSoon.add("Europa Report");
listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
listDataChild.put(listDataHeader.get(1), nowShowing);
listDataChild.put(listDataHeader.get(2), comingSoon);
}}
Adapter
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;
}
}

Navigation Drawer with expandable list inside doesn't work

After several researching and resembling of codes, expandable list inside navigation drawer doesnt work. Is there anyone of you guys have tried doing this.I will really appreciate if you can help me with this. Im striving for this for almost 3 days.Please help me.
Am i miss something on my code?
heres the code:
MainActivity
public class MainActivity extends Activity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
public DrawerLayout drawer;
private LinearLayout linear;
Button btn;
HashMap<String, List<String>> listDataChild;
List<String> listDataHeader;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.button1);
linear = (LinearLayout)findViewById(R.id.linear);
expListView = (ExpandableListView) findViewById(R.id.lvExp);
drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
prepareListData();
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(!drawer.isDrawerOpen(expListView))
{
drawer.openDrawer(expListView);
}
else
{
drawer.closeDrawer(expListView);
}
}
});
// get the listview
// preparing list data
listAdapter = new ExpandableListAdapter(this, 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(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(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) {
// TODO Auto-generated method stub
Toast.makeText(
getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});
}
/*
* Preparing the list data
*/
private 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");
top250.add("The Godfather");
top250.add("The Godfather: Part II");
top250.add("Pulp Fiction");
top250.add("The Good, the Bad and the Ugly");
top250.add("The Dark Knight");
top250.add("12 Angry Men");
List<String> nowShowing = new ArrayList<String>();
nowShowing.add("The Conjuring");
nowShowing.add("Despicable Me 2");
nowShowing.add("Turbo");
nowShowing.add("Grown Ups 2");
nowShowing.add("Red 2");
nowShowing.add("The Wolverine");
List<String> comingSoon = new ArrayList<String>();
comingSoon.add("2 Guns");
comingSoon.add("The Smurfs 2");
comingSoon.add("The Spectacular Now");
comingSoon.add("The Canyons");
comingSoon.add("Europa Report");
listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
listDataChild.put(listDataHeader.get(1), nowShowing);
listDataChild.put(listDataHeader.get(2), comingSoon);
}
}
Heres my activity_main layout
<?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="fill_parent"
android:orientation="vertical"
android:background="#f4f4f4" >
<LinearLayout
android:id="#+id/linear"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#000000"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<Button
android:id="#+id/button1"
android:layout_width="47dp"
android:layout_height="45dp"
android:background="#drawable/arrow_right" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Drawer"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ExpandableListView
android:id="#+id/lvExp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:cacheColorHint="#00000000" >
</ExpandableListView>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
</LinearLayout>
My Baseadapter:
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;
//private LayoutInflater vi;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
//vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#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;
}
}
I think you need to add xml like this
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#f4f4f4" >
<LinearLayout
android:id="#+id/linear"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#000000"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<Button
android:id="#+id/button1"
android:layout_width="47dp"
android:layout_height="45dp"
android:background="#drawable/arrow_right" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Drawer"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
</LinearLayout>
<ExpandableListView
android:id="#+id/lvExp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left"
android:cacheColorHint="#00000000" >
</ExpandableListView>
</android.support.v4.widget.DrawerLayout>

Categories

Resources