2014-11-02 205 views
2

我的ListView出现问题:(定制的)项目只能在分隔线上点击。我看了几个小时的解决方案,但没有为我工作。Android ListView项目不可点击

android:descendantFocusability =“blocksDescendants”不能正常工作。对每个儿童项目设置focusable =“false”也不起作用。有没有人知道这个问题的解决方案?

ItemLayout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="horizontal" 
     android:paddingTop="@dimen/padding" 
     android:paddingBottom="@dimen/padding" 
     android:paddingLeft="@dimen/padding" 
     android:paddingRight="@dimen/padding" 
     android:layout_width="match_parent" 
     android:descendantFocusability="blocksDescendants" 
     android:layout_height="wrap_content" > 

    <TextView 
      android:layout_width="match_parent" 
      android:focusable="false" 
      android:focusableInTouchMode="false" 
      android:layout_height="wrap_content" 
      style="@style/elremFontMedium" 
      android:id="@+id/detailNoticeText" /> 

</LinearLayout> 

此北京时间我的ListView

<ListView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/detailNoticeListView"/> 

由于适配器我用一个CursorAdapter

public class NoticeAdapter extends CursorAdapter { 

    public NoticeAdapter(Context context, Cursor cursor) { 
     super(context, cursor, 0); 
    } 

    @Override 
    public View newView(Context context, Cursor cursor, ViewGroup parent) { 
     LayoutInflater inflater = LayoutInflater.from(parent.getContext()); 
     View retView = inflater.inflate(R.layout.contact_detail_notice_item, parent, false); 

     return retView; 
    } 

    @Override 
    public void bindView(View view, Context context, Cursor cursor) { 
     TextView textViewNotice = (TextView) view.findViewById(R.id.detailNoticeText); 
     textViewNotice.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.NOTICE_TABLE_COLUMN_TEXT))); 
    } 
} 

的onItemClick是我Fragmant

@Override 
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 

     switch(adapterView.getId()) { 
      case R.id.detailNoticeListView: 
       Cursor cursor = mNoticeAdapter.getCursor(); 
       cursor.moveToPosition(i); 

       TextView textView = (TextView) view.findViewById(R.id.detailNoticeText); 

       Bundle bundle = new Bundle(); 
       bundle.putString(NoticeDialog.EDIT_TEXT_ID, cursor.getString(cursor.getColumnIndex("_id"))); 
       bundle.putString(NoticeDialog.EDIT_TEXT, textView.getText().toString()); 

       NoticeDialog noticeDialog = new NoticeDialog(); 
       noticeDialog.setTargetFragment(this, 0); 
       noticeDialog.setArguments(bundle); 
       noticeDialog.show(getFragmentManager(),"noticeDialog"); 
       break; 
     } 
    } 
+0

使用此列表视图编写更多类的代码 – 2014-11-02 14:18:12

+0

您在哪里设置onItemClickListener? – SemaphoreMetaphor 2014-11-02 14:46:01

+0

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

回答

1

我已经解决了这个问题。 原因是我像这样修改了TextView样式。

<style name="elremFontMedium" parent="@android:style/TextAppearance.Medium"> 
    <item name="android:textSize">20dp</item> 
    <item name="android:textIsSelectable">true</item> 
</style> 

问题是<item name="android:textIsSelectable">true</item>。 所以我删除了这个属性,一切正常。

0

android:clickable="true"加到ItemLayout.xmlLinearLayout。您还在哪里onItemClickListener

+0

添加clickable = true不能工作。监听器位于我的片段中。我在我的问题中添加了。 – 2014-11-02 14:31:45