3

我做了自己的定制ListView,每行有2 个 TextView

我打算在可点击textview的每一行中制作这些 s 。listview因此,每当用户点击其中任何一个时,它都会将他重定向到其他地方。

实际上,当用户单击一个ListView项目时,它会单击所有项目,并且textviews会被忽略。

总之,当我单击一个ListView项目时,我不希望 Listview 项目响应,我希望Textview内部Listview响应。

编辑:

这是我自定义视图的方式:

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

2 回答 2

3

在您的 Adapter 类中,在 getview() 方法中,您可以像这样执行 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.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;
}
于 2013-03-14T17:26:32.470 回答
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"

/>

然后在您的自定义适配器上

@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;
}
于 2014-08-25T10:59:04.060 回答