2016-08-23 13 views
0

我有三個TextViews一個ListView:如何確定哪些觀點是點擊在ListView同時處理ListView.setOnItemClickListener

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/txtProductName" 
     android:text="name" 
     android:textSize="17sp" 
     android:paddingTop="10dp" 
     android:paddingBottom="10dp" 
     android:paddingLeft="10dp" 
     android:paddingRight="10dp" 
     android:layout_width="0dp" 
     android:layout_alignParentLeft="true" 
     android:layout_toLeftOf="@+id/txtOPrice" 
     android:layout_height="wrap_content" /> 

    <TextView 
     android:id="@+id/txtOPrice" 
     android:text="1.60" 
     android:textSize="17sp" 
     android:paddingTop="10dp" 
     android:paddingBottom="10dp" 
     android:paddingLeft="10dp" 
     android:paddingRight="10dp" 
     android:layout_width="65dp" 
     android:layout_height="wrap_content" 
     android:background="@android:color/holo_blue_dark" 
     android:clickable="true" 
     android:textStyle="bold" 
     android:textColor="#ffffff" 

     android:layout_toLeftOf="@+id/txtNetPrice" 
     android:textAlignment="center" /> 

    <TextView 
     android:id="@+id/txtNetPrice" 
     android:text="1.61" 
     android:textSize="17sp" 
     android:paddingTop="10dp" 
     android:paddingBottom="10dp" 
     android:paddingLeft="10dp" 
     android:paddingRight="10dp" 
     android:layout_width="65dp" 
     android:layout_height="wrap_content" 
     android:background="@android:color/holo_red_light" 
     android:textStyle="bold" 
     android:textColor="@android:color/white" 
     android:layout_alignParentRight="true" 
     android:textAlignment="center" /> 
</RelativeLayout> 

我處理ListView項單擊事件如下:

adapter = new productListAdapter(this,arrayListProducts); 
    listProducts.setAdapter(adapter); 

    listProducts.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
      //how to know which textview was clicked? 
     } 
    }); 

我只想知道如何確定哪些TextViews被點擊?

我是新手,搜索了很多,但什麼也沒找到。

我將不勝感激任何形式的幫助。提前致謝!

回答

0

當您收到回調時,view參數是點擊查看項目,而i是您的列表視圖中的位置。但是,view是整個列表項的參考,這意味着RelativeLayout在你的情況。從那裏你可以在視圖參數中找到使用findViewById()所需的特定TextView,這將返回你想要的列表項中的TextView。

+0

感謝您的快速回復。然而,我問的是:如果用戶點擊一個特定的textview我需要運行一堆特定的代碼,如果用戶點擊另一個我需要運行一個不同的代碼。那麼如何確定RelativeLayout中哪個TextView實際上被點擊了。 –

+0

在這種情況下,onItemClickListener不是您要查找的內容。您需要爲每個文本視圖設置3個不同的onClickListener,並且可以使用'setTag'爲每個文本視圖設置一個標籤,因此您可以識別它們 –

0

您可以在佈局的所有3個文本視圖上設置點擊監聽器,也可以設置標記到所有視圖。所以通過使用標籤和ID你可以得到哪個視圖被點擊。

+0

您可以詳細說明如何執行此操作嗎? –

+0

View v =((View)findViewbyid(R.id.txtProductName)); v.setOnclickListener(本); v.setTag(pos);的onClick(視圖V) { 開關(v.getId()){ 情況 R.id.txtProductName: doprodTask(v.getTag()) } doProdTask(位置) { } – Ashish

+0

類似地,對於所有3個文本視圖,你完成 – Ashish

0

爲什麼不使用帶自定義適配器的listview並將clicklistners添加到每個textview。

+0

我需要處理我的活動中的點擊事件,而不是在適配器類中。我已經在使用一個自定義的適配器類。 –

+0

什麼時候會有辦法。您可以將內部類添加到您的活動中。 –

相關問題