Good evening everyone !
I am currently working on an ExpandableListView.
The parent contains 1 ImageView.
The child contains 1 TextView and 3 Buttons.
In terms of display, no problem: everything works as I want.
However, I want to add actions to my buttons and I realize that when I put a SetOnclickListener in the getChildView I am not able to launch a new INTENT.
The examples I find on the internet seem to show only clicks on dynamic elements. However, the buttons are not dynamic in my case.
I planned to use a variable to do a SWITCH and thus launch the right INTENT.
But since it doesn't work, I wonder if my direction is the right one.
MaintActivity.java
package com.evo.tab2escape;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;
public class jeux2 extends Activity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<Integer> headerData;
HashMap<Integer,ArrayList<ChildDataModel>> childData;
ChildDataModel childDataModel;
Context mContext;
ArrayList<ChildDataModel> logo0,logo1,logo2,logo3,logo4,logo5,logo6,logo7,logo8,logo9,logo10,logo11;
private int lastExpandedPosition = -1;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.jeux2);
mContext = this;
headerData = new ArrayList<>();
childData = new HashMap<Integer, ArrayList<ChildDataModel>>();
logo0 = new ArrayList<>();
logo1 = new ArrayList<>();
logo2 = new ArrayList<>();
logo3 = new ArrayList<>();
logo4 = new ArrayList<>();
logo5 = new ArrayList<>();
logo6 = new ArrayList<>();
logo7 = new ArrayList<>();
logo8 = new ArrayList<>();
logo9 = new ArrayList<>();
logo10 = new ArrayList<>();
logo11 = new ArrayList<>();
// get the listview
expListView = (ExpandableListView) findViewById(R.id.ExpandableListView);
// populate data
// Adding header data
headerData.add(R.drawable.logo0);
headerData.add(R.drawable.logo1);
headerData.add(R.drawable.logo2);
headerData.add(R.drawable.logo3);
headerData.add(R.drawable.logo4);
headerData.add(R.drawable.logo5);
headerData.add(R.drawable.logo6);
headerData.add(R.drawable.logo7);
headerData.add(R.drawable.logo8);
headerData.add(R.drawable.logo9);
headerData.add(R.drawable.logo10);
headerData.add(R.drawable.logo11);
// Adding child data
childDataModel = new ChildDataModel(1,"Test0","logo0");
logo0.add(childDataModel);
childData.put(headerData.get(0),logo0);
childDataModel = new ChildDataModel(1,"Test1","logo1");
logo1.add(childDataModel);
childData.put(headerData.get(1),logo1);
childDataModel = new ChildDataModel(1,"Test2","logo2");
logo2.add(childDataModel);
childData.put(headerData.get(2),logo2);
childDataModel = new ChildDataModel(1,"Test3","logo3");
logo3.add(childDataModel);
childData.put(headerData.get(3),logo3);
childDataModel = new ChildDataModel(1,"Test4","logo4");
logo4.add(childDataModel);
childData.put(headerData.get(4),logo4);
childDataModel = new ChildDataModel(1,"Test5","logo5");
logo5.add(childDataModel);
childData.put(headerData.get(5),logo5);
childDataModel = new ChildDataModel(1,"Test6","logo6");
logo6.add(childDataModel);
childData.put(headerData.get(6),logo6);
childDataModel = new ChildDataModel(1,"Test7","logo7");
logo7.add(childDataModel);
childData.put(headerData.get(7),logo7);
childDataModel = new ChildDataModel(1,"Test8","logo8");
logo8.add(childDataModel);
childData.put(headerData.get(8),logo8);
childDataModel = new ChildDataModel(1,"test09","logo9");
logo9.add(childDataModel);
childData.put(headerData.get(9),logo9);
childDataModel = new ChildDataModel(1,"test10","logo10");
logo10.add(childDataModel);
childData.put(headerData.get(10),logo10);
childDataModel = new ChildDataModel(1,"test11","logo11");
logo11.add(childDataModel);
childData.put(headerData.get(11),logo11);
listAdapter = new ExpandableListAdapter(this, headerData, childData);
// setting list adapter
expListView.setAdapter(listAdapter);
//--------------------------child click listener--------------------------
expListView.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id)
{
Log.d("TEST", "onChildClick: " + groupPosition + " " + childPosition + " " + id + " " + parent);
//Toast.makeText(getApplicationContext(), "heeeeeeeeerrrrreeeee!", Toast.LENGTH_SHORT).show();
return true;
}
});
//group expanded
expListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
#Override
public void onGroupExpand(int headPosition) {
if (lastExpandedPosition != -1
&& headPosition != lastExpandedPosition) {
expListView.collapseGroup(lastExpandedPosition);
}
lastExpandedPosition = headPosition;
}
});
//group collapsed
expListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int headPosition) {
}
});
//--------------------------RETOUR--------------------------
Button retour = (Button) findViewById(R.id.retour);
retour.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Intent Intent_next = new Intent(jeux2.this, MainActivity.class);
startActivity(Intent_next);
}
});
//--------------------------HISTO--------------------------
TextView histo = (TextView) findViewById(R.id.histo);
histo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Intent Intent_next = new Intent(jeux2.this, historique.class);
startActivity(Intent_next);
}
});
}
}
class ChildDataModel
{
long id;
String txt;
String aventure;
public ChildDataModel(int id, String explications, String aventure) {
this.setId(id);
this.setTxt(explications);
}
public long getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTxt() {
return txt;
}
public void setTxt(String explications) {
this.txt = explications;
}
public String getTxt_aventure() {
return aventure;
}
#Override
public String toString()
{
return super.toString();
}
}
ExpandableListAdapter.java
package com.evo.tab2escape;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<Integer> headerData; // Images
// child data in format of header , child
private HashMap<Integer, ArrayList<ChildDataModel>> childData;
public ExpandableListAdapter(Context context, List<Integer> listDataHeader,
HashMap<Integer, ArrayList<ChildDataModel>> childData) {
this._context = context;
this.headerData = listDataHeader;
this.childData = childData;
}
#Override
public Object getChild(int groupPosition, int childPosititon) {
return this.childData.get(this.headerData.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) {
ChildDataModel child = (ChildDataModel) 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 explications = (TextView) convertView.findViewById(R.id.explications);
explications.setText(child.getTxt());
Button play = (Button) convertView.findViewById(R.id.play);
Button doc = (Button) convertView.findViewById(R.id.doc);
Button param = (Button) convertView.findViewById(R.id.param);
final String aventure = child.getTxt_aventure();
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Log.d("TAG", "poulet: ");
}
});
doc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Log.d("TAG", "poulet: ");
}
});
param.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Log.d("TAG", "poulet: ");
}
});
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return this.childData.get(this.headerData.get(groupPosition))
.size();
}
#Override
public Object getGroup(int groupPosition) {
return this.headerData.get(groupPosition);
}
#Override
public int getGroupCount() {
return this.headerData.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(R.layout.list_group, null);
}
ImageView logo = (ImageView) convertView.findViewById(R.id.logo);
int imageId = this.headerData.get(groupPosition);
logo.setImageResource(imageId);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
list_group.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">
<Space
android:layout_width="match_parent"
android:layout_height="20dp">
</Space>
<ImageView
android:id="#+id/logo"
android:layout_width="match_parent"
android:scaleType="fitXY"
android:layout_height="100dp"/>
</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="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal"
android:id="#+id/divers">
<TextView
android:id="#+id/explications"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:gravity="center_vertical"
android:textStyle="bold"
android:layout_weight="1.5">
</TextView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal">
<Button
android:id="#+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Jouer"
android:layout_gravity="center"
android:background="#drawable/bouton_main"
android:layout_weight="1">
</Button>
<Space
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
</Space>
<Button
android:id="#+id/doc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Documents"
android:layout_gravity="center"
android:background="#drawable/bouton_main"
android:layout_weight="1">
</Button>
<Space
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
</Space>
<Button
android:id="#+id/param"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Paramètres"
android:layout_gravity="center"
android:background="#drawable/bouton_main"
android:layout_weight="1">
</Button>
<Space
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
</Space>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Can somoene help me ?
I want to start a new intent on click on Button.
Edit : i add child Click Listener on MainActivity but its works only if i set clickable,focusable = false on the 3 Buttons in layout list_item.xml.
And than the click is on the complete child. I can't say if the click is on Button 1, 2 or 3.
Can we fix it ?
Problem solved.
I do this in ExpandableListAdapter in getChildView.
param.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent next = new Intent(parent.getContext(), parametres.class);
parent.getContext().startActivity(next);
}
});
Related
I have an ExpandableListView which contains 2 TextViews and an EditText for entering amount for each child and it allows inline edit.
I need to to change the amount value for the group instantly when one of its child's amount changed.
I have done that but the problem is that when I called the notifyDataSetChanged() method for showing the change in group I lost the focus from the current edittext. That is, if I changed the amount field in the first child of a group and then click on the amount field of the second child, the focus is still on the first child because of the notifyDataSetChanged().
How can I solve this?
My code is below.
ExpandableListAdapter.java:
package com.example.empandroid.view;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import com.fort.empandroid.R;
import com.example.empandroid.model.Employee;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Typeface;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseExpandableListAdapter;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private final Context context;
private List<Employee> listDataHeader;
private HashMap<Integer, List<Employee>> listDataChild;
DecimalFormat df = new DecimalFormat("#.00");
int s;
Float value = 0.0f, change = 0.0f;
public ExpandableListAdapter(Context context, List<Employee> listDataHeader, HashMap<Integer, List<Employee>> listChildData) {
this.context = context;
this.listDataHeader = listDataHeader;
this.listDataChild = listChildData;
//this.context=mainContxt.getApplicationContext();
}
public static class ListHolder {
Employee expCoa;
TextView txtName;
TextView txtAmount;
TextView txtMonthTotal;
EditText edtAmount;
}
#Override
public int getGroupCount() {
return this.listDataHeader.size();
}
#Override
public int getChildrenCount(int groupPosition) {
int childcount = this.listDataChild.get(this.listDataHeader.get(groupPosition).getiD()).size();
return childcount;
}
#Override
public Employee getGroup(int groupPosition) {
return this.listDataHeader.get(groupPosition);
}
#Override
public Employee getChild(int groupPosition, int childPosition) {
return this.listDataChild.get(this.listDataHeader.get(groupPosition).getiD()).get(childPosition);
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
df.setMinimumIntegerDigits(1);
ListHolder expHolder = new ListHolder();
expHolder.expCoa = getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
expHolder.txtName = (TextView) convertView.findViewById(R.id.txtAccount);
expHolder.txtAmount = (TextView) convertView.findViewById(R.id.txtAmount);
expHolder.txtMonthTotal = (TextView) convertView.findViewById(R.id.txtMonthTotal);
convertView.setTag(expHolder);
expHolder.txtName.setTypeface(null, Typeface.BOLD);
expHolder.txtAmount.setTypeface(null, Typeface.BOLD);
expHolder.txtMonthTotal.setTypeface(null, Typeface.BOLD);
setupItem(expHolder);
return convertView;
}
#Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
df.setMinimumIntegerDigits(1);
final ListHolder expHolder = new ListHolder();
expHolder.expCoa = getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
expHolder.txtName = (TextView) convertView.findViewById(R.id.txtAccount);
expHolder.edtAmount = (EditText) convertView.findViewById(R.id.edtAmount);
expHolder.txtMonthTotal = (TextView) convertView.findViewById(R.id.txtMonthTotal);
convertView.setTag(expHolder);
setupChildItem(expHolder);
//expHolder.edtAmount.requestFocus();
AlertDialog.Builder builder = new AlertDialog.Builder(context);
final AlertDialog dialog = builder.create();
//AlertDialog.Builder alert = new AlertDialog.Builder(mainContxt);
expHolder.edtAmount.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
//dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
onChange(v, groupPosition, childPosition, expHolder.expCoa);
} else {
//Toast.makeText(context, " else "+s, 68000).show();
onFocus(v);
}
}
});
return convertView;
}
private void onFocus(View v) {
final EditText Caption = (EditText) v;
value = Float.parseFloat(Caption.getText().toString());
Toast.makeText(context, "onFocus value " + value + " change " + change, 68000).show();
}
public void onChange(View views, int groupPosition, int childPosition, Employee expCoaChd) {
final int position = views.getId();
final EditText Caption = (EditText) views;
change = Float.parseFloat(Caption.getText().toString());
if (value != change) { //use database type for this loop condition
//perform change
ListHolder expHolders = new ListHolder();
expHolders.expCoa = getGroup(groupPosition);
expHolders.expCoa.setAmount(expHolders.expCoa.getAmount() - expCoaChd.getAmount() + change);
expCoaChd.setAmount(change);
notifyDataSetChanged();
//Toast.makeText(context, "onChange value "+value+" change "+change, 68000).show();
change = 0.0f;
value = 0.0f;
}
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
private void setupChildItem(ListHolder expHolder) {
expHolder.txtName.setText(expHolder.expCoa.getStrName());
if (expHolder.expCoa.getiD() == -998 || expHolder.expCoa.getiD() == -999) {
expHolder.edtAmount.setText("");
expHolder.txtMonthTotal.setText("");
} else {
expHolder.edtAmount.setText(df.format(expHolder.expCoa.getAmount()));
expHolder.txtMonthTotal.setText(df.format(expHolder.expCoa.getExpenseMTD()));
}
}
private void setupItem(ListHolder expHolder) {
expHolder.txtName.setText(expHolder.expCoa.getStrName());
if (expHolder.expCoa.getiD() == -998 || expHolder.expCoa.getiD() == -999) {
expHolder.txtAmount.setText("");
expHolder.txtMonthTotal.setText("");
} else {
expHolder.txtAmount.setText(df.format(expHolder.expCoa.getAmount()));
expHolder.txtMonthTotal.setText(df.format(expHolder.expCoa.getExpenseMTD()));
}
}
}
list_group.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="match_parent"
android:orientation="horizontal"
>
<TextView
android:id="#+id/txtAccount"
android:layout_width="0dp"
android:layout_height="50dp"
android:textSize="16sp"
android:gravity="center_vertical"
android:layout_weight="3"/>
<TextView
android:id="#+id/txtAmount"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="end"
android:textSize="15sp"
android:textAppearance="?android:attr/textAppearanceSmall"/>
<TextView
android:id="#+id/txtMonthTotal"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="end"
android:textSize="15sp"
android:textAppearance="?android:attr/textAppearanceSmall"/>
</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="match_parent"
android:orientation="horizontal"
>
<TextView
android:id="#+id/txtAccount"
android:layout_width="0dp"
android:layout_height="50dp"
android:textSize="15sp"
android:gravity="center_vertical"
android:layout_weight="3"/>
<EditText
android:id="#+id/edtAmount"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="end"
android:textSize="15sp"
android:textAppearance="?android:attr/textAppearanceSmall"/>
<TextView
android:id="#+id/txtMonthTotal"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="end"
android:textSize="15sp"
android:textAppearance="?android:attr/textAppearanceSmall"/>
</LinearLayout>
Good Day! i am new in android programming and java, so i did a copy paste of a source code in the internet then it worked but i have to do some edit for this.
i wanna ask how to add a function when i click on switch of every group that supposed to be on/off not to toast. i understand how to use onClickListener Somehow but i dont know where to put the function.
any answers or good tutorial link for this will be greatly appreciated THANK YOU!
MainActivity.java
package com.capstone.r.e.d.e_kit;
import java.util.ArrayList;
import android.app.ExpandableListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.ExpandableListView;
public class MainActivity extends ExpandableListActivity{
private ArrayList<String> parentItems = new ArrayList<String>();
private ArrayList<Object> childItems = new ArrayList<Object>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// this is not really necessary as ExpandableListActivity contains an ExpandableList
//setContentView(R.layout.main);
ExpandableListView expandableList = getExpandableListView(); // you can use (ExpandableListView) findViewById(R.id.list)
expandableList.setDividerHeight(2);
expandableList.setGroupIndicator(null);
expandableList.setClickable(true);
setGroupParents();
setChildData();
MainActivityAdapter adapter = new MainActivityAdapter(parentItems, childItems);
adapter.setInflater((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE), this);
expandableList.setAdapter(adapter);
expandableList.setOnChildClickListener(this);
}
public void setGroupParents() {
parentItems.add("Flash Light");
parentItems.add("Whistle");
parentItems.add("Infographics");
parentItems.add("Battery Saving Mode");
}
public void setChildData() {
// Flash Light
ArrayList<String> child = new ArrayList<String>();
child.add("Switch");
childItems.add(child);
// Whistle
child = new ArrayList<String>();
child.add("Switch");
childItems.add(child);
// Information
child = new ArrayList<String>();
child.add("Online News");
child.add("Do's and Dont's");
childItems.add(child);
// Battery Saving Mode
child = new ArrayList<String>();
child.add("Switch");
childItems.add(child);
}
}
MainActivityAdapter.java
package com.capstone.r.e.d.e_kit;
import java.util.ArrayList;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckedTextView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivityAdapter extends BaseExpandableListAdapter {
private Activity activity;
private ArrayList<Object> childtems;
private LayoutInflater inflater;
private ArrayList<String> parentItems, child;
public MainActivityAdapter(ArrayList<String> parents, ArrayList<Object> childern) {
this.parentItems = parents;
this.childtems = childern;
}
public void setInflater(LayoutInflater inflater, Activity activity) {
this.inflater = inflater;
this.activity = activity;
}
#SuppressWarnings("unchecked")
#Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
child = (ArrayList<String>) childtems.get(groupPosition);
TextView textView = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.group, null);
}
textView = (TextView) convertView.findViewById(R.id.textView1);
textView.setText(child.get(childPosition));
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(activity, child.get(childPosition),
Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.row, null);
}
((CheckedTextView) convertView).setText(parentItems.get(groupPosition));
((CheckedTextView) convertView).setChecked(isExpanded);
return convertView;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
#Override
public int getChildrenCount(int groupPosition) {
return ((ArrayList<String>) childtems.get(groupPosition)).size();
}
#Override
public Object getGroup(int groupPosition) {
return null;
}
#Override
public int getGroupCount() {
return parentItems.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 boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ExpandableListView
android:id="#+id/list"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:groupIndicator="#null" />
</LinearLayout>
row.xml
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_marginLeft="5dp"
android:gravity="center_vertical"
android:padding="10dp"
android:textColor="#FFFFFF"
android:textSize="15sp"
android:background="#339966"
/>
group.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="40dp"
android:background="#android:color/white"
android:clickable="true"
android:orientation="vertical"
android:paddingLeft="40dp"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="39dp"
android:gravity="center_vertical">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textIsSelectable="true"
android:textColor="#000000"
android:textSize="14sp"
/>
</LinearLayout>
</LinearLayout>
Try to set setOnGroupClickListener:
adapter.setInflater((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE), this);
expandableList.setAdapter(adapter);
expandableList.setOnChildClickListener(this);
expandableList.setOnGroupClickListener(this); //Implement inteface ExpandableListView.OnGroupClickListener
Documatation:
http://developer.android.com/reference/android/widget/ExpandableListView.html#setOnGroupClickListener(android.widget.ExpandableListView.OnGroupClickListener)
Tutorial:
http://www.vogella.com/tutorials/AndroidListView/article.html#expandablelistview
I'm making an android app for an automatic salad machine. The app lets you choose your ingredients and such, and then sends it to the machine. I set up an activity with an expandable listview to hold the ingredients, but I also need a next button on the activity and a nutritional information box below it. I added them to the activity, but when I run it only the expandable listview shows up.
Here's the code for the activity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="1">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:id="#+id/button"
android:layout_gravity="right" />
<ExpandableListView
android:id="#+id/list"
android:layout_height="286dp"
android:layout_width="match_parent"
android:groupIndicator="#null"
android:divider="#A4C739"
android:dividerHeight="0.5dp"
android:layout_weight="1.15" />
<TextView
android:layout_width="match_parent"
android:layout_height="77dp"
android:text="Nutrition Facts:"
android:id="#+id/nutritionTV" />
</LinearLayout>
and here's the code for the class that goes with it:
package com.picknchew.companionapp;
import java.util.ArrayList;
import android.app.ExpandableListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.ExpandableListView;
public class ChooseIngredientsActivity extends ExpandableListActivity{
private ArrayList<String> parentItems = new ArrayList<String>();
private ArrayList<Object> childItems = new ArrayList<Object>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// this is not really necessary as ExpandableListActivity contains an ExpandableList
//setContentView(R.layout.main);
ExpandableListView expandableList = getExpandableListView(); // you can use (ExpandableListView) findViewById(R.id.list)
expandableList.setDividerHeight(2);
expandableList.setGroupIndicator(null);
expandableList.setClickable(true);
setGroupParents();
setChildData();
MyExpandableAdapter adapter = new MyExpandableAdapter(parentItems, childItems);
adapter.setInflater((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE), this);
expandableList.setAdapter(adapter);
expandableList.setOnChildClickListener(this);
}
public void setGroupParents() {
parentItems.add("Lettuce");
parentItems.add("Protein");
parentItems.add("Fruit");
parentItems.add("Dressings");
}
public void setChildData() {
// Android
ArrayList<String> child = new ArrayList<String>();
child.add("Romain");
child.add("Iceberg");
child.add("Arugula");
child.add("Green");
childItems.add(child);
// Core Java
child = new ArrayList<String>();
child.add("Tofu");
child.add("Shredded Cheddar");
child.add("Sliced Eggs");
child.add("Beans");
child.add("Avocado");
childItems.add(child);
// Desktop Java
child = new ArrayList<String>();
child.add("Melon");
child.add("Watermelon");
child.add("Pineapple");
child.add("Grapefruit");
childItems.add(child);
// Enterprise Java
child = new ArrayList<String>();
child.add("Honey Mustard");
child.add("Ranch");
child.add("Caesar");
child.add("Vinaigrette");
childItems.add(child);
}
}
and finally here's the code for the expandable list view adapter, in case that's relevant:
package com.picknchew.companionapp;
import java.util.ArrayList;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.CheckedTextView;
import android.widget.TextView;
import android.widget.Toast;
public class MyExpandableAdapter extends BaseExpandableListAdapter {
private Activity activity;
private ArrayList<Object> childtems;
private LayoutInflater inflater;
private ArrayList<String> parentItems, child;
public MyExpandableAdapter(ArrayList<String> parents, ArrayList<Object> childern) {
this.parentItems = parents;
this.childtems = childern;
}
public void setInflater(LayoutInflater inflater, Activity activity) {
this.inflater = inflater;
this.activity = activity;
}
#Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
child = (ArrayList<String>) childtems.get(groupPosition);
CheckBox checkbox = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.group, null);
}
checkbox = (CheckBox) convertView.findViewById(R.id.checkBox2);
checkbox.setText(child.get(childPosition));
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(activity, child.get(childPosition),
Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.child, null);
}
((CheckedTextView) convertView).setText(parentItems.get(groupPosition));
((CheckedTextView) convertView).setChecked(isExpanded);
return convertView;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
#Override
public int getChildrenCount(int groupPosition) {
return ((ArrayList<String>) childtems.get(groupPosition)).size();
}
#Override
public Object getGroup(int groupPosition) {
return null;
}
#Override
public int getGroupCount() {
return parentItems.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 boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
Mixing fixed heights with weights won't work.
What you can do: set all elements' height to 0 and assign them weights.
What I would do: use a RelativeLayout, put the Button at the top, the TextView at the bottom (alignParentBottom="true") and the ListView inbetween like
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:id="#+id/button" />
<TextView
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="77dp"
android:text="Nutrition Facts:"
android:id="#+id/nutritionTV" />
<ExpandableListView
android:layout_above="#+id/nutritionTV"
android:layout_below="#+id/button"
android:id="#+id/list"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:groupIndicator="#null"
android:divider="#A4C739"
android:dividerHeight="0.5dp" />
</RelativeLayout>
Result:
EDIT: You have to actually use this layout, which you at the moment don't do:
// this is not really necessary as ExpandableListActivity contains an ExpandableList
//setContentView(R.layout.main);
Uncomment the setContentView line. Don't forget to assign the views correctly, ie
ExpandableListView expandableList = getExpandableListView(); // you can use (ExpandableListView) findViewById(R.id.list)
will become
ExpandableListView expandableList = (ExpandableListView) findViewById(R.id.list)
I have gone through many tutorials and implemented expandablelistview with one textview as a child of groupview. Now i need to implement expandablelistview with 6-7 textviews and an imageview as childs of every group.
Can anyone help..
Sorry for poor painting
https://copy.com/IdpK0BLDUHck
please follow this code
import java.util.ArrayList;
import android.app.Activity;
import android.app.ExpandableListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckedTextView;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.parsedemoapp.ParseMainActivity;
import com.rstm.quizappsforiit.R;
public class ExpandableListMainActivity extends ExpandableListActivity {
// Create ArrayList to hold parent Items and Child Items
private ArrayList<String> parentItems = new ArrayList<String>();
private ArrayList<Object> childItems = new ArrayList<Object>();
private ArrayList<Integer> childimage = new ArrayList<Integer>();
ImageView imageView2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create Expandable List and set it's properties
ExpandableListView expandableList = getExpandableListView();
expandableList.setDividerHeight(2);
expandableList.setGroupIndicator(null);
expandableList.setClickable(true);
// Set the Items of Parent
setGroupParents();
// Set The Child Data
setChildData();
// Create the Adapter
MyExpandableAdapter adapter = new MyExpandableAdapter(parentItems,
childItems, childimage);
adapter.setInflater(
(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE),
this);
// Set the Adapter to expandableList
expandableList.setAdapter(adapter);
expandableList.setOnChildClickListener(this);
}
// method to add parent Items
public void setGroupParents() {
parentItems.add("Chemistry");
parentItems.add("Mathematics");
parentItems.add("Physics");
}
// method to set child data of each parent
public void setChildData() {
// Add Child Items for Fruits
ArrayList<String> child = new ArrayList<String>();
child.add("Some Basic Concepts");
child.add("States of Matter");
child.add("Atomic Structure");
child.add("Solutions");
child.add("Chemical Energetics and Thermodynamics");
child.add("Chemical and Ionic Equilibria");
child.add("Redox Reactions and Electrochemistry");
childItems.add(child);
childimage.add(R.drawable.buy1);
childItems.add(childimage);
// Add Child Items for Animals
child = new ArrayList<String>();
child.add("Physics and Measurement");
child.add("Kinematics");
child.add("Laws of Motion");
child.add("Work, Energy and Power");
child.add("Rotational Motion");
child.add("Gravitation");
child.add("Solids and Fluids");
childItems.add(child);
childItems.add(childimage);
}
public class MyExpandableAdapter extends BaseExpandableListAdapter {
private Activity activity;
private ArrayList<Object> childtems;
private ArrayList<Integer> childimage;
private LayoutInflater inflater;
private ArrayList<String> parentItems, child;
public MyExpandableAdapter(ArrayList<String> parents,
ArrayList<Object> childern, ArrayList<Integer> childimage) {
this.parentItems = parents;
this.childtems = childern;
this.childimage = childimage;
}
public void setInflater(LayoutInflater inflater, Activity activity) {
this.inflater = inflater;
this.activity = activity;
}
#SuppressWarnings("unchecked")
#Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
child = (ArrayList<String>) childtems.get(groupPosition);
TextView textView = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.child_view, null);
}
textView = (TextView) convertView.findViewById(R.id.textViewChild);
String a = child.get(childPosition);
textView.setText(child.get(childPosition));
imageView2 = (ImageView) convertView.findViewById(R.id.imageView2);
imageView2.setBackgroundResource(childimage.get(childPosition));
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
}
});
return convertView;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.parent_view, null);
}
((CheckedTextView) convertView).setText(parentItems
.get(groupPosition));
((CheckedTextView) convertView).setChecked(isExpanded);
return convertView;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
#SuppressWarnings("unchecked")
#Override
public int getChildrenCount(int groupPosition) {
return ((ArrayList<String>) childtems.get(groupPosition)).size();
}
#Override
public Object getGroup(int groupPosition) {
return null;
}
#Override
public int getGroupCount() {
return parentItems.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 boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
}
Source Code
https://drive.google.com/open?id=0BzBKpZ4nzNzUVkExUDlqRFhtcGc
package com.keshav.expandablelistviewindrawerkeshav;
import android.os.Bundle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class DrawerActivityExpandable extends AppCompatActivity
{
Toolbar toolbar;
private DrawerLayout drawer;
private ExpandableListView drawerList;
private ActionBarDrawerToggle actionBarDrawerToggle;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
ImageView profile_image;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer_expandable);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setTitle("ABC");
// getSupportActionBar().setDisplayShowHomeEnabled(true);
initDrawer();
// Listview Group click listener
drawerList.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
drawerList.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
// TODO Colapse Here Using this... in android
int previousGroup = -1;
boolean flag = false;
#Override
public void onGroupExpand(int groupPosition) {
Log.e("keshav", "onGroupClick is -> " + groupPosition);
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();
if (groupPosition != previousGroup && flag) {
drawerList.collapseGroup(previousGroup);
}
previousGroup = groupPosition;
flag = true;
if(groupPosition==0 ||groupPosition==1||groupPosition==4){
drawer.closeDrawer(drawerList);
toolbar.setTitle(""+listDataHeader.get(groupPosition));
}
}
});
// Listview Group collasped listener
drawerList.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();
}
});
// Todo Listview on child click listener
drawerList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(
getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : keshav : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
Log.e("keshav","Child Data 1->"+listDataHeader.get(groupPosition));
Log.e("keshav","Child Data 2->"+listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition));
toolbar.setTitle(""+listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition));
// if(groupPosition==2 ||groupPosition==3)
drawer.closeDrawer(drawerList);
return false;
}
});
profile_image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Log.e("Keshav", "Clck Profile Image");
/*if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int permissionCheck = ContextCompat.checkSelfPermission(DrawerActivity.this,
Manifest.permission.CAMERA);
if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
//showing dialog to select image
selectImage();
Log.e("permission", "granted");
} else {
ActivityCompat.requestPermissions(DrawerActivity.this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.CAMERA}, 1);
}
} else {
selectImage();
}*/
}
});
}
private void initDrawer() {
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerList = (ExpandableListView) findViewById(R.id.left_drawer);
View header = getLayoutInflater().inflate(R.layout.nav_header_main, null);
drawerList.addHeaderView(header);
drawer.setDrawerShadow(R.mipmap.drawer_shadow, GravityCompat.START);
profile_image = (ImageView) header.findViewById(R.id.user_profile_image);
profile_image.setBackgroundResource(R.mipmap.keshav);
// getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// getSupportActionBar().setHomeButtonEnabled(true);
// preparing list data
prepareListData();
drawerList.setAdapter(new NavigationDrawerExpandableAdapter(this, listDataHeader, listDataChild));
// drawerList.setOnChildClickListener(this);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.drawer_open, R.string.drawer_close) {
#Override
public void onDrawerClosed(View drawerView) {
// Code here will be triggered once the drawer closes as we don't want anything to happen so we leave this blank
super.onDrawerClosed(drawerView);
}
#Override
public void onDrawerOpened(View drawerView) {
// Code here will be triggered once the drawer open as we don't want anything to happen so we leave this blank
super.onDrawerOpened(drawerView);
}
};
//Setting the actionbarToggle to drawer layout
drawer.setDrawerListener(actionBarDrawerToggle);
//calling sync state is necessay or else your hamburger icon wont show up
actionBarDrawerToggle.syncState();
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("Dashboard");
listDataHeader.add("Enter Manually");
listDataHeader.add("Profile");
listDataHeader.add("Reports");
listDataHeader.add("Logout");
// TODO Adding child data
List<String> nowShowing = new ArrayList<String>();
nowShowing.add("My Profile");
nowShowing.add("Change Password");
List<String> blank = new ArrayList<String>();
List<String> report = new ArrayList<String>();
report.add("Batch Wise Report");
report.add("Location Wise Report");
report.add("Date Wise Report");
listDataChild.put(listDataHeader.get(0), blank);
listDataChild.put(listDataHeader.get(1), blank); // Header, Child data
listDataChild.put(listDataHeader.get(2), nowShowing);
listDataChild.put(listDataHeader.get(3), report);
listDataChild.put(listDataHeader.get(4), blank);
}
}
-------------------------------
Adapter
------------------------------
package com.keshav.expandablelistviewindrawerkeshav;
import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.HashMap;
import java.util.List;
/**
* Created by root on 11/12/15.
*/
public class NavigationDrawerExpandableAdapter extends BaseExpandableListAdapter {
public LayoutInflater minflater;
public Activity activity;
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 NavigationDrawerExpandableAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
public void setInflater(LayoutInflater mInflater, Activity act) {
this.minflater = mInflater;
activity = act;
}
#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);
//TODO set Hard Code Child item
ImageView iv_ListChild = (ImageView) convertView
.findViewById(R.id.iv_ListChild);
txtListChild.setText(childText);
Log.e("Keshav", "childText 1 -> " + childText);
if (childText.equals("My Profile")){
iv_ListChild.setBackgroundResource(R.mipmap.youtuben);
}else if (childText.equals("Change Password")){
iv_ListChild.setBackgroundResource(R.mipmap.facebook);
}else if (childText.equals("Batch Wise Report")){
iv_ListChild.setBackgroundResource(R.mipmap.googleplus);
}else if (childText.equals("Location Wise Report")){
iv_ListChild.setBackgroundResource(R.mipmap.linkedin);
}else if (childText.equals("Date Wise Report")){
iv_ListChild.setBackgroundResource(R.mipmap.twitter);
}else {
iv_ListChild.setBackgroundResource(R.mipmap.ic_launcher_round);
}
//TODO set Hard Code Child item
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 void onGroupCollapsed(int groupPosition) {
super.onGroupCollapsed(groupPosition);
}
#Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(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.NORMAL); // TODO Set Text Color BOLD Here
lblListHeader.setText(headerTitle);
Log.e("keshav","groupPosition "+groupPosition);
ImageView iv_ListHeader = (ImageView) convertView
.findViewById(R.id.iv_ListHeader);
if(groupPosition==0)
iv_ListHeader.setBackgroundResource(R.mipmap.changepassword_48);
else if(groupPosition==1)
iv_ListHeader.setBackgroundResource(R.mipmap.barcode_48);
else if(groupPosition==2)
iv_ListHeader.setBackgroundResource(R.mipmap.changepassword_48);
else if(groupPosition==3)
iv_ListHeader.setBackgroundResource(R.mipmap.enter_manualy_48);
else
iv_ListHeader.setBackgroundResource(R.mipmap.ic_launcher);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
------------------------------------
activity_drawer.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">
<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>
<FrameLayout
android:id="#+id/fllContent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
<ExpandableListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:groupIndicator="#null"
android:dividerHeight="1dp"
android:divider="#color/list_divider"
android:listSelector="#drawable/list_selector"
android:background="#color/list_background" />
<!--android:groupIndicator="#null"-->
</android.support.v4.widget.DrawerLayout>
------------------------------------
listgroup.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="40dp"
android:orientation="horizontal"
android:background="#drawable/list_selector">
<ImageView
android:id="#+id/iv_ListHeader"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginBottom="10dp"
android:background="#mipmap/ic_launcher" />
<TextView
android:id="#+id/lblListHeader"
style="#style/TextViewSmallBlack"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:layout_marginLeft="10dp"
android:layout_marginBottom="10dp"
android:gravity="left"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:text="list group"
android:textSize="14sp"
/>
</LinearLayout>
------------------------------------
listitem.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="40dp"
android:orientation="horizontal"
android:background="#drawable/list_selector">
<ImageView
android:id="#+id/iv_ListChild"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="40dp"
android:layout_marginBottom="10dp"
android:background="#mipmap/ic_launcher" />
<TextView
android:id="#+id/lblListItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14dip"
style="#style/TextViewSmallBlack"
android:layout_marginTop="5dp"
android:layout_marginLeft="10dp"
android:gravity="left"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:text="list group"
/>
</LinearLayout>
------------------------------------
navheader.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="190dp"
android:gravity="start"
android:background="#color/colorPrimary"
android:orientation="vertical"><!--android:background="#color/com_facebook_button_background_color_disabled"-->
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/user_profile_image"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginLeft="20dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="40dp"
app:border_color="#color/click_tick" />
<TextView
android:id="#+id/userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/user_profile_image"
android:layout_centerHorizontal="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:gravity="left"
android:text="Keshav Gera"
android:textColor="#color/white"
android:textSize="16sp"
android:textStyle="bold" /> <!--android:textSize="14sp"-->
<TextView
android:id="#+id/usertype"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:gravity="left"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="5dp"
android:textSize="10sp"
android:textColor="#fff"
android:layout_alignParentBottom="true"
android:layout_alignLeft="#+id/userName"
android:layout_alignStart="#+id/userName" /> <!--android:textSize="14sp"-->
</LinearLayout>
<!--
<?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="#dimen/nav_header_height"
android:background="#drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:src="#android:drawable/sym_def_app_icon" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:text="Android Studio"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android.studio#android.com" />
</LinearLayout>
-->
------------------------------------
toolbar.xml
------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
local:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
local:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
when click button in child view then update imageview in group.
The problem is when I click the button, if all the checkbox selected,the imageview show the last one, not all checkbox selected, the imageview show the second one else show the first one!
The question is when I click the button,the image view cannot update in the correct position!
package cn.nedu.exlistview;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
class AccountExpandablebaseadapter extends BaseExpandableListAdapter {
private Resources mResource;
private Context context;
List<String> accountFather = new ArrayList<String>();
List<List<Map<String, String>>> accountChild = new ArrayList<List<Map<String, String>>>();
List<View> cachedGroupView = new ArrayList<View>();
public AccountExpandablebaseadapter(Context context,
List<String> accountFathers,
List<List<Map<String, String>>> accountChilds,Resources mResouce) {
this.accountFather = accountFathers;
this.accountChild = accountChilds;
this.mResource = mResouce;
this.context = context;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
GroupViewHolder viewHolder = null;
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.member_listview, null);
viewHolder = new GroupViewHolder();
viewHolder.groupTitle = (TextView) view.findViewById(R.id.txtFather);
/*customize indicator view*/
viewHolder.groupStatus = (ImageView) view.findViewById(R.id.status);
//RelativeLayout tabLayout = (RelativeLayout)view.findViewById();
//mResource.getDrawable(R.drawable.indicator_close);
view.setTag(viewHolder);
}else{
viewHolder = (GroupViewHolder) view.getTag();
}
cachedGroupView.add(view); //cached group view
viewHolder.groupTitle.setText(getGroup(groupPosition).toString());
Drawable indicator_close = mResource.getDrawable(R.drawable.indicator_close);
indicator_close.setBounds(0, 0, indicator_close.getMinimumWidth(), indicator_close.getMinimumHeight());
Drawable indicator_open = mResource.getDrawable(R.drawable.indicator_open);
indicator_open.setBounds(0, 0, indicator_open.getMinimumWidth(), indicator_open.getMinimumHeight());
if (isExpanded) {
//imageIndicator.setImageResource(R.drawable.indicator_close);
viewHolder.groupTitle.setCompoundDrawables(indicator_close, null, null, null);
viewHolder.groupTitle.setBackgroundColor(R.color.text_color_normal);
viewHolder.groupTitle.setTextColor(mResource.getColor(R.color.text_color_click));
}else{
//imageIndicator.setImageResource(R.drawable.indicator_open);
viewHolder.groupTitle.setCompoundDrawables(indicator_open, null, null, null);
viewHolder.groupTitle.setBackgroundColor(Color.TRANSPARENT);
viewHolder.groupTitle.setTextColor(mResource.getColor(R.color.text_color_normal));
}
return view;
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public Object getGroup(int groupPosition) {
return accountFather.get(groupPosition).toString();
}
public int getGroupCount() {
return accountFather.size();
}
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
View view = convertView;
ChildViewHolder viewHolder = null;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.member_childitem2, null);
viewHolder = new ChildViewHolder();
viewHolder.checkBox1 = (CheckBox)view.findViewById(R.id.checkbox1);
viewHolder.checkBox2 = (CheckBox)view.findViewById(R.id.checkbox2);
viewHolder.checkBox3 = (CheckBox)view.findViewById(R.id.checkbox3);
viewHolder.checkBox4 = (CheckBox)view.findViewById(R.id.checkbox4);
viewHolder.checkBox5 = (CheckBox)view.findViewById(R.id.checkbox5);
viewHolder.checkBox6 = (CheckBox)view.findViewById(R.id.checkbox6);
view.setTag(viewHolder);
}else{
viewHolder = (ChildViewHolder) view.getTag();
}
Button btn_submit = (Button)view.findViewById(R.id.btn_submit);
View groupView = cachedGroupView.get(childPosition);
final GroupViewHolder groupViewHolder = (GroupViewHolder) groupView.getTag();
btn_submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context,
groupPosition+ ":"
+ childPosition,
Toast.LENGTH_SHORT).show();
groupViewHolder.groupStatus.setImageResource(R.drawable.indicator_close);
}
});
return view;
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public Object getChild(int groupPosition, int childPosition) {
return accountChild.get(groupPosition).get(childPosition)
.get(ExListView.FATHERACCOUNT).toString();
}
public int getChildrenCount(int groupPosition) {
return accountChild.get(groupPosition).size();
}
public boolean hasStableIds() {
return true;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
class GroupViewHolder{
TextView groupTitle;
ImageView groupStatus;
}
class ChildViewHolder{
CheckBox checkBox1;
CheckBox checkBox2;
CheckBox checkBox3;
CheckBox checkBox4;
CheckBox checkBox5;
CheckBox checkBox6;
}
}
package cn.nedu.exlistview;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ExpandableListView.OnChildClickListener;
public class ExListView extends Activity {
final static String FATHERACCOUNT = "fatherAccount";
final static String CHILDACCOUNT = "childAccount";
final static String BALANCEACCOUNT = "accountBalance";
private int currentHourPosition = -1;
List<String> accountFather = new ArrayList<String>();
List<List<Map<String, String>>> accountChild = new ArrayList<List<Map<String, String>>>();
AccountExpandablebaseadapter adapter;
ExpandableListView listAccount;
private Resources mResouce;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// StriceMode
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectAll().penaltyLog().penaltyDialog().build());
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
mResouce = getResources();
listAccount = (ExpandableListView) findViewById(R.id.listAccount);
accountFather.add("GroupName1");
accountFather.add("GroupName2");
accountFather.add("GroupName3");
List<Map<String, String>> financeAccount = new ArrayList<Map<String, String>>();
Map<String, String> child1Data1 = new HashMap<String, String>();
child1Data1.put(FATHERACCOUNT, "wwwwww)");
child1Data1.put(CHILDACCOUNT, "wwwwww");
child1Data1.put(BALANCEACCOUNT, "¥0.00");
financeAccount.add(child1Data1);
List<Map<String, String>> dummyAccount = new ArrayList<Map<String, String>>();
Map<String, String> child2Data1 = new HashMap<String, String>();
child2Data1.put(FATHERACCOUNT, "zzzzz");
child2Data1.put(CHILDACCOUNT, "zzzzzz");
child2Data1.put(BALANCEACCOUNT, "¥0.00");
dummyAccount.add(child2Data1);
List<Map<String, String>> cashAccount = new ArrayList<Map<String, String>>();
Map<String, String> child3Data1 = new HashMap<String, String>();
child3Data1.put(FATHERACCOUNT, "xxxxxx");
child3Data1.put(CHILDACCOUNT, "eeeeeee");
child3Data1.put(BALANCEACCOUNT, "¥0.00");
cashAccount.add(child3Data1);
List<Map<String, String>> creditorAccount = new ArrayList<Map<String, String>>();
Map<String, String> child4Data1 = new HashMap<String, String>();
List<Map<String, String>> owesAccount = new ArrayList<Map<String, String>>();
Map<String, String> child5Data1 = new HashMap<String, String>();
accountChild.add(financeAccount);
accountChild.add(dummyAccount);
accountChild.add(cashAccount);
adapter = new AccountExpandablebaseadapter(ExListView.this,
accountFather, accountChild, mResouce);
listAccount.setAdapter(adapter);
listAccount.setGroupIndicator(null);
listAccount.setDivider(null);
listAccount.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
/*
* Toast.makeText( getBaseContext(),
* String.valueOf(groupPosition) + ":" +
* String.valueOf(childPosition), Toast.LENGTH_SHORT).show();
*/
return false;
}
});
//
listAccount.setOnGroupClickListener(new OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
return false;
}
});
listAccount.setOnGroupExpandListener(new OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
if (currentHourPosition != -1
&& currentHourPosition != groupPosition) {
listAccount.collapseGroup(currentHourPosition);
}
currentHourPosition = groupPosition;
listAccount.setSelectedGroup(groupPosition);
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#003300">
<ExpandableListView
android:id="#+id/listAccount"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:groupIndicator="#null"
android:scrollbars="none"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="#+id/tabLayout">
<ImageView
android:id="#+id/indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/app_name"
android:visibility="gone" />
<TextView
android:id="#+id/txtFather"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="6dp"
android:layout_toRightOf="#id/indicator"
android:textColor="#000000"
android:textSize="16dp"
/>
</RelativeLayout>
<ImageView
android:id="#+id/status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:contentDescription="#string/app_name"
android:src="#drawable/icon" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="vertical" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/checkbox1"
android:text="Checkbox1"
android:button="#drawable/checkbox_bg"
android:textColor="#000000" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/checkbox2"
android:text="Checkbox2"
android:button="#drawable/checkbox_bg"
android:textColor="#000000" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/checkbox3"
android:text="Checkbox3"
android:textColor="#000000"
android:button="#drawable/checkbox_bg" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:id="#+id/checkbox4"
android:text="Checkbox4"
android:button="#drawable/checkbox_bg" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:id="#+id/checkbox5"
android:text="Checkbox5"
android:button="#drawable/checkbox_bg" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:id="#+id/checkbox6"
android:text="Checkbox6"
android:button="#drawable/checkbox_bg" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Take Picture" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:id="#+id/btn_submit" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:id="#+id/btn_cancel" />
</LinearLayout>
</LinearLayout>
Extend this class as your Activity and it will handle saving and restoring position and expansion state.
import java.util.ArrayList;
import java.util.List;
import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
public class PersistentExpandableListActivity extends ExpandableListActivity {
private long[] expandedIds;
public PersistentExpandableListActivity() {
super();
}
#Override
protected void onStart() {
super.onStart();
if (this.expandedIds != null) {
restoreExpandedState(expandedIds);
}
}
#Override
protected void onStop() {
super.onStop();
expandedIds = getExpandedIds();
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
this.expandedIds = getExpandedIds();
outState.putLongArray("ExpandedIds", this.expandedIds);
}
#Override
protected void onRestoreInstanceState(Bundle state) {
super.onRestoreInstanceState(state);
long[] expandedIds = state.getLongArray("ExpandedIds");
if (expandedIds != null) {
restoreExpandedState(expandedIds);
}
}
private long[] getExpandedIds() {
ExpandableListView list = getExpandableListView();
ExpandableListAdapter adapter = getExpandableListAdapter();
if (adapter != null) {
int length = adapter.getGroupCount();
ArrayList<Long> expandedIds = new ArrayList<Long>();
for(int i=0; i < length; i++) {
if(list.isGroupExpanded(i)) {
expandedIds.add(adapter.getGroupId(i));
}
}
return toLongArray(expandedIds);
} else {
return null;
}
}
private void restoreExpandedState(long[] expandedIds) {
this.expandedIds = expandedIds;
if (expandedIds != null) {
ExpandableListView list = getExpandableListView();
ExpandableListAdapter adapter = getExpandableListAdapter();
if (adapter != null) {
for (int i=0; i<adapter.getGroupCount(); i++) {
long id = adapter.getGroupId(i);
if (inArray(expandedIds, id)) list.expandGroup(i);
}
}
}
}
private static boolean inArray(long[] array, long element) {
for (long l : array) {
if (l == element) {
return true;
}
}
return false;
}
private static long[] toLongArray(List<Long> list) {
long[] ret = new long[list.size()];
int i = 0;
for (Long e : list)
ret[i++] = e.longValue();
return ret;
}
}