2013-05-14 197 views
6

在这个ListView不可点击的情况下,我想对项目点击执行一个操作,但我不知道为什么这不是可点击的,我也检查了调试控制器没有在itemclicklistener中消失。ListView项在Android中不可点击

设计文件:

<?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="match_parent" 
android:orientation="vertical" > 

<CheckBox 
    android:id="@+id/cbSolved" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginLeft="15dp" 
    android:layout_marginTop="20dp" 
    android:text="Show Solved" /> 

<CheckBox 
    android:id="@+id/cbMyCases" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBaseline="@+id/cbSolved" 
    android:layout_alignBottom="@+id/cbSolved" 
    android:layout_alignParentRight="true" 
    android:layout_marginRight="46dp" 
    android:text="My Cases" /> 

<EditText 
    android:id="@+id/etCaseSeacrh" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/cbSolved" 
    android:layout_marginLeft="28dp" 
    android:ems="10" 
    android:hint="Search" > 
</EditText> 

<ListView 
    android:id="@+id/liCases" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/btnNewCase" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/etCaseSeacrh" 
    android:layout_marginTop="23dp" 
    android:clickable="true" > 
</ListView> 

<Button 
    android:id="@+id/btnNewCase" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:text="New Case" /> 

</RelativeLayout> 

这是我的Java类在这个我访问列表中,但无法点击

public class ShowCases extends Activity{ 
    ListView listshowcase; 
EditText search; 
    ListViewAdapter adapter; 

    @Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
    setContentView(R.layout.screen4); 

    listshowcase = (ListView) findViewById(R.id.liCases); 
    adapter = new ListViewAdapter(this); 
     listshowcase.setAdapter(adapter); 
    listshowcase.setClickable(true); 
    listshowcase.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> arg0, View view, int arg2, 
       long arg3) { 
      // TODO Auto-generated method stub 
      String name = ((TextView) view.findViewById(R.id.name)).getText().toString(); 
      String cost = ((TextView) view.findViewById(R.id.email)).getText().toString(); 
      String description = ((TextView) view.findViewById(R.id.mobile)).getText().toString(); 
      String id = ((TextView) view.findViewById(R.id.id)).getText().toString(); 
      // Starting new intent 
      Intent in = new Intent(getApplicationContext(), CompanyDetail.class); 
      in.putExtra("name", name); 
      in.putExtra("email", cost); 
      in.putExtra("ff", description); 
      in.putExtra("fff", id); 
      startActivity(in); 
     } 
    }); 


    search=(EditText)findViewById(R.id.etCaseSeacrh); 

    search.addTextChangedListener(new TextWatcher() { 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 
      // Call back the Adapter with current character to Filter 

     } 

     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count,int after) { 
     } 

       @Override 
     public void afterTextChanged(Editable arg0) { 
      // TODO Auto-generated method stub 

     } 
    }); 
    } 

,这是自定义的getview Listadapter类

@Override 
    public View getView(final int paramInt, View paramView, 
     final ViewGroup paramViewGroup) { 
     // TODO Auto-generated method stub 
     LayoutInflater inflator = activity.getLayoutInflater(); 
     if (paramView == null) { 
     view = new ViewHolder(); 
     paramView = inflator.inflate(R.layout.list_item, null); 

     view.name = (TextView) paramView.findViewById(R.id.name); 
     view.email = (TextView) paramView.findViewById(R.id.email); 
     view.mobile = (TextView) paramView.findViewById(R.id.mobile); 
     view.id = (TextView) paramView.findViewById(R.id.id); 
     paramView.setTag(view); 

     } else { 
     view = (ViewHolder) paramView.getTag(); 
     } 

    view.name.setText(TAG_NAME.get(paramInt)); 
    view.email.setText(TAG_EMAIL.get(paramInt)); 
    view.mobile.setText(TAG_PHONE_MOBILE.get(paramInt)); 
    view.id.setText(TAG_ID.get(paramInt)); 
    /*view.name.setFocusableInTouchMode(false); 
    view.name.setFocusable(false);*/ 
    /*view.setOnClickListener(myClickListener);*/ 
    view.name.setFocusable(false); 
    view.email.setFocusable(false); 
    view.mobile.setFocusable(false); 
    view.id.setFocusable(false); 

    return paramView; 
} 
+0

删除所有setFocusable – 2013-05-14 05:56:47

+0

使用此,不工作 – Android 2013-05-14 06:09:22

+0

可能重复[ListView项目不可点击。为什么?](http://stackoverflow.com/questions/8955270/listview-items-are-not-clickable-why) – rds 2015-07-01 13:38:44

回答

31

好吧,你有一个按钮在那里。在您的list_item xml中,将android:descendantFocusability="blocksDescendants"放在LinearLayout或RelativeLayout中。

+0

哇它的工作,唉我knw什么功能这个的 ? – Android 2013-05-14 06:21:17

+0

我接受你的答案,请我想知道这个功能,请告诉你是否可以 – Android 2013-05-14 06:23:21

+1

此链接http://cyrilmottier.com/2011/11/23/listview-tips-tricks-4-add-several可点击区域/有很多有用的信息。我无法解释以及链接中解释的内容。 – 2013-05-14 06:27:32

0

你能显示列表吗?如果是,那么请显示layout文件持有TextView在列表中显示项目。

尝试从TextView中删除isTextSelectable。

检查是否有一些愚蠢的错误就像我几个月前发从这里:

onItemClick in ListView doesn't work

+0

使用这个,不工作 – Android 2013-05-14 06:10:32

0

刚刚从ShowCases删除

listshowcase.setClickable(true); 

此行。然后再试一次。

我希望这会帮助你。如果没有,请让我知道。

+0

使用这个,不工作 – Android 2013-05-14 06:10:11

0

将OnClickListener添加到从适配器返回的每个视图中。