gpt4 book ai didi

android - 单击按钮时展开可扩展 ListView

转载 作者:太空狗 更新时间:2023-10-29 15:17:54 25 4
gpt4 key购买 nike

我正在尝试在我的项目中实现 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;
}
}

最佳答案

如果您想通过单击特定按钮或其他 View 来展开/折叠组,您必须在适配器类的 getGroupView 方法中获取该按钮。然后,在 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;

关于android - 单击按钮时展开可扩展 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10827454/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com