1

我在每一行都创建了带有复选框和文本视图(带有自定义背景)的 ListView。当我单击一行时,复选框和 textview 会用 listview 行突出显示,但我想在单击行后仅突出显示没有复选框和 textview 的 listview 行。请问你能帮帮我吗?

这就是我点击一行后想要的:http: //tinypic.com/r/14kfpza/5

这是我点击一行后得到的:http: //tinypic.com/r/2mr5w9c/5

谢谢

我的列表视图适配器:

public class MyAdapter<String> extends ArrayAdapter<String>{

    Context ctx;
    List<String> content;
    public MyAdapter(Context context, List<String> objects) {
        super(context, R.layout.listview_row ,objects);
        this.ctx = context;
        this.content = objects;
    }

    public View getView(int position, View convertView, ViewGroup parent){
        View row = convertView;
        if(row == null){
            LayoutInflater inflater=getLayoutInflater();
            row=inflater.inflate(R.layout.listview_row, parent, false);
        }
        TextView desc = (TextView)row.findViewById(R.id.textView1);
        String name = content.get(position);
        desc.setText(name.toString());

        return row;
    }



}

listview_row.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="16dp"
            android:text="Large Text"
            android:clickable="true"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:background="@drawable/textview_bg"
            android:textAppearance="?android:attr/textAppearanceLarge" />

       <CheckBox
           android:id="@+id/checkBox1"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignParentRight="true"
           android:layout_alignTop="@+id/textView1"
           android:layout_marginRight="5dp"
           android:clickable="true"
           android:focusable="false"
           android:focusableInTouchMode="false"
           android:text="" />

    </RelativeLayout>
4

2 回答 2

0

把它写在你的 listView xml 中。机器人:addStatesFromChildren="假"

这不会将点击事件发送给 listView 的子项

于 2013-08-27T03:12:35.503 回答
0

OK,我已经解决了,用自定义TextView,setPressed方法被覆盖了。我对 CheckBox 所做的也是如此。

public class TextViewForListView extends TextView{

    public TextViewForListView(Context context) {
        super(context);

    }
    public TextViewForListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public TextViewForListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    @Override
    public void setPressed(boolean pressed) {
        if (pressed && (this.getParent() instanceof View) && ((View) this.getParent()).isPressed()) {
            return;
        }
        super.setPressed(pressed);
    }

}
于 2013-08-27T12:26:40.687 回答