I am trying to implement a navigation drawer with a collapsable list view in my app. I want it to appear as follows:
But I get this result:
The Nav drawer is generated by android studio "Drawer Activity" template and here is the code:
activity_main.xml
<android.support.v4.widget.DrawerLayout
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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer">
<ExpandableListView
android:id="#+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.design.widget.NavigationView>
MainActivity.java
private void enableExpandableList() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
expListView = (ExpandableListView) findViewById(R.id.left_drawer);
prepareListData(listDataHeader, listDataChild);
listAdapter = new ExpandListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
expListView.setOnGroupClickListener(new ExpandableListView.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 ExpandableListView.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 ExpandableListView.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 ExpandableListView.OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// till here
Toast.makeText(
getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});
}
private void prepareListData(List<String> listDataHeader, Map<String,
List<String>> listDataChild) {
// Adding child data
listDataHeader.add("Product1");
listDataHeader.add("product2");
listDataHeader.add("Product3");
// Adding child data
List<String> top = new ArrayList<String>();
top.add("x1");
top.add("x2");
top.add("x3");
top.add("x4");
top.add("x5");
List<String> mid = new ArrayList<String>();
mid.add("y1");
mid.add("y2");
mid.add("y3");
List<String> bottom = new ArrayList<String>();
bottom.add("z1");
bottom.add("z2");
bottom.add("z3");
listDataChild.put(listDataHeader.get(0), top); // Header, Child data
listDataChild.put(listDataHeader.get(1), mid);
listDataChild.put(listDataHeader.get(2), bottom);
}
ExpandListAdapter.java
public class ExpandListAdapter 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 ExpandListAdapter(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;
}
}
app_bar_main.xml
<android.support.design.widget.CoordinatorLayout
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="match_parent"
android:layout_height="match_parent"
tools:context="com.dcs.wallhouse.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay"/>
<com.miguelcatalan.materialsearchview.MaterialSearchView
android:id="#+id/search_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
</FrameLayout>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Try adding a LinearLayout so they views won't overlap. The problem is you won't have a single scrolling area.
<android.support.v4.widget.DrawerLayout
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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:orientation="vertical">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer"/>
<ExpandableListView
android:id="#+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Related
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);
}
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);
I implemented a navigation drawer, with navigation view. and i am adding value in navigation view through a menu.xml file.
<android.support.design.widget.NavigationView
android:id="#+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:itemTextColor="#android:color/white"
android:background="?attr/colorAccent"
app:menu="#menu/drawer_view"
app:headerLayout="#layout/nav_header"
>
</android.support.design.widget.NavigationView>
every thing is working fine, but i am facing a problem, I want to display submenu's , after click on menu.
I tried many thing's like:-
I add submenu in menu.xml, inside item.
somthing like this..
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_ChangeOutlet_fragment"
android:icon="#drawable/home_icon"
android:title="#string/changeOutlet"
android:checked="true">
<menu>
<group>
<item
android:title="one"></item>
<item
android:title="two"></item>
<item
android:title="three"></item>
</group>
</menu>
then it return me output like this.
Output: click on this link to see output
https://drive.google.com/file/d/0B0B9-WZYydK7RG1yY0tRdkhOSW8/view?usp=sharing
Now problem is that, in this way i am not able to click on menu , only sub menu are clickable here.
I want here to show sub menu , only after click on menu, otherwise sub menu does shown to other or say it is in invisible state
The best solution is to have an expandable list view in navigation view.See the code below
activity_navigation_view.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:fitsSystemWindows="true">
<include layout="#layout/navigation_view_fragment_container"/>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/navigation_view_header">
<ExpandableListView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#android:color/white"
android:layout_marginTop="192dp"
android:id="#+id/navigationmenu">
</ExpandableListView>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
The layout navigation header is as below
navigation_view_header.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="192dp"
android:background="#ff5722"
android:padding="16dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark"
android:orientation="vertical"
android:gravity="bottom">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Username"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"/>
</LinearLayout>
In your navigation view activity, set the adapter for the expandable list view.
NavigationViewActivity.java
public class NavigationViewActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
ExpandableListAdapter mMenuAdapter;
ExpandableListView expandableList;
List<ExpandedMenuModel> listDataHeader;
HashMap<ExpandedMenuModel, List<String>> listDataChild;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_view);
final ActionBar ab = getSupportActionBar();
/* to set the menu icon image*/
ab.setHomeAsUpIndicator(R.drawable.ic_menu);
ab.setDisplayHomeAsUpEnabled(true);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
expandableList= (ExpandableListView) findViewById(R.id.navigationmenu);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
if (navigationView != null) {
setupDrawerContent(navigationView);
}
prepareListData();
mMenuAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild, expandableList);
// setting list adapter
expandableList.setAdapter(mMenuAdapter);
}
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding data header
listDataHeader.add("heading1");
listDataHeader.add("heading2");
listDataHeader.add("heading3");
// Adding child data
List<String> heading1= new ArrayList<String>();
heading1.add("Submenu of item 1");
List<String> heading2= new ArrayList<String>();
heading2.add("Submenu of item 2");
heading2.add("Submenu of item 2");
heading2.add("Submenu of item 2");
listDataChild.put(listDataHeader.get(0), heading1);// Header, Child data
listDataChild.put(listDataHeader.get(1), heading2);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
mDrawerLayout.openDrawer(GravityCompat.START);
return true;
}
return super.onOptionsItemSelected(item);
}
private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
mDrawerLayout.closeDrawers();
return true;
}
});
}
#Override
public void onFragmentInteraction(Boolean isDataSaved) {
}
}
The adapter for expandable list view is as follows
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context mContext;
private List<String> mListDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> mListDataChild;
ExpandableListView expandList;
public ExpandableListAdapter(Context context, List<String> listDataHeader,HashMap<String, List<String>> listChildData,ExpandableListView mView)
{
this.mContext = context;
this.mListDataHeader = listDataHeader;
this.mListDataChild = listChildData;
this.expandList=mView;
}
#Override
public int getGroupCount() {
int i= mListDataHeader.size();
Log.d("GROUPCOUNT",String.valueOf(i));
return this.mListDataHeader.size();
}
#Override
public int getChildrenCount(int groupPosition) {
int childCount=0;
if(groupPosition!=2)
{
childCount=this.mListDataChild.get(this.mListDataHeader.get(groupPosition))
.size();
}
return childCount;
}
#Override
public Object getGroup(int groupPosition) {
return this.mListDataHeader.get(groupPosition);
}
#Override
public Object getChild(int groupPosition, int childPosition) {
Log.d("CHILD",mListDataChild.get(this.mListDataHeader.get(groupPosition))
.get(childPosition).toString());
return this.mListDataChild.get(this.mListDataHeader.get(groupPosition))
.get(childPosition);
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
ExpandedMenuModel headerTitle = (ExpandedMenuModel) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.listheader, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.submenu);
ImageView headerIcon= (ImageView)convertView.findViewById(R.id.iconimage);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle.getIconName());
headerIcon.setImageDrawable(headerTitle.getIconImg());
return convertView;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_submenu, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.submenu);
txtListChild.setText(childText);
return convertView;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
list_submenu.xml is as follows
<?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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="#000000"
android:layout_marginLeft="20dp"
android:textSize="18sp"
android:id="#+id/submenu"/>
</LinearLayout>
listheader.xml is as follows
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="2dp"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:id="#+id/iconimage"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="#000000"
android:textSize="20sp"
android:id="#+id/submenu"/>
</LinearLayout>
</LinearLayout>
I have posted whole code for clarity. Hope this helps.......
You can create it using a ExpandableListview.
look at this
I prefer you should go with custom layout in side navigation drawer.
Here is sample link - how to implement custom layout in navigation drawer.
You also need to implement expandable listview in side navigation drawer for your scenario, here is sample of expandable list view.
You need to combine both things to achive navigation drawer with expandable list.
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;
}
...
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>