32

编辑(已解决)对于答案转到问题的底部。Listitem click不适用于复选框Android

我有一个列表视图,我使用customadapter在它的一些行充气。 正在充气的行包含一个复选框。我可以保持复选框的状态,但问题是,我的列表项不可点击。 这里是我的代码:

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; 
     } 
} 

我想listItems中的可点击的情况下,用户希望只点击一个项目。

EDIT(上AKHIL的要求) - preann2.xml在布局文件夹:

<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布局中的文件夹:

<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(解决) 只需将这些属性添加到复选框:

android:focusable="false" 
    android:focusableInTouchMode="false" 
+1

如果您希望在单击行的情况下检查checkBox,那么为行本身添加onClickListener()条件。 – 2013-04-07 05:21:20

+0

我想在点击列表视图中的项目时执行其他一些操作。通过点击它们可以直接设置复选框,所以有关这方面的问题。 我实际上想要在单击某个项目时调用某个活动。 – silentkratos 2013-04-07 05:32:08

+0

尝试在复选框的'onClickListener'内添加'log',并让我们知道日志是否已注册。 – 2013-04-07 05:36:32

回答

0

我误解你的问题.. please have a look here您的问题..希望它能够解决您的问题..

+0

我也这么做过。文本视图,图标和复选框在一个布局中。 Mainmenu listview是另一种布局。 – silentkratos 2013-04-07 05:29:46

+0

我得到了那..但是我在你的ListItem布局中说,保持你的TextView&Icon在一个布局,并设置监听器在该布局上,就像你做复选框 – Akhil 2013-04-07 05:34:21

+0

我已经共享布局作为编辑。你能告诉我如何..?请? – silentkratos 2013-04-07 05:35:53

5

在列表项的rootView中使用此行
android:descendantFoc可用性=“blocksDescendants”