package com.example.expandablelistviewtest;import java.util.ArrayList;
import java.util.List;import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;public class MainActivity extends Activity { List<Bean> PList;
List<List<String>> CList;
private ExpandableListView mList = null;
ExpandableListAdapter adapter = null;
class Bean{
private int num =0;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PList = new ArrayList<Bean>();
CList = new ArrayList<List<String>>();
for(int i =0;i<30;i++){
Bean bean = new Bean();
PList.add(bean);
List<String> a= new ArrayList<String>();
a.add("1");
CList.add(a);
}
adapter = new ExpandableListAdapter(this, PList, CList);
mList = (ExpandableListView)findViewById(R.id.listview);
mList.setAdapter(adapter);
mList.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
// TODO Auto-generated method stub
for (int i = 0; i < adapter.getGroupCount(); i++) {
if (groupPosition != i) {
mList.collapseGroup(i);
}
}
}
}); } @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
class ExpandableListAdapter extends BaseExpandableListAdapter{
private Context mContext = null;
private List<Bean>PList= null;
private List<List<String>>CList = null;
private LayoutInflater mInflater = null;
private boolean inEditOne = false;
public ExpandableListAdapter(Context mContext,List<Bean>PList,List<List<String>>CList){
this.mContext = mContext;
mInflater = LayoutInflater.from(mContext);
this.PList = PList;
this.CList = CList;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return CList.get(groupPosition).get(childPosition);
} @Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return CList.get(groupPosition).size();
} @Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return PList.get(groupPosition);
} @Override
public int getGroupCount() {
// TODO Auto-generated method stub
return PList.size();
} @Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
} @Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
} @Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
class ParentHolder{
private ImageView img = null;
private TextView txt1 =null;
private TextView txt2 =null;
private TextView txt3 =null;
}
class ChildHolder{
private Button btn1 =null;
private Button btn2 =null;
private Button btn3 =null;
private EditText edit1 = null;
private EditText edit2 = null;
}
@Override
public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView,ViewGroup parent) {
final ChildHolder cHolder;
if(convertView == null){
cHolder = new ChildHolder();
convertView = mInflater.inflate(R.layout.item_childs, null);
cHolder.btn1 = (Button)convertView.findViewById(R.id.btn1);
cHolder.btn2 = (Button)convertView.findViewById(R.id.btn2);
cHolder.btn3 = (Button)convertView.findViewById(R.id.btn3);
cHolder.edit1 = (EditText)convertView.findViewById(R.id.edit1);
cHolder.edit2 = (EditText)convertView.findViewById(R.id.edit2);
convertView.setTag(cHolder);
}else{
cHolder = (ChildHolder)convertView.getTag();
}
cHolder.btn1.setFocusable(false);
cHolder.btn2.setFocusable(false);
cHolder.btn3.setFocusable(false);
// switch(PList.get(groupPosition).getNum()){
// case 0:
// cHolder.edit1.setFocusable(false);
// cHolder.edit1.setFocusableInTouchMode(false);
// cHolder.edit2.setFocusable(false);
// cHolder.edit2.setFocusableInTouchMode(false);
// break;
// case 1:
// cHolder.edit2.setFocusable(false);
// cHolder.edit2.setFocusableInTouchMode(false);
// cHolder.edit1.setFocusable(true);
// cHolder.edit1.requestFocus();
// cHolder.edit1.setFocusableInTouchMode(true);
// cHolder.edit1.requestFocusFromTouch();
// break;
// case 2:
// cHolder.edit1.setFocusable(false);
// cHolder.edit1.setFocusableInTouchMode(false);
// cHolder.edit2.setFocusable(true);
// cHolder.edit2.requestFocus();
// cHolder.edit2.setFocusableInTouchMode(true);
// cHolder.edit2.requestFocusFromTouch();
// break;
// }
cHolder.edit1.setText(CList.get(groupPosition).get(childPosition));
cHolder.btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
int num = Integer.parseInt(cHolder.edit1.getText().toString());
num--;
cHolder.edit1.setText(num+"");
}
});
cHolder.btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int num = Integer.parseInt(cHolder.edit1.getText().toString());
num++;
cHolder.edit1.setText(num+"");
}
});
cHolder.btn3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext, "提交成功!!!", Toast.LENGTH_SHORT).show();
}
});
// cHolder.edit1.setOnTouchListener(new OnTouchListener() {
// @Override
// public boolean onTouch(View arg0, MotionEvent arg1) {
// inEditOne = true;
// PList.get(groupPosition).setNum(1);
// cHolder.edit2.setFocusable(false);
// cHolder.edit2.setFocusableInTouchMode(false);
// cHolder.edit1.setFocusable(true);
// cHolder.edit1.requestFocus();
// cHolder.edit1.setFocusableInTouchMode(true);
// cHolder.edit1.requestFocusFromTouch();
// InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
// imm.showSoftInput(cHolder.edit1, 0);
// return false;
// }
// });
// cHolder.edit1.setOnFocusChangeListener(new OnFocusChangeListener() {
// @Override
// public void onFocusChange(View v, boolean hasFocus) {
// if(!hasFocus&&inEditOne){
// cHolder.edit1.requestFocus();
// }
//
// }
// });
// cHolder.edit2.setOnFocusChangeListener(new OnFocusChangeListener() {
// @Override
// public void onFocusChange(View v, boolean hasFocus) {
// if(!hasFocus&&!inEditOne){
// cHolder.edit2.requestFocus();
// }
//
// }
// });
// cHolder.edit2.setOnTouchListener(new OnTouchListener() {
// @Override
// public boolean onTouch(View arg0, MotionEvent arg1) {
// inEditOne = false;
// PList.get(groupPosition).setNum(2);
// cHolder.edit1.setFocusable(false);
// cHolder.edit1.setFocusableInTouchMode(false);
// cHolder.edit2.setFocusable(true);
// cHolder.edit2.requestFocus();
// cHolder.edit2.setFocusableInTouchMode(true);
// cHolder.edit2.requestFocusFromTouch();
// InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
// imm.showSoftInput(cHolder.edit2, 0);
// return false;
// }
// });
// cHolder.edit1.setFocusable(false);
// cHolder.edit2.setFocusable(false);
return convertView;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,ViewGroup parent) {
ParentHolder pHolder;
if(convertView == null){
pHolder = new ParentHolder();
convertView =mInflater.inflate(R.layout.expandable_item,null);
pHolder.img = (ImageView)convertView.findViewById(R.id.img);
pHolder.txt1 = (TextView)convertView.findViewById(R.id.txt1);
pHolder.txt2 = (TextView)convertView.findViewById(R.id.txt2);
pHolder.txt3 = (TextView)convertView.findViewById(R.id.txt3);
convertView.setTag(pHolder);
}else{
pHolder = (ParentHolder)convertView.getTag();
}
pHolder.img.setFocusable(false);
pHolder.txt1.setFocusable(false);
pHolder.txt2.setFocusable(false);
pHolder.txt3.setFocusable(false);
pHolder.txt1.setText("父类商品"+groupPosition);
return convertView;
}
}
}
import java.util.List;import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;public class MainActivity extends Activity { List<Bean> PList;
List<List<String>> CList;
private ExpandableListView mList = null;
ExpandableListAdapter adapter = null;
class Bean{
private int num =0;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PList = new ArrayList<Bean>();
CList = new ArrayList<List<String>>();
for(int i =0;i<30;i++){
Bean bean = new Bean();
PList.add(bean);
List<String> a= new ArrayList<String>();
a.add("1");
CList.add(a);
}
adapter = new ExpandableListAdapter(this, PList, CList);
mList = (ExpandableListView)findViewById(R.id.listview);
mList.setAdapter(adapter);
mList.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
// TODO Auto-generated method stub
for (int i = 0; i < adapter.getGroupCount(); i++) {
if (groupPosition != i) {
mList.collapseGroup(i);
}
}
}
}); } @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
class ExpandableListAdapter extends BaseExpandableListAdapter{
private Context mContext = null;
private List<Bean>PList= null;
private List<List<String>>CList = null;
private LayoutInflater mInflater = null;
private boolean inEditOne = false;
public ExpandableListAdapter(Context mContext,List<Bean>PList,List<List<String>>CList){
this.mContext = mContext;
mInflater = LayoutInflater.from(mContext);
this.PList = PList;
this.CList = CList;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return CList.get(groupPosition).get(childPosition);
} @Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return CList.get(groupPosition).size();
} @Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return PList.get(groupPosition);
} @Override
public int getGroupCount() {
// TODO Auto-generated method stub
return PList.size();
} @Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
} @Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
} @Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
class ParentHolder{
private ImageView img = null;
private TextView txt1 =null;
private TextView txt2 =null;
private TextView txt3 =null;
}
class ChildHolder{
private Button btn1 =null;
private Button btn2 =null;
private Button btn3 =null;
private EditText edit1 = null;
private EditText edit2 = null;
}
@Override
public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView,ViewGroup parent) {
final ChildHolder cHolder;
if(convertView == null){
cHolder = new ChildHolder();
convertView = mInflater.inflate(R.layout.item_childs, null);
cHolder.btn1 = (Button)convertView.findViewById(R.id.btn1);
cHolder.btn2 = (Button)convertView.findViewById(R.id.btn2);
cHolder.btn3 = (Button)convertView.findViewById(R.id.btn3);
cHolder.edit1 = (EditText)convertView.findViewById(R.id.edit1);
cHolder.edit2 = (EditText)convertView.findViewById(R.id.edit2);
convertView.setTag(cHolder);
}else{
cHolder = (ChildHolder)convertView.getTag();
}
cHolder.btn1.setFocusable(false);
cHolder.btn2.setFocusable(false);
cHolder.btn3.setFocusable(false);
// switch(PList.get(groupPosition).getNum()){
// case 0:
// cHolder.edit1.setFocusable(false);
// cHolder.edit1.setFocusableInTouchMode(false);
// cHolder.edit2.setFocusable(false);
// cHolder.edit2.setFocusableInTouchMode(false);
// break;
// case 1:
// cHolder.edit2.setFocusable(false);
// cHolder.edit2.setFocusableInTouchMode(false);
// cHolder.edit1.setFocusable(true);
// cHolder.edit1.requestFocus();
// cHolder.edit1.setFocusableInTouchMode(true);
// cHolder.edit1.requestFocusFromTouch();
// break;
// case 2:
// cHolder.edit1.setFocusable(false);
// cHolder.edit1.setFocusableInTouchMode(false);
// cHolder.edit2.setFocusable(true);
// cHolder.edit2.requestFocus();
// cHolder.edit2.setFocusableInTouchMode(true);
// cHolder.edit2.requestFocusFromTouch();
// break;
// }
cHolder.edit1.setText(CList.get(groupPosition).get(childPosition));
cHolder.btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
int num = Integer.parseInt(cHolder.edit1.getText().toString());
num--;
cHolder.edit1.setText(num+"");
}
});
cHolder.btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int num = Integer.parseInt(cHolder.edit1.getText().toString());
num++;
cHolder.edit1.setText(num+"");
}
});
cHolder.btn3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext, "提交成功!!!", Toast.LENGTH_SHORT).show();
}
});
// cHolder.edit1.setOnTouchListener(new OnTouchListener() {
// @Override
// public boolean onTouch(View arg0, MotionEvent arg1) {
// inEditOne = true;
// PList.get(groupPosition).setNum(1);
// cHolder.edit2.setFocusable(false);
// cHolder.edit2.setFocusableInTouchMode(false);
// cHolder.edit1.setFocusable(true);
// cHolder.edit1.requestFocus();
// cHolder.edit1.setFocusableInTouchMode(true);
// cHolder.edit1.requestFocusFromTouch();
// InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
// imm.showSoftInput(cHolder.edit1, 0);
// return false;
// }
// });
// cHolder.edit1.setOnFocusChangeListener(new OnFocusChangeListener() {
// @Override
// public void onFocusChange(View v, boolean hasFocus) {
// if(!hasFocus&&inEditOne){
// cHolder.edit1.requestFocus();
// }
//
// }
// });
// cHolder.edit2.setOnFocusChangeListener(new OnFocusChangeListener() {
// @Override
// public void onFocusChange(View v, boolean hasFocus) {
// if(!hasFocus&&!inEditOne){
// cHolder.edit2.requestFocus();
// }
//
// }
// });
// cHolder.edit2.setOnTouchListener(new OnTouchListener() {
// @Override
// public boolean onTouch(View arg0, MotionEvent arg1) {
// inEditOne = false;
// PList.get(groupPosition).setNum(2);
// cHolder.edit1.setFocusable(false);
// cHolder.edit1.setFocusableInTouchMode(false);
// cHolder.edit2.setFocusable(true);
// cHolder.edit2.requestFocus();
// cHolder.edit2.setFocusableInTouchMode(true);
// cHolder.edit2.requestFocusFromTouch();
// InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
// imm.showSoftInput(cHolder.edit2, 0);
// return false;
// }
// });
// cHolder.edit1.setFocusable(false);
// cHolder.edit2.setFocusable(false);
return convertView;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,ViewGroup parent) {
ParentHolder pHolder;
if(convertView == null){
pHolder = new ParentHolder();
convertView =mInflater.inflate(R.layout.expandable_item,null);
pHolder.img = (ImageView)convertView.findViewById(R.id.img);
pHolder.txt1 = (TextView)convertView.findViewById(R.id.txt1);
pHolder.txt2 = (TextView)convertView.findViewById(R.id.txt2);
pHolder.txt3 = (TextView)convertView.findViewById(R.id.txt3);
convertView.setTag(pHolder);
}else{
pHolder = (ParentHolder)convertView.getTag();
}
pHolder.img.setFocusable(false);
pHolder.txt1.setFocusable(false);
pHolder.txt2.setFocusable(false);
pHolder.txt3.setFocusable(false);
pHolder.txt1.setText("父类商品"+groupPosition);
return convertView;
}
}
}
<?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="horizontal"
android:padding="10dp"
android:background="#ffffff"
android:gravity="center_vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="减"
android:focusable="false"
android:focusableInTouchMode="false" />
<EditText
android:id="@+id/edit1"
android:inputType="number"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="1"
android:gravity="center" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="加"
android:focusable="false"
android:focusableInTouchMode="false" />
</LinearLayout>
<EditText
android:id="@+id/edit2"
android:layout_width="200dp"
android:inputType="text"
android:layout_height="wrap_content"
android:hint="请输入商品备注"
android:gravity="center" />
</LinearLayout>
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加"
android:focusable="false"
android:focusableInTouchMode="false" />
</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="wrap_content"
android:orientation="horizontal"
android:padding="10dp"
android:background="#ffffff" >
<ImageView
android:id="@+id/img"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="父商品item" />
<TextView
android:id="@+id/txt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="14元" />
<TextView
android:id="@+id/txt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="14元" />
</LinearLayout>
</LinearLayout>