2014-11-02 213 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