Hi I am new to android and I am trying to implement Expandable listview below list view in drawer layout.I am getting problem while using liatview and expandable listview together as I am unable to place both in drawer layout
here is my code acrivity_main
<android.support.v4.widget.DrawerLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:fitsSystemWindows="true"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="dev.edmt.expandablelistview.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ExpandableListView
android:id="@+id/lvExp"
android:layout_height="match_parent"
android:layout_width="250dp"
android:layout_gravity="left"></ExpandableListView>
</LinearLayout>
<ListView
android:divider="@null"
android:background="#b9cae5"
android:id="@+id/drawerList"
android:layout_width="250dp"
android:layout_gravity="left"
android:layout_height="match_parent"/></android.support.v4.widget.DrawerLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private ExpandableListView listView;
private ExpandableListAdapter listAdapter;
private List < String > listDataHeader;
private HashMap < String, List < String >> listHash;
ListView nlistView;
MyAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nlistView = (ListView) findViewById(R.id.drawerList);
myAdapter = new MyAdapter(this);
nlistView.setAdapter(myAdapter);
listView = (ExpandableListView) findViewById(R.id.lvExp);
initData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listHash);
listView.setAdapter(listAdapter);
}
private void initData() {
listDataHeader = new ArrayList < > ();
listHash = new HashMap < > ();
listDataHeader.add("Hello");
List < String > edmtDev = new ArrayList < > ();
edmtDev.add("This is Expandable ListView");
listHash.put(listDataHeader.get(0), edmtDev);
}}
ExpandableListAdapter
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> listDataHeader;
private HashMap<String,List<String>> listHashMap;
public ExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listHashMap) {
this.context = context;
this.listDataHeader = listDataHeader;
this.listHashMap = listHashMap;
}
@Override
public int getGroupCount() {
return listDataHeader.size();
}
@Override
public int getChildrenCount(int i) {
return listHashMap.get(listDataHeader.get(i)).size();
}
@Override
public Object getGroup(int i) {
return listDataHeader.get(i);
}
@Override
public Object getChild(int i, int i1) {
return listHashMap.get(listDataHeader.get(i)).get(i1); // i = Group Item , i1 = ChildItem
}
@Override
public long getGroupId(int i) {
return i;
}
@Override
public long getChildId(int i, int i1) {
return i1;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
String headerTitle = (String)getGroup(i);
if(view == null)
{
LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.list_group,null);
}
TextView lblListHeader = (TextView)view.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return view;
}
@Override
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
final String childText = (String)getChild(i,i1);
if(view == null)
{
LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.list_item,null);
}
TextView txtListChild = (TextView)view.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
return view;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}}
NormalListView Adapter
public class MyAdapter extends BaseAdapter {
String[] avengers;
private Context context;
//integer array for the images
public MyAdapter(Context context) {
//here initialize the array
this.context = context;
avengers = context.getResources().getStringArray(R.array.avengers);
}
@Override
public int getCount() {
return avengers.length;
}
@Override
public Object getItem(int position) {
//shud return a particular text at a given position
return avengers[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
//in this method we are going to perfom all work that is needed t obe performed via the list view
//perfomr all our work
View row = null;
if (view == null) { // if its null then we are creating this row for the first time
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.custom_row, viewGroup, false);
System.out.println("the row inflated is " + row);
} else {
row = view;
}
TextView titleTextView = (TextView) row.findViewById(R.id.textView);
titleTextView.setText(avengers[position]);
return row;
}}
I was unable to imply Expandable Listview with Normal ListView but achieved the same in ListViews by implementing 2 listviews.
FinalActivity.java this is my main Java file
public class FinalActivity extends AppCompatActivity {
ListView listView, secondList;
String secondData[] = {
"hello",
"hi",
"bye"
};
String data[] = {
"Sunday",
"Monday",
"Tuesday"
};
ArrayAdapter < String > adapter, secondAdapter;
RelativeLayout layout;
public int a = 0;
DrawerLayout drawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_final);
listView = (ListView) findViewById(R.id.listings);
secondList = (ListView) findViewById(R.id.secondListings);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
adapter = new ArrayAdapter < String > (FinalActivity.this, android.R.layout.simple_list_item_1, data);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView << ? > adapterView, View view, int position, long l) {
System.out.println("the checked item is " + position);
if (position == 2) {
if (a == 0) {
secondList.setVisibility(View.VISIBLE);
secondAdapter = new ArrayAdapter < String > (FinalActivity.this, android.R.layout.simple_list_item_1, secondData);
secondList.setAdapter(secondAdapter);
a = 1;
} else if (a == 1) {
secondList.setVisibility(View.GONE);
a = 0;
}
System.out.println("the value of a is " + a);
}
}
});
}
}
My activity_main.xm`
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer"
tools:context="com.example.user.loadmore.FinalActivity">
<include layout="@layout/inclusion"/></android.support.v4.widget.DrawerLayout>
My inclusion.xml file which contains both listviews
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_gravity="left"
android:layout_height="match_parent"
android:background="#b6bac1">
<ListView
android:id="@+id/listings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/secondListings"
android:layout_gravity="start"
android:layout_below="@+id/listings"
/></LinearLayout>