2013-03-14 59 views
2

我自己定製的ListView,並在每一行有 TextView s。可定製的ListView內可點擊的TextView

我打算讓listview可點擊的每一行內的textview秒。所以每當用戶點擊其中的任何一個時,它都會將他重定向到其他地方。

實際上,當用戶點擊ListView項目時,它會點擊它的全部內容,並且textviews將被忽略。

總之,當我點擊ListView項目時,我不希望Listview項目作出響應,我想Listview內的Textview作出響應。

編輯:

這裏是我定製了自己的看法:

String[] from = {"value1", "value2"}; 
int[] to = {R.id.label, R.id.label2}; 
SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, data, R.layout.rowlayout, from, to); 
+0

粘貼您的代碼 – stinepike 2013-03-14 17:16:19

+0

^這。你試過什麼了?您是否將OnClickListeners添加到單獨的TextViews,或者您是否重寫了onListItemClick()? – crazylpfan 2013-03-14 17:17:23

+0

我還沒有嘗試任何事情,我該嘗試什麼? – Alex 2013-03-14 17:25:13

回答

2

在適配器類,裏面getview()方法,你可以這樣

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View row = convertView; 
    Holder holder = null; 
    if (row == null) { 
     LayoutInflater vi = 
       (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     row = vi.inflate(R.layout.listitem, null); 
     holder.txtTitle1 = (TextView) row.findViewById(R.id.heading1); 
     holder.txtTitle2 = (TextView) row.findViewById(R.id.heading2); 

     holder.txtTitle1 .setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
      } 
     }); 

     holder.txtTitle2 .setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
      } 
     }); 
    } 
    return row; 
} 
///// 
/////////// 

static class Holder { 
    TextView txtTitle1; 
    TextView txtTitle2; 
} 
2
set your TextView in your layot.xml 

<TextView 
    android:id="@+id/YourID" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:clickable="true" 

/> 
執行的onclick行動

,然後在您的自定義適配器

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View row = convertView; 
    Holder holder = null; 
    if (row == null) { 
     LayoutInflater vi = 
       (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     row = vi.inflate(R.layout.youLayout, null); 
     holder.txt1 = (TextView) row.findViewById(R.id.tx1); 
     holder.txt2 = (TextView) row.findViewById(R.id.tx2); 

     holder.txt1 .setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
//your statements    
} 
     }); 

     holder.txt2 .setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
//your statements 
      } 
     }); 
    } 
    return row; 
}