2012-01-20 79 views
3

如何爲listview中的每列分別單擊事件? listview的每一行有兩個textviews。我想爲每個textview處理兩個單獨的事件。列表視圖中每列的單獨點擊事件

我該怎麼做?請幫忙。

+0

我想你可能會想到TableLayout。 –

回答

3

您可以通過定義onClickListener爲TextView的在你,你設置的TextView值

這裏是這個

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    if(convertView == null){ 
     LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     converView = inflater.inflate(R.layout.your_layout, null); 
     final ViewHolder viewHolder = new ViewHolder(); 
     viewHolder.textView1 = (TextView) view.findViewById(R.id.text1); 
     textView1.setOnClickListener(new OnClickListener(){ 
       @Override 
       public void onClick(View v){ 
        // do whatever you want 
       }; 
     }); 
     viewHolder.textView2 = (TextView) view.findViewById(R.id.text2); 
     textView2.setOnClickListener(new OnClickListener(){ 
       @Override 
       public void onClick(View v){ 
        // do whatever you want 
       }; 
     }); 
    } 
.......... 
} 
0

的代碼段自定義適配器當您創建自定義列表視圖處理這個點擊事件使用適配器..

在你getview()設置了點擊監聽器爲您的TextView ..

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View v = convertView; 
    if (v == null) { 
     LayoutInflater vi = (LayoutInflater) 
     getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     v = vi.inflate(R.layout.row, null); 
    } 

    TextView textViewTwo = (TextView) v.findViewById(R.id.text_view_two); 
    if (textViewTwo != null) 
     textViewTwo.setOnClickListener(this); 

    return v; 
} 

@Override 
public void onClick(View v) { 
    Log.d("Sample", "Clicked on tag: " + v.getTag()); 
    // Do something here, like start new activity. 
} 
相關問題