2015-12-24 66 views
0

我有一個自定義的ListView項目。 setOnClickListener不起作用。我一直在尋找解決方案几個小時,但找不到任何東西。我認爲這個問題並不是'集中虛假'的事情,因爲我已經做到了。請幫幫我。Android自定義ListView項目setOnItemClickListener不工作

payments_list_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingTop="12dp" 
    android:paddingBottom="12dp" 
    android:focusable="false" 
    android:focusableInTouchMode="false"> 




<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:layout_marginTop="12dp" 
    android:paddingLeft="10dp"> 

    <ImageView 
     android:layout_width="46dp" 
     android:layout_height="35.3dp" 
     android:textColor="#000000" 
     android:id="@+id/payment_item_name2" 
     android:layout_gravity="center_horizontal|start" 
     android:src="@drawable/payments_internet" 
     android:textSize="20sp" 
     android:text="Test" 
     android:textStyle="bold" 
     android:capitalize="words" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textColor="#8c8c8c" 
    android:id="@+id/payment_item_name" 
    android:layout_gravity="center_horizontal|center" 
    android:textSize="20sp" 
    android:layout_marginLeft="10dp" 
    android:text="Test" 
    android:textStyle="bold" 
    android:capitalize="words" /> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:paddingTop="8dp" 
    android:layout_marginTop="12dp" 
    android:paddingRight="10dp"> 

    <ImageView 
     android:layout_width="15.3dp" 
     android:layout_height="20.1dp" 
     android:src="@drawable/arrow_right_red" 
     android:layout_gravity="center_vertical|right" 
     android:id="@+id/backButton" 
     android:textAllCaps = "true" 
     /> 
</LinearLayout> 

這是我活動的onCreate:

ListView paymentsList = (ListView) findViewById(R.id.paymentsList);      
paymentsList.setAdapter(testAdapter); 
paymentsList.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     Log.w("Clicked23", "CLicked23"); 
    } 
}); 

`

我的適配器:

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    LayoutInflater inflater = LayoutInflater.from(getContext()); 
    View payments_list_item = inflater.inflate(R.layout.payments_list_item, parent, false); 
    String payment = getItem(position); 
    TextView payment_item_name = (TextView) payments_list_item.findViewById(R.id.payment_item_name); 
    payment_item_name.setText(payment.toUpperCase()); 
    return payments_list_item; 
} 

//編輯 最後解決我的問題 感謝@ mustafasevgi`s回答:

我加

機器人:descendantFocusability = 「blocksDescendants」到我的自定義ListView項的佈局。

+0

請附上失敗的代碼。 –

+0

我剛剛加了@WilliamRosenbloom。 – Alvin

+0

'Log.w'不打印? –

回答

0

您可以添加此代碼?

機器人:descendantFocusability = 「blocksDescendants」

payment_list_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:paddingTop="12dp" 
     android:paddingBottom="12dp" 
     android:descendantFocusability="blocksDescendants"> 




    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:layout_marginTop="12dp" 
     android:paddingLeft="10dp"> 

     <ImageView 
      android:layout_width="46dp" 
      android:layout_height="35.3dp" 
      android:textColor="#000000" 
      android:id="@+id/payment_item_name2" 
      android:layout_gravity="center_horizontal|start" 
      android:src="@drawable/payments_internet" 
      android:textSize="20sp" 
      android:text="Test" 
      android:textStyle="bold" 
      android:capitalize="words" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="#8c8c8c" 
     android:id="@+id/payment_item_name" 
     android:layout_gravity="center_horizontal|center" 
     android:textSize="20sp" 
     android:layout_marginLeft="10dp" 
     android:text="Test" 
     android:textStyle="bold" 
     android:capitalize="words" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:paddingTop="8dp" 
     android:layout_marginTop="12dp" 
     android:paddingRight="10dp"> 

     <ImageView 
      android:layout_width="15.3dp" 
      android:layout_height="20.1dp" 
      android:src="@drawable/arrow_right_red" 
      android:layout_gravity="center_vertical|right" 
      android:id="@+id/backButton" 
      android:textAllCaps = "true" 
      /> 
    </LinearLayout> 
+0

這解決了我的問題。非常感謝你! Gardaş=)@mustafasevgi – Alvin

0

檢查下面的代碼:

public class CustomListActivity extends AppCompatActivity implements AdapterView.OnItemClickListener { 

ListView paymentsList; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_custom_list);//your layout view 
paymentsList = (ListView) findViewById(R.id.paymentsList); 
paymentsList.setAdapter(testAdapter); 
paymentsList.setOnItemClickListener(this); 
} 
@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
{ 
    //your logic 
Toast.makeText(this, "Listview Clicked", Toast.LENGTH_SHORT).show(); 

} 
} 
0

試試這個:

paymentsList.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 
      Log.w("Clicked23", "CLicked23"); 
      } 
     }); 
0

如何有關修改payments_list_item從XML中刪除此

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

這個

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    android:paddingBottom="12dp" 
    android:paddingTop="12dp"> 

    <ImageView 
     android:id="@+id/payment_item_name2" 
     android:layout_width="46dp" 
     android:layout_height="35.3dp" 
     android:src="@drawable/payments_internet"/> 

    <TextView 
     android:id="@+id/payment_item_name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:text="Test" 
     android:textColor="#8c8c8c" 
     android:textSize="20sp" 
     android:textStyle="bold" /> 

    <ImageView 
     android:id="@+id/backButton" 
     android:layout_width="15.3dp" 
     android:layout_height="20.1dp" 
     android:layout_gravity="center_vertical|right" 
     android:src="@drawable/arrow_right_red"/> 

</LinearLayout> 

你寫的非常複雜的佈局..

相關問題