In My expandable list view first child item has two buttons.button1 and button2.in other child list item has check box and text.these check boxes are default disabled
in first time user expand the child list and after press button1 i want to enable all check boxes in particular group
As this point i can only enable first child list item checkbox.how can enable all check boxes by clicking button on the 1st child item.
Expandable list adapter
public class ExpandListAdapter extends BaseExpandableListAdapter {
private Context context;
private ArrayList<ExpandableListGroup> groups;
public ExpandListAdapter(Context context, ArrayList<ExpandableListGroup> groups) {
this.context = context;
this.groups = groups;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
ArrayList<ExpandableListChild> chList = groups.get(groupPosition).getItems();
return chList.get(childPosition);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final ExpandableListChild child = (ExpandableListChild) getChild(groupPosition, childPosition);
final ViewHolder holder;
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.btn_start_inspection = (Button) convertView.findViewById(R.id.btn_start_inspection);
holder.btn_complete_inspection = (Button) convertView.findViewById(R.id.btn_complete_inspection);
holder.cbx_complete_inspection = (CheckBox) convertView.findViewById(R.id.check_sub_task);
holder.txt_activity_name = (TextView) convertView.findViewById(R.id.txt_sub_task_title);
holder.txt_activity_name.setText(child.getName().toString());
//button for enable checkboxes
holder.btn_start_inspection.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.btn_complete_inspection.setBackgroundColor(Color.RED);
holder.itemEnabled = true;
notifyDataSetChanged();
}
});
//enable check boxes
if(holder.itemEnabled == true){
holder.cbx_complete_inspection.setEnabled(true);
}else{
holder.cbx_complete_inspection.setEnabled(false);
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
ArrayList<ExpandableListChild> chList = groups.get(groupPosition).getItems();
return chList.size();
}
#Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
#Override
public int getGroupCount() {
return groups.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
ExpandableListGroup group = (ExpandableListGroup) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater inf = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.list_group, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.txt_company_name);
tv.setText(group.getName());
TextView tvAddress = (TextView) convertView.findViewById(R.id.txt_company_address);
tvAddress.setText(group.getAddressLevel());
iv = (ImageView) convertView.findViewById(R.id.img_expand);
return convertView;
}
#Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
iv.setImageDrawable(context.getResources().getDrawable(R.drawable.exp_fold));
}
#Override
public void onGroupCollapsed(int groupPosition) {
super.onGroupCollapsed(groupPosition);
iv.setImageDrawable(context.getResources().getDrawable(R.drawable.exp_min));
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
static class ViewHolder {
Button btn_complete_inspection;
Button btn_start_inspection;
CheckBox cbx_complete_inspection;
TextView txt_activity_name;
boolean itemEnabled = false;
}
}
listchildItem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/lnrChild"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/lnr_task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="10dp">
<Button
android:id="#+id/btn_start_inspection"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_gravity="left"
android:layout_marginLeft="10dp"
android:background="#drawable/rounded_cornerexpandable_list_child"
android:gravity="center"
android:text="Start Inspection"
android:textAllCaps="false"
android:textColor="#color/color_white"
android:textSize="12sp"
></Button>
<Button
android:id="#+id/btn_complete_inspection"
android:layout_width="150dp"
android:layout_height="35dp"
android:layout_marginLeft="150dp"
android:src="#drawable/rounded_cornerexpandable_list_child_gray"
android:text="Complete Inspection"
android:textAllCaps="false"
android:textColor="#color/color_white"
android:textSize="12sp"
></Button>
<LinearLayout
android:id="#+id/lnr_active_sub_task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/btn_complete_inspection">
<LinearLayout
android:id="#+id/lnr_sub_task"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:gravity="center"
android:orientation="horizontal">
<CheckBox
android:id="#+id/check_sub_task"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:checked="false"
android:gravity="center"></CheckBox>
</LinearLayout>
<LinearLayout
android:id="#+id/textHolder"
android:layout_width="0.0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:gravity="left"
android:orientation="horizontal">
<TextView
android:id="#+id/txt_sub_task_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="exx"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:layout_width="0.0dp"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_weight="0.5"
android:gravity="right"
android:orientation="horizontal">
<ImageView
android:id="#+id/txt_sub_task_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_keyboard_arrow_right_black_24dp"
android:gravity="center"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
What I see is that after click button on 1st list item only 1st check box in the 1st child item is enabled, the other list item check boxes are not. What I want is that checkboxes in the child item rows are enabled. (extra lines added from comments)
Related
I followed this tutorial http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/. In that tutorial the child layout set as string format but here I need to set layout directly instead of creating a string.
Here is my child layout code, that have Three buttons Yes, No, Maybe and a Checkbox.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/hidden"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginLeft="-270dp"
android:layout_marginTop="60dp"
android:layout_weight="1"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="horizontal"
android:paddingTop="1dp"
android:visibility="gone"
android:weightSum="3">
<Button
android:id="#+id/yesButton"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="7dp"
android:layout_weight="1"
android:background="#color/lightgray"
android:text="Yes"
android:textColor="#color/black"></Button>
<Button
android:id="#+id/noButton"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="25dp"
android:layout_weight="1"
android:background="#color/lightgray"
android:text="No"
android:textColor="#color/black"></Button>
<Button
android:id="#+id/buttonMayBe"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="25dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:background="#color/lightgray"
android:text="Maybe"
android:textColor="#color/black"></Button>
<CheckBox
android:id="#+id/invitationCheckBox"
android:layout_width="#dimen/check_box_width"
android:layout_height="50dp"
android:button="#drawable/selector"
android:focusable="true"
android:scaleX="0.5"
android:scaleY="0.5" />
</LinearLayout>
Expandable listview adpater
public class ExpandableAdapter extends BaseExpandableListAdapter {
private Context _context;
ArrayList<Group> list;
public ExpandableAdapter(Context context, ArrayList<Group> list) {
this._context = context;
this.list = list;
}
#Override
public Object getChild(int groupPosition, int childPosititon) {
return list.get(groupPosition).getMembers().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) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.expand_child, null);
}
TextView txtListChild = (TextView) convertView.findViewById(R.id.text1);
txtListChild.setText(list.get(groupPosition).getMembers().get(childPosition).getName());
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return list.get(groupPosition).getMembers().size();
}
#Override
public Object getGroup(int groupPosition) {
return list.get(groupPosition);
}
#Override
public int getGroupCount() {
return list.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(android.R.layout.simple_expandable_list_item_2, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(android.R.id.text1);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(list.get(groupPosition).getGroupName());
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
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.
I have an expandable listview. I have put some dummy data and evrything seems ok but when I run the application all groups are in collapse mode and they don't take effect when I click on each of them. This is the screenshot:
XML of Group is:
<?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="wrap_content"
android:background="#color/bg_slider"
android:orientation="horizontal" >
<TextView
android:id="#+id/tvRecipeName"
style="#style/normal_text.bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="50dp"
android:layout_marginRight="5dp"
android:focusable="false"
android:lines="1"
android:maxLines="1"
android:text="#string/dd_title" />
<ImageButton
android:id="#+id/ibDeleteRecipe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:background="#color/transparent"
android:contentDescription="#string/cd"
android:focusable="false"
android:src="#android:drawable/ic_menu_delete" />
<View
android:layout_width="1dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/tvRecipeName"
android:focusable="false" />
</RelativeLayout>
XML of child is:
<?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="wrap_content"
android:background="#color/White"
android:gravity="left|center_vertical" >
<View
android:layout_width="1dp"
android:layout_height="35dp"
android:layout_toLeftOf="#+id/tvIngredient"
android:focusable="false" />
<TextView
android:id="#+id/tvIngredient"
style="#style/normal_text_black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="30dp"
android:layout_marginRight="5dp"
android:lines="1"
android:maxLines="1"
android:text="#string/dd_title"
android:focusable="false" />
<ImageButton
android:id="#+id/ibDeleteIngredient"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:background="#color/transparent"
android:contentDescription="#string/cd"
android:focusable="false"
android:src="#android:drawable/btn_dialog" />
</RelativeLayout>
and in main.xml, definition of expandable list is:
<ExpandableListView
android:id="#+id/elv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
I have an adapter that I have written this code for that:
public class ExpAdapter extends BaseExpandableListAdapter {
private final String TAG = "ExpAdapter";
private Context context;
static final String arrGroupelements[] = {"India", "Australia", "England", "South Africa"};
static final String arrChildelements[][] = { {"Sachin Tendulkar", "Raina", "Dhoni", "Yuvi" },
{"Ponting", "Adam Gilchrist", "Michael Clarke"},
{"Andrew Strauss", "kevin Peterson", "Nasser Hussain"},
{"Graeme Smith", "AB de villiers", "Jacques Kallis"} };
public ExpAdapter(Context context) {
this.context = context;
Log.i(TAG, "Adapter created.");
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.elv_child, null);
}
TextView tvItem = (TextView) convertView.findViewById(R.id.tvIngredient);
ImageButton ibDelete = (ImageButton) convertView.findViewById(R.id.ibDeleteIngredient);
ibDelete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.i(TAG, "******");
}
});
tvItem.setText(arrChildelements[groupPosition][childPosition]);
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return arrChildelements[groupPosition].length;
}
#Override
public Object getGroup(int groupPosition) {
return null;
}
#Override
public int getGroupCount() {
return arrGroupelements.length;
}
#Override
public long getGroupId(int groupPosition) {
return 0;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.elv_group, null);
}
TextView tvItem = (TextView) convertView.findViewById(R.id.tvRecipeName);
ImageButton ibDeleteRcipe = (ImageButton) convertView.findViewById(R.id.ibDeleteRecipe);
ibDeleteRcipe.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.i(TAG, "%%%%%%");
}
});
tvItem.setText(arrGroupelements[groupPosition]);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
and finally, in code of fragment activity i have:
public class ShoppingList extends FragmentActivity {
ExpAdapter adapter = new ExpAdapter(this);
//Linking expnadable list view
expListView = (ExpandableListView) findViewById(R.id.elv);
expListView.setAdapter(adapter);
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
Log.i(TAG, "Group " + groupPosition + " expanded.");
}
});
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int groupPosition) {
Log.i(TAG, "Group " + groupPosition + " collapsed.");
}
});
expListView.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Log.i(TAG, "item " + childPosition + " of group " + groupPosition + " clicked.");
return false;
}
});
}
When I run the application and click on parents, nothing happens. I tried to add following lines of code at the end of shopping class:
int count = adapter.getGroupCount();
for (int position = 1; position <= count; position++)
expListView.expandGroup(position - 1);
Now when I run the application result is like this screenshot:
If I click on delete buttons (parent or child), I can see they take effect (as I check logcat), however when I click on child or parent nothing happens. So, I have no idea why callbacks doesn't work. Based on my research I found that I need to set Focussable of image buttons to false. As you can see in XML files I did it but still when I click on parent and child rows I can't see any response.
Finally I found the solution :)
I have no idea is it bug or disease or ...
As mentioned above based on my research I found that we need to set focus-ability of image view/box to "false". I did it in XML file and it didn't work. I set focus-ability to "true" in XML and in code set it to false. like this:
#Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.elv_group, null);
}
TextView tvItem = (TextView) convertView.findViewById(R.id.tvRecipeName);
ImageButton ibDeleteRcipe = (ImageButton) convertView.findViewById(R.id.ibDeleteRecipe);
ibDeleteRcipe.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
...
}
});
ibDeleteRcipe.setFocusable(false);
tvItem.setText(arrGroupElements[groupPosition]);
return convertView;
}
Therefore based of my achievement! it should be set to "false" in code not in XML! Now the question is What is difference? -I have no idea.
It is necessary to define android:focusable="false" in the layout xml for focusable views within the expandable list view item but not in the "ExpandableListView" attribute.
Eg: if a custom parent list item includes a checkbox (it will automatically get the focus) it should be like below.
<CheckBox
android:id="#+id/cbkSelectDownload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"/>
transferAvailPowered.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:layout_margin="5dp"
android:padding="5dp"
android:gravity="left">
<TextView
android:id="#+id/availSerial"
android:layout_width="0dp"
android:layout_weight=".30"
android:layout_height="wrap_content"
android:layout_marginLeft="45dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:focusable="false"
android:padding="5dp"
android:gravity="left" />
<TextView
android:id="#+id/availModel"
android:layout_width="0dp"
android:layout_weight=".30"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:focusable="false"
android:padding="5dp"
android:gravity="left" />
<AutoCompleteTextView
android:id="#+id/availSite"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:padding="5dp"
android:hint="To Site"
android:background="#android:color/white"
android:textColor="#android:color/black"
android:textCursorDrawable="#null"
android:focusable="false"
android:layout_margin="5dp"
android:gravity="left" />
<ImageButton
android:id="#+id/addToTransfer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_margin="5dp"
android:background="#drawable/addsmall"
android:focusable="false"
android:gravity="left" />
transferAvailAttached.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:layout_margin="5dp"
android:padding="5dp">
<TextView
android:id="#+id/availSerial"
android:layout_width="0dp"
android:layout_weight=".30"
android:focusable="false"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:padding="5dp"
android:gravity="right" />
<TextView
android:id="#+id/availModel"
android:layout_width="0dp"
android:layout_weight=".30"
android:layout_height="wrap_content"
android:focusable="false"
android:layout_margin="5dp"
android:padding="5dp"
android:gravity="center" />
<CheckBox
android:id="#+id/include"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left|center_vertical"
android:focusable="false"
android:layout_marginRight="30dp"
android:background="#drawable/bg_checkbox" />
<ImageButton
android:id="#+id/removeAttachment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:padding="5dp"
android:layout_margin="5dp"
android:background="#drawable/deletesmall"
android:gravity="right" />
Adapter
class EquipAdapter : BaseExpandableListAdapter
{
private List<CPEquipment> Parent { get; set; }
private List<List<CPEquipment>> Child { get; set; }
private Context _context { get; set; }
private IListAdapter _adapter { get; set; }
private ExpandableListView _list { get; set; }
public EquipAdapter(Context context, List<CPEquipment> parent, List<List<CPEquipment>> child, IListAdapter adapter, ExpandableListView list)
{
_context = context;
Parent = parent;
Child = child;
_adapter = adapter;
_list = list;
}
public override Java.Lang.Object GetChild(int groupPosition, int childPosition)
{
List<CPEquipment> level1 = Child.ElementAt(groupPosition);
CPEquipment level2 = level1.ElementAt(childPosition);
E e = new E() {Serial = level2.Serial, Model = level2.Model};
return e;
}
public override long GetChildId(int groupPosition, int childPosition)
{
return Convert.ToInt32(groupPosition.ToString(CultureInfo.InvariantCulture) + childPosition.ToString(CultureInfo.InvariantCulture));
}
public override int GetChildrenCount(int groupPosition)
{
return Child.ElementAt(groupPosition).Count;
}
public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)
{
if (convertView == null)
{
LayoutInflater inflater = (LayoutInflater)_context.GetSystemService(Context.LayoutInflaterService);
convertView = inflater.Inflate(Resource.Layout.transferAvailAttached, null);
}
E e = (E)GetChild(groupPosition, childPosition);
TextView serial = (TextView)convertView.FindViewById(Resource.Id.availSerial);
serial.Text = e.Serial;
TextView model = (TextView)convertView.FindViewById(Resource.Id.availModel);
model.Text = e.Model;
return convertView;
}
public override Object GetGroup(int groupPosition)
{
CPEquipment c = Parent.ElementAt(groupPosition);
E e = new E(){Serial = c.Serial, Model = c.Model, Type = c.Status};
return e;
}
public override long GetGroupId(int groupPosition)
{
return groupPosition;
}
public override View GetGroupView(int groupPosition, bool isExpanded, View convertView, ViewGroup parent)
{
E e = (E)GetGroup(groupPosition);
if (convertView == null)
{
LayoutInflater inflater = (LayoutInflater)_context.GetSystemService(Context.LayoutInflaterService);
convertView = inflater.Inflate(Resource.Layout.transferAvailPowered, null);
}
TextView serial = (TextView)convertView.FindViewById(Resource.Id.availSerial);
serial.Text = e.Serial;
TextView model = (TextView)convertView.FindViewById(Resource.Id.availModel);
model.Text = e.Model;
AutoCompleteTextView acText = (AutoCompleteTextView)convertView.FindViewById(Resource.Id.availSite);
acText.Adapter = _adapter;
_list.ExpandGroup(groupPosition);
return convertView;
}
public override bool IsChildSelectable(int groupPosition, int childPosition)
{
return true;
}
public override int GroupCount
{
get { return Parent.Count; }
}
public override bool HasStableIds
{
get { return true; }
}
}
Outcome:
In this scenario, the AutoCompleteTextView in the parent group and the green plus button should be "selectable" so that the user can input the info into the field and click that button without collapsing the group. And the CheckBox and the red x button in the child should also be "selectable" so the user can check the CheckBox and click the button. The only part of that that actually works is that the CheckBox is selectable and the group doesn't collapse because for lack of a better term, the group layout is "dead" and does nothing when pressed. And the seemingly "working" CheckBox isn't even so because it does this weird thing where checking or unchecking one will randomly check or uncheck others.
Below code is to resolve checkbox problem "CheckBox isn't even so because it does this weird thing where checking or unchecking one will randomly check or uncheck others." Along with ImageButton and CheckBox handling.
public override View GetChildView(final int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater)_context.GetSystemService(Context.LayoutInflaterService);
convertView = inflater.Inflate(Resource.Layout.transferAvailAttached, null);
E e = (E)GetChild(groupPosition, childPosition);
TextView serial = (TextView)convertView.FindViewById(Resource.Id.availSerial);
serial.Text = e.Serial;
TextView model = (TextView)convertView.FindViewById(Resource.Id.availModel);
model.Text = e.Model;
CheckBox include = (CheckBox)convertView.FindViewById(Resource.Id.include);
include.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
//DO your checkbox handling here
}
});
ImageButton removeAttachment =(CheckBox)convertView.FindViewById(Resource.Id. removeAttachment);
removeAttachment.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
//DO your imageButton handling here
}
});
return convertView;
}
public override View GetGroupView(final int groupPosition, bool isExpanded, View convertview, ViewGroup parent)
{
View convertView = convertview;
if (convertView == null)
{
E e = (E)GetGroup(groupPosition);
LayoutInflater inflater = (LayoutInflater)_context.GetSystemService(Context.LayoutInflaterService);
convertView = inflater.Inflate(Resource.Layout.transferAvailPowered, null);
}
TextView serial = (TextView)convertView.FindViewById(Resource.Id.availSerial);
serial.Text = e.Serial;
TextView model = (TextView)convertView.FindViewById(Resource.Id.availModel);
model.Text = e.Model;
AutoCompleteTextView acText = (AutoCompleteTextView)convertView.FindViewById(Resource.Id.availSite);
acText.Adapter = _adapter;
ImageButton addToTransfer =(CheckBox)convertView.FindViewById(Resource.Id. addToTransfer);
addToTransfer.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
//DO your addToTransfer imageButton handling here
}
});
_list.ExpandGroup(groupPosition);
return convertView;
}