2017-06-26 205 views
-2

我無法使我的列表視圖可點擊。我嘗試了相同類型問題的所有給定解決方案,並試圖將focusableintouchmode值更改爲true和false。無法使Listview項目可點擊

點擊按鈕10/12次後,它有時會工作(即listview中的文本被點擊)。 下面是我的xml和java文件。 我已經附上了logcat的截圖,當我按了20多次後,我的第9行被點擊了。 enter image description here 請幫助..

Activity_card.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/rlLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.hppc.business.Card"> 

    <ListView 
     android:id="@+id/lvUsers" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:clickable="true" 
     android:focusable="false" 
     android:focusableInTouchMode="false" 
     android:descendantFocusability="blocksDescendants"> 

    </ListView> 

</RelativeLayout>   

entry1.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:clickable="true" 
    android:focusable="false" 
    android:focusableInTouchMode="false" 
    android:orientation="horizontal" 
    android:padding="13dp"> 

    <ImageView 
     android:layout_width="90dp" 
     android:layout_height="50dp" 
     android:layout_gravity="center" 
     android:clickable="true" 
     android:focusable="false" 
     android:focusableInTouchMode="false" 
     android:src="@drawable/image" /> 

    <linearlayout>.....</linearlayout> 
</linearLayout>   

card.java

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, 
    int position, long id) { 
     Intent i = new Intent(Card.this, Manual.class); 
     Log.i("HelloListView", "You clicked Item: " + id + " at position:" + position); 
     Log.e("YESSSS",Integer.toString(position)); 
     startActivity(i); 
    } 
}); 
+1

嘗試刪除'機器人:可點擊=「真」'你entry1.xml –

+0

從你的列表視圖中刪除XML的最後四行即。可點擊的,可調焦的,可調焦的touchmode,特別是後裔可聚焦性 –

+0

@JozefDochan非常感謝。它的工作 –

回答

0

你必須讓你的列表視圖可聚焦:

  android:focusable="true" 
      android:focusableInTouchMode="true" 
+0

感謝您的幫助man.I以前已經嘗試過,但它沒有爲我工作。在entry1.xml中刪除可點擊。 –