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);
Related
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>
EarBoxExpandableListAdapter
public class EarBoxExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public EarBoxExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
#Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.childrow, null);
}
// TextView txtListChild = (TextView) convertView
// .findViewById(R.id.lblListItem);
//
// txtListChild.setText(childText);
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
#Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
#Override
public int getGroupCount() {
return this._listDataHeader.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.parentrow, null);
}
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
Below is my xml layout code for the child row.Evwnthough I kept the width as match parent, in actual output which I attached as screen shot Im getting the wrap content of the child rows.
childrow.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:id="#+id/btnPlay"
android:src="#android:drawable/ic_media_play"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/round_corner_rect"
android:layout_weight="0"
android:layout_margin="10dp" />
<TextView
android:id="#+id/songCurrentDurationLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="left"
android:textColor="#747272"
android:textStyle="bold"
android:visibility="visible"
android:text="3.25"
android:layout_gravity="center_vertical" />
<SeekBar
android:id="#+id/songProgressBar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:layout_weight="1"
android:layout_gravity="center" />
<TextView
android:id="#+id/songTotalDurationLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="right"
android:textColor="#656262"
android:textStyle="bold"
android:visibility="visible"
android:text="42:52"
android:layout_gravity="center_vertical" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">
<Button
style="#style/earBoxbuttonStyle"
android:text="Save"
android:id="#+id/button"
android:layout_weight="1"
android:layout_width="match_parent" />
<Button
style="#style/earBoxbuttonStyle"
android:text="Reply"
android:id="#+id/button3"
android:layout_weight="1"
android:layout_width="match_parent" />
<Button
style="#style/earBoxbuttonStyle"
android:text="Delete"
android:id="#+id/button4"
android:layout_weight="1"
android:layout_width="match_parent" />
</LinearLayout>
</LinearLayout>
MessageInboxFragment
/**
* Created by sethu on 6/13/2016.
*/
public class MessageInboxFragment extends Fragment{
ExpandableListView expandableListView;
EarBoxExpandableListAdapter listAdapter;
static List<String> listDataHeader;
static HashMap<String, List<String>> listDataChild;
private int lastExpandedPosition = -1;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_messages_inbox, container, false);
expandableListView=(ExpandableListView) rootView.findViewById(R.id.expandableListView);
expandableListView.setDividerHeight(1);
// final ArrayList<Parent> dummyList = ExpandableListMain.buildDummyData();
prepareListData();
listAdapter = new EarBoxExpandableListAdapter(getActivity(), listDataHeader, listDataChild);
expandableListView.setAdapter(listAdapter);
expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
if (lastExpandedPosition != -1
&& groupPosition != lastExpandedPosition) {
expandableListView.collapseGroup(lastExpandedPosition);
}
lastExpandedPosition = groupPosition;
}
});
return rootView;
}
public static void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("Top 250");
listDataHeader.add("Now Showing");
listDataHeader.add("Coming Soon..");
// Adding child data
List<String> top250 = new ArrayList<String>();
top250.add("The Shawshank Redemption");
List<String> nowShowing = new ArrayList<String>();
nowShowing.add("The Conjuring");
List<String> comingSoon = new ArrayList<String>();
comingSoon.add("2 Guns");
listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
listDataChild.put(listDataHeader.get(1), nowShowing);
listDataChild.put(listDataHeader.get(2), comingSoon);
}
}
xml screenshot
device output screenshot
I m creating ExpandableList view in Fragment, the Group item display in list, but when I touch the group item, it is not expanding.
Below is my fragment
public class InstituteFragment extends Fragment {
ExpandableListView expandableListView;
ArrayList<String> subjectName;
HashMap<String,ArrayList<String>> topicName;
public InstituteFragment() {
}
public static InstituteFragment newInstance(){
InstituteFragment fragment = new InstituteFragment();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
subjectName = new ArrayList<>();
topicName = new HashMap<>();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_institute, container, false);
prepData();
expandableListView = (ExpandableListView)view.findViewById(R.id.expd_list_view);
SubjectAdapter adapter = new SubjectAdapter(getActivity(),subjectName,topicName);
expandableListView.setAdapter(adapter);
return view;
}
private void prepData() {
subjectName.add("Physics");
subjectName.add("Chemistry");
subjectName.add("Biology");
ArrayList<String> physics = new ArrayList<>();
physics.add("Demo");
physics.add("Demo1");
physics.add("Demo2");
physics.add("Demo3");
ArrayList<String> chemistry = new ArrayList<>();
chemistry.add("Demo");
chemistry.add("Demo1");
chemistry.add("Demo2");
chemistry.add("Demo3");
ArrayList<String> biology = new ArrayList<>();
biology.add("Demo");
biology.add("Demo1");
biology.add("Demo2");
biology.add("Demo3");
topicName.put(subjectName.get(0), physics);
topicName.put(subjectName.get(1),chemistry);
topicName.put(subjectName.get(2),biology);
}
}
below is adapter
public class SubjectAdapter extends BaseExpandableListAdapter {
Context context;
ArrayList<String> subjectName;
HashMap<String,ArrayList<String>> topicName;
public SubjectAdapter(Context context, ArrayList<String> subjectName, HashMap<String, ArrayList<String>> topicName) {
this.context = context;
this.topicName = topicName;
this.subjectName = subjectName;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return this.topicName.get(this.subjectName.get(groupPosition))
.get(childPosition);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.chapter_name, null);
TextView txtListChild = (TextView) convertView
.findViewById(R.id.chapter_name);
txtListChild.setText(childText);
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return this.topicName.get(this.subjectName.get(groupPosition)).size();
}
#Override
public int getGroupCount() {
return this.subjectName.size();
}
#Override
public Object getGroup(int groupPosition) {
return this.subjectName.get(groupPosition);
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String subjectName = (String)getGroup(groupPosition);
LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.subject_name,null);
TextView textView = (TextView)convertView.findViewById(R.id.subject_name);
textView.setText(subjectName);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
below is institute_fragment.xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragment.InstituteFragment">
<ExpandableListView
android:id="#+id/expd_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ExpandableListView>
</RelativeLayout>
below is my groupview
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/_10sdp"
android:layout_margin="#dimen/_5sdp"
android:background="#android:color/white"
android:id="#+id/subject_name_cardview">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/subject_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Subject Name"
android:textAppearance="?android:textAppearanceMedium"
android:padding="#dimen/_10sdp"
android:layout_marginTop="#dimen/_10sdp"
android:layout_marginBottom="#dimen/_10sdp"
android:layout_marginLeft="#dimen/_10sdp"
android:textColor="#android:color/black" />
<ImageButton
android:id="#+id/subject_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_keyboard_arrow_down_24dp"
android:background="#00000000"
android:layout_marginRight="#dimen/_20sdp"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
below is childView xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/_5sdp"
android:background="#color/colorPrimary"
android:id="#+id/chapter_name_cardview">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/chapter_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Subject Name"
android:textAppearance="?android:textAppearanceSmall"
android:padding="#dimen/_10sdp"
android:layout_marginTop="#dimen/_5sdp"
android:layout_marginBottom="#dimen/_5sdp"
android:layout_marginLeft="#dimen/_10sdp"
android:textColor="#android:color/black" />
<ImageButton
android:id="#+id/chapter_name_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_keyboard_arrow_right_24dp"
android:background="#00000000"
android:layout_marginRight="#dimen/_20sdp"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
Well, easy one.
You have imagebutton in groupview that is intercepting touch event. Add these parameters:
android:focusable="false"
android:focusableInTouchMode="false"
And do same for childview. You have exactly the same case there.
P.S. why are you using imagebuttons anyway? you could use imageviews instead
You need to do this one.
expandableListView = (ExpandableListView)view.findViewById(R.id.expd_list_view);
SubjectAdapter adapter = new SubjectAdapter(getActivity(),subjectName,topicName);
expandableListView.setAdapter(adapter);
expandableListView .setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView expandableListView, View view, int groupPosition, long l) {
expandableListView.expandGroup(groupPosition);
return false;
}
});
Hope this will help you.
Try to pass your ExpandableListView in the adapter:
SubjectAdapter adapter = new SubjectAdapter(getActivity(),subjectName,topicName, expandableListView);
Then youe adapter should look like:
...
ExpandableListView expandableListView;
public SubjectAdapter(Context context, ArrayList<String> subjectName, HashMap<String, ArrayList<String>> topicName, ExpandableListView expandableListView) {
this.context = context;
this.topicName = topicName;
this.subjectName = subjectName;
this.expandableListView = expandableListView;
}
In your group view add following:
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
expandableListView.expandGroup(groupPosition);
}
});
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;
}
...
My problem is, that I have an activity with one big TextView and one ExpandableListView underneath the TextView. Unfortunately the text of the TextView is too long, so I can't see the details when I expand the ExpandableListView.
So my question is, how can I use CWAC-Merge in my Code, to get one big scrollable Activity.
I would like to have, that everything in the activity_contact.xml from scrollView1 to the bottom is scrollable! I already copied the necessary libs (merge-1.0.1.jar and sacklist-1.0.0.jar) to my Project, but I don't know how to apply the MergeAdapter. I would really appreciate, if someone could tell me where and how I have to use the MergeAdapter!
This is my activity_contact.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include layout="#layout/titlebar" />
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_marginTop="0dp"
android:layout_weight="1"
android:fillViewport="true" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/contact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="#string/contact"
android:textSize="#dimen/textSize" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ExpandableListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" >
</ExpandableListView>
</TableRow>
</TableLayout>
</ScrollView>
</LinearLayout>
This is my Contact.java class:
public class Contact extends Activity {
private SparseArray<Group> groups = new SparseArray<Group>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact);
ExpandableListView listView = (ExpandableListView) findViewById(R.id.listView);
createData();
ExpandableListAdapter adapter = new ExpandableListAdapter(this, groups,
false, null);
listView.setAdapter(adapter);
}
private void createData() {
Group group1 = new Group(getString(R.string.stringGroup));
group1.children.add(getString(R.string.stringGroup2));
groups.append(0, group1);
}
}
This is my ExpandableListAdapter.java
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private final SparseArray<Group> groups;
private LayoutInflater inflater;
private Activity activity;
private boolean setOnClickListener;
private Class<?> onClickClass;
public ExpandableListAdapter(Activity act, SparseArray<Group> groups,
boolean setOnClickListener, Class<?> onClickListenerClass) {
activity = act;
this.groups = groups;
inflater = act.getLayoutInflater();
this.setOnClickListener = setOnClickListener;
this.onClickClass = onClickListenerClass;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return groups.get(groupPosition).children.get(childPosition);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
#Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String children = (String) getChild(groupPosition, childPosition);
TextView text = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.listrow_details, null);
}
text = (TextView) convertView.findViewById(R.id.copyrightTextView);
text.setText(Html.fromHtml(children));
if (setOnClickListener) {
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(activity, onClickClass);
intent.putExtra("name", children);
activity.startActivity(intent);
}
});
}
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return groups.get(groupPosition).children.size();
}
#Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
#Override
public int getGroupCount() {
return groups.size();
}
#Override
public void onGroupCollapsed(int groupPosition) {
super.onGroupCollapsed(groupPosition);
}
#Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
}
#Override
public long getGroupId(int groupPosition) {
return 0;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.listrow_group, null);
}
Group group = (Group) getGroup(groupPosition);
((CheckedTextView) convertView).setText(group.string);
((CheckedTextView) convertView).setChecked(isExpanded);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
The listrow_details.xml:
<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="50dp"
android:clickable="true"
android:orientation="vertical" >
<TextView
android:id="#+id/copyrightTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="15dp"
android:gravity="center_vertical"
android:linksClickable="true"
android:padding="10dp"
android:textStyle="bold" >
</TextView>
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#android:color/black" />
</LinearLayout>
MergeAdapter is a ListAdapter, not an ExpandableListAdapter. You cannot use MergeAdapter to solve your problem. You are welcome to read through the source code to MergeAdapter and attempt to figure out if there is a way to create a MergeExpandableListAdapter or something like that.
Or, come up with a UI that does not have a long TextView after the ExpandableListView.