chr*_*ine 57 android listview button
我想在Button里面显示一个ListView.目标应该是单击该ListView行或按钮.
点击它显示更多信息的行.点击底部更多按钮显示的按钮.
与GMAIL应用程序相同.
在右侧有一个复选框,单击底部的复选框后,会出现按钮栏.
我的问题是在插入按钮后ListView,按钮不可点击.如果我LinearLayout从按钮添加llButton.setClickable()它是有效的.但是,只有按钮.它ListView本身不再对点击作出反应!
我试过这个例子.
与上述相同的问题......
fla*_*rld 120
只是为了说明这一点 - 似乎没有人说过这么简单的事情 - 虽然不允许有一个可聚焦的按钮与列表视图一起工作,但有一个更简单的解决方案.
接受的答案是给定的 - 在设置列表项的单击侦听器时应始终这样做,因此OP不知道这是愚蠢的.
如果您使用XML布局作为列表项,只需将按钮设置为具有以下属性,它也将使列表项也可以单击:
android:focusable="false"
Far*_*tas 18
将下面的行添加到列表项XML中.
android:descendantFocusability="blocksDescendants"
Run Code Online (Sandbox Code Playgroud)
然后你的列表项将是这样的:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:descendantFocusability="blocksDescendants"
android:layout_height="wrap_content" >
// Your layout objects here
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
如果您使用的是自定义适配器,则单击ListView内的Button将无效,因此您应尝试使用以下代码进行检查OnItemClickListener.
listId.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> list, View v, int pos, long id) {
// Your code for item clicks
}
});
Run Code Online (Sandbox Code Playgroud)
小智 7
要在单击按钮或列表项时触发事件,可以执行以下操作:
您只处理onItemClick:
mListView.setOnItemClickListener(new ListView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int i, long l) {
// handle click here
}
);
Run Code Online (Sandbox Code Playgroud)
在适配器中,您将按钮修改为不可点击/可聚焦(或者在xml文件中执行此操作):
public class MyAdapter extends BaseAdapter {
...
public View getView(int position, View convertView, ViewGroup parent) {
.....
Button btn = view.findViewById(R.id.button);
btn.setFocusable(false);
btn.setClickable(false);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
93242 次 |
| 最近记录: |