列表项单击不与复选框的Android合作 [英] Listitem click doesn't work with checkboxes Android
问题描述
修改(解决)作为回答到问题的底部。
我有一个ListView和我夸大使用customadapter它的一些行。 被夸大的行中包含一个复选框。我可以保持复选框的状态,但问题是,我的列表中的项目没有更多的点击。 这是我的code:
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
FL =(ListView控件)findViewById(R.id.MainMenu); // MAINMENU是列表视图
fl.setAdapter(新CustomAdapter(Annotate.this,R.layout preann2,_dir)。); // preann2是行和_dir是对象的列表
//fl.setOnItemClickListener(this);试过这种太
}
公共无效onItemClick(适配器视图<>一种,视图中查看,INT POS,长ID)
{
//实现
}
公共类CustomAdapter扩展ArrayAdapter<字符串>
{
公共CustomAdapter(上下文的背景下,INT textViewResourceId,字符串[]对象){
超(背景下,textViewResourceId,对象);
}
@覆盖
公共查看getView(最终诠释的立场,观点convertView,ViewGroup中父){
LayoutInflater充气= getLayoutInflater();
查看排= inflater.inflate(R.layout preann2,父母,假);
尝试
{
TextView的名称=(TextView中)row.findViewById(R.id.title);
Name.setText(XYZ);
ImageView的图标=(ImageView的)row.findViewById(R.id.icon);
row.setFocusable(真正的);
row.setClickable(真正的);
row.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
// TODO自动生成方法存根
的CheckState [位置] =的CheckState [位置]!;
}
});
复选框C =(复选框)row.findViewById(R.id.checkBox);
c.setChecked(的CheckState [位置]);
c.setOnCheckedChangeListener(新OnCheckedChangeListener(){
@覆盖
公共无效onCheckedChanged(CompoundButton V,
布尔器isChecked){
// TODO自动生成方法存根
复选框复选框=(复选框)V;
如果(器isChecked){
的CheckState [位置] =真;
}
其他
的CheckState [位置] = FALSE;
}
});
/ *尝试这太
c.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
复选框复选框=(复选框)V;
如果(checkBox.isChecked()){
//checkBox.setChecked(false);
的CheckState [位置] =真;
}
其他
的CheckState [位置] = FALSE;
}}); * /
icon.setImageResource(R.drawable.ic_launcher);
}赶上(Throwable的错误)
{
err.printStackTrace();
}
返回行;
}
}
我希望listItems中可点击的情况下用户想要点击仅一个项目。
修改(有关Akhil的要求) - preann2.xml在布局文件夹:
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:方向=横向
机器人:填充=4dip
>
< ImageView的机器人:ID =@ + ID /图标
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =FILL_PARENT
机器人:layout_alignParentTop =真
机器人:layout_alignParentBottom =真
机器人:layout_marginRight =4dip/>
<的TextView
机器人:ID =@ + ID /标题
机器人:layout_width =FILL_PARENT
机器人:layout_height =match_parent
机器人:layout_weight =1
机器人:ellipsize =结束
机器人:重力=center_vertical
机器人:单线=真
机器人:TEXTSTYLE =黑体/>
<复选框
机器人:ID =@ + ID /复选框
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT/>
< / LinearLayout中>
main.xml中的布局文件夹:
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直>
<的ListView
机器人:ID =@ + ID / MainMenu的
机器人:layout_width =match_parent
机器人:layout_height =match_parent>
< / ListView控件>
< / LinearLayout中>
修改(解决) 只需添加这些属性复选框:
机器人:可调焦=假
机器人:focusableInTouchMode =假
在问题中提到的解决方案解决了这个问题:
机器人:可调焦=假
机器人:focusableInTouchMode =假
AbsListView
检查查看#hasFocusable
,也不允许该项目如果是presses。因此,禁用重点通常都可获得焦点项目。
EDIT (SOLVED) For the answer go to the bottom of the question.
I have a listview and I inflate some rows in it using a customadapter. The row being inflated contains a checkbox. I can maintain the checkbox's status, but the problem is, my list items are no more clickable. Here's my code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
fl=(ListView)findViewById(R.id.MainMenu);//Mainmenu is listview
fl.setAdapter(new CustomAdapter(Annotate.this,R.layout.preann2,_dir));//preann2 is row and _dir is the list of objects
//fl.setOnItemClickListener(this); tried this too
}
public void onItemClick(AdapterView<?> a, View view, int pos, long id)
{
//Implementations
}
public class CustomAdapter extends ArrayAdapter<String>
{
public CustomAdapter(Context context, int textViewResourceId,String[] objects) {
super(context, textViewResourceId,objects);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.preann2, parent, false);
try
{
TextView Name=(TextView)row.findViewById(R.id.title);
Name.setText("xyz");
ImageView icon=(ImageView)row.findViewById(R.id.icon);
row.setFocusable(true);
row.setClickable(true);
row.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
checkState[position]=!checkState[position];
}
});
CheckBox c=(CheckBox)row.findViewById(R.id.checkBox);
c.setChecked(checkState[position]);
c.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton v,
boolean isChecked) {
// TODO Auto-generated method stub
CheckBox checkBox=(CheckBox)v;
if (isChecked) {
checkState[position]=true;
}
else
checkState[position]=false;
}
});
/* tried this too
c.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
CheckBox checkBox=(CheckBox)v;
if (checkBox.isChecked()) {
//checkBox.setChecked(false);
checkState[position]=true;
}
else
checkState[position]=false;
}});*/
icon.setImageResource(R.drawable.ic_launcher);
}catch(Throwable err)
{
err.printStackTrace();
}
return row;
}
}
I want the listitems to be clickable in case the user wants to click only a single item.
EDIT (on Akhil's request)- preann2.xml in layouts folder:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="4dip"
>
<ImageView android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="4dip"/>
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:ellipsize="end"
android:gravity="center_vertical"
android:singleLine="true"
android:textStyle="bold" />
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
main.xml in layouts folder:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/MainMenu"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
EDIT (SOLVED) Just add these properties to checkbox:
android:focusable="false"
android:focusableInTouchMode="false"
The solution mentioned in the question solves the problem:
android:focusable="false"
android:focusableInTouchMode="false"
这篇关于列表项单击不与复选框的Android合作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!