我正在尝试在我的项目中实现exapndablelistview。我能够扩展listView,问题是我只想在我点击图片时展开它,但每当我点击listview行的任何地方时列表都会展开。 请帮我解决这个问题。
提前致谢。
我在xml中的代码是
<ExpandableListView
android:id="@+id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_weight="1"
android:drawSelectorOnTop="false"
android:groupIndicator="@android:color/transparent">
</ExpandableListView>
我的java类是
public class MainActivity extends ExpandableListActivity{
TextView tv;
ExpandableListView lv;
Button b1;
Integer imgk[]={R.drawable.dhoni,R.drawable.ganguly,R.drawable.irfan,R.drawable.rahul,R.drawable.sachin,R.drawable.sehwag,R.drawable.singh,R.drawable.sri,R.drawable.uthapa,R.drawable.yuvi};
String[] names={"MS Dhoni","Sorav Ganguly","Irfan Pathan","Rahul Dravid","Sachin Tendulkar","Virender Sehwag","Harbajan Singh","Sreeshanth","Robin Uthapa","Yuvraj Singh"};
String matches[]={"21","12","13","15","35","22","25","18","21","31"};
String[] fifty={"12","21","16","10","16","18","10","20","19","22"};
String hund[]={"21","12","17","18","13","21","23","10","21","14"};
String[] team={"India","pakistan","bangladesh","australia","new zealand","south Africa","england","zimbabwe","west indies","sri lanka"};
String[][] childs={{"India","pakistan"},{"India","pakistan"},{"India","pakistan"},{"India","pakistan"},{"India","pakistan"},{"India","pakistan"},{"India","pakistan"},{"India","pakistan"},{"India","pakistan"},{"India","pakistan"}};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myscreen);
tv = (TextView) this.findViewById(R.id.textView1);
tv.setSelected(true);
lv=getExpandableListView();
CustomAdapter ca=new CustomAdapter(MainActivity.this);
lv.setAdapter(ca);
}
public class CustomAdapter extends BaseExpandableListAdapter{
Context con;
private LayoutInflater myInflater;
private Bitmap bm;
int count=0;
public CustomAdapter(Context con) {
// TODO Auto-generated constructor stub
this.con=con;
myInflater=LayoutInflater.from(con);
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return names.length;
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return childs[groupPosition].length;
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return names[groupPosition];
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childs[groupPosition][childPosition];
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
@Override
public View getGroupView(final int groupPosition, final boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder holder;
if (convertView == null) {
convertView = myInflater.inflate(R.layout.inflatexml, null);
holder = new ViewHolder();
holder.txtName = (TextView) convertView.findViewById(R.id.textView1);
holder.img=(ImageView)convertView.findViewById(R.id.imageView1);
holder.img1= (ImageView)convertView.findViewById(R.id.imageView2);
holder.down=(ImageView)convertView.findViewById(R.id.imageView4);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtName.setText(names[groupPosition]);
holder.img.setImageResource(imgk[groupPosition]);
holder.img1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Bitmap bm=BitmapFactory.decodeResource(MainActivity.this.getResources(),
imgk[groupPosition]);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
Intent in=new Intent(MainActivity.this,Details.class);
in.putExtra("img", b);
in.putExtra("names", names[groupPosition]);
in.putExtra("matches", matches[groupPosition]);
in.putExtra("fifty", fifty[groupPosition]);
in.putExtra("hund", hund[groupPosition]);
in.putExtra("team", team[groupPosition]);
startActivity(in);
}
});
holder.down.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
count++;
if(isExpanded){
holder.down.setFocusable(false);
lv.collapseGroup(groupPosition);
System.out.println("collapsed.....");
}
else{
holder.down.setFocusable(false);
lv.expandGroup(groupPosition);
System.out.println("Expanded.....");
}
}
});
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
TextView tv=getGenericView();
tv.setText(""+childs[groupPosition][childPosition]);
return tv;
}
public TextView getGenericView() {
// TODO Auto-generated method stub
AbsListView.LayoutParams lp=new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
TextView textView = new TextView(MainActivity.this);
textView.setLayoutParams(lp);
return textView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
}
static class ViewHolder {
TextView txtName;
ImageView img,img1,down;
}
}
答案 0 :(得分:0)
如果要通过单击特定Button或其他View来展开/折叠组,则必须在Adapter类的getGroupView方法中获取该Button。然后,在你的Button的onClick方法中,要么将父项强制转换为ExpandableListView,要么在创建适配器时在构造函数中传递List的引用。
我更喜欢第一种方法。这是代码,假设你有一个TextView和一个箭头ImageView。我已经添加了更改箭头状态。 @覆盖 public View getGroupView(final int groupPosition,final boolean isExpanded, 查看convertView,最终ViewGroup父级){
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.left_drawer_list_group, parent, false);
}
TextView listHeaderText = (TextView) convertView
.findViewById(R.id.left_menu_list_header_text);
ImageView listHeaderArrow = (ImageView) convertView.findViewById(R.id.left_menu_list_header_arrow);
listHeaderText.setText(headerTitle);
//Set the arrow programatically, so we can control it
int imageResourceId = isExpanded ? android.R.drawable.arrow_up_float : android.R.drawable.arrow_down_float;
listHeaderArrow.setImageResource(imageResourceId);
listHeaderArrow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isExpanded) ((ExpandableListView) parent).collapseGroup(groupPosition);
else ((ExpandableListView) parent).expandGroup(groupPosition, true);
}
});
return convertView;
}