gpt4 book ai didi

android - Android中的可展开 ListView 对点击没有反应,它不会展开

转载 作者:IT老高 更新时间:2023-10-28 23:05:12 25 4
gpt4 key购买 nike

我有一个可展开的 ListView 。我放了一些虚拟数据,一切看起来都还可以,但是当我运行应用程序时,所有组都处于折叠模式,当我单击每个组时它们都不会生效。这是截图:

enter image description here

组的 XML 是:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/bg_slider"
android:orientation="horizontal" >


<TextView
android:id="@+id/tvRecipeName"
style="@style/normal_text.bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="50dp"
android:layout_marginRight="5dp"
android:focusable="false"
android:lines="1"
android:maxLines="1"
android:text="@string/dd_title" />


<ImageButton
android:id="@+id/ibDeleteRecipe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:background="@color/transparent"
android:contentDescription="@string/cd"
android:focusable="false"
android:src="@android:drawable/ic_menu_delete" />

<View
android:layout_width="1dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/tvRecipeName"
android:focusable="false" />

</RelativeLayout>

child 的 XML 是:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/White"
android:gravity="left|center_vertical" >

<View
android:layout_width="1dp"
android:layout_height="35dp"
android:layout_toLeftOf="@+id/tvIngredient"
android:focusable="false" />

<TextView
android:id="@+id/tvIngredient"
style="@style/normal_text_black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="30dp"
android:layout_marginRight="5dp"
android:lines="1"
android:maxLines="1"
android:text="@string/dd_title"
android:focusable="false" />

<ImageButton
android:id="@+id/ibDeleteIngredient"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:background="@color/transparent"
android:contentDescription="@string/cd"
android:focusable="false"
android:src="@android:drawable/btn_dialog" />

</RelativeLayout>

在main.xml中,可扩展列表的定义是:

<ExpandableListView
android:id="@+id/elv"
android:layout_width="match_parent"
android:layout_height="match_parent" />

我有一个适配器,为此我编写了以下代码:

public class ExpAdapter extends BaseExpandableListAdapter {

private final String TAG = "ExpAdapter";
private Context context;
static final String arrGroupelements[] = {"India", "Australia", "England", "South Africa"};
static final String arrChildelements[][] = { {"Sachin Tendulkar", "Raina", "Dhoni", "Yuvi" },
{"Ponting", "Adam Gilchrist", "Michael Clarke"},
{"Andrew Strauss", "kevin Peterson", "Nasser Hussain"},
{"Graeme Smith", "AB de villiers", "Jacques Kallis"} };

public ExpAdapter(Context context) {
this.context = context;

Log.i(TAG, "Adapter created.");
}


@Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}

@Override
public long getChildId(int groupPosition, int childPosition) {

return 0;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.elv_child, null);
}

TextView tvItem = (TextView) convertView.findViewById(R.id.tvIngredient);
ImageButton ibDelete = (ImageButton) convertView.findViewById(R.id.ibDeleteIngredient);
ibDelete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.i(TAG, "******");
}
});
tvItem.setText(arrChildelements[groupPosition][childPosition]);

return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
return arrChildelements[groupPosition].length;
}

@Override
public Object getGroup(int groupPosition) {
return null;
}

@Override
public int getGroupCount() {
return arrGroupelements.length;
}

@Override
public long getGroupId(int groupPosition) {
return 0;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.elv_group, null);
}

TextView tvItem = (TextView) convertView.findViewById(R.id.tvRecipeName);
ImageButton ibDeleteRcipe = (ImageButton) convertView.findViewById(R.id.ibDeleteRecipe);
ibDeleteRcipe.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.i(TAG, "%%%%%%");
}
});
tvItem.setText(arrGroupelements[groupPosition]);

return convertView;
}

@Override
public boolean hasStableIds() {
return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}

}

最后,在 fragment Activity 的代码中,我有:

public class ShoppingList extends FragmentActivity {    
ExpAdapter adapter = new ExpAdapter(this);
//Linking expnadable list view
expListView = (ExpandableListView) findViewById(R.id.elv);
expListView.setAdapter(adapter);
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Log.i(TAG, "Group " + groupPosition + " expanded.");
}
});
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
Log.i(TAG, "Group " + groupPosition + " collapsed.");
}
});
expListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Log.i(TAG, "item " + childPosition + " of group " + groupPosition + " clicked.");
return false;
}
});

}

当我运行应用程序并单击 parent 时,没有任何反应。我尝试在购物类末尾添加以下代码行:

int count = adapter.getGroupCount();
for (int position = 1; position <= count; position++)
expListView.expandGroup(position - 1);

现在我运行应用程序的结果是这样的截图:

enter image description here

如果我单击删除按钮(父或子),我可以看到它们生效(当我检查 logcat 时),但是当我单击子或父时没有任何反应。所以,我不知道为什么回调不起作用。根据我的研究,我发现我需要将图像按钮的 Focussable 设置为 false。正如您在 XML 文件中看到的那样,我做到了,但是当我单击父行和子行时,我仍然看不到任何响应。

最佳答案

我终于找到了解决方案:)

我不知道是 bug 还是疾病还是...

如上所述,根据我的研究,我发现我们需要将 ImageView /框的焦点能力设置为“false”。我在 XML 文件中做了它,它没有工作。我在 XML 中将焦点能力设置为“真”,并在代码中将其设置为假。像这样:

@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.elv_group, null);
}

TextView tvItem = (TextView) convertView.findViewById(R.id.tvRecipeName);
ImageButton ibDeleteRcipe = (ImageButton) convertView.findViewById(R.id.ibDeleteRecipe);
ibDeleteRcipe.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
...
}
});
ibDeleteRcipe.setFocusable(false);

tvItem.setText(arrGroupElements[groupPosition]);

return convertView;
}

因此基于我的成就!它应该在代码而不是 XML 中设置为“false”!现在的问题是有什么区别? -我不知道。

关于android - Android中的可展开 ListView 对点击没有反应,它不会展开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11818278/

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