突出显示没有视图的ListView行

时间:2013-08-27 02:57:52

标签: android android-listview android-xml

我已经在每一行创建了带有复选框和textview(带自定义背景)的ListView。 当我单击一行时,复选框和textview会以列表视图行突出显示,但是我想在单击该行后仅突出显示listview行而不显示复选框和textview。你能帮帮我吗?

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

这是我点击一行后所拥有的:http://tinypic.com/r/2mr5w9c/5

谢谢

我的listview适配器:

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>

2 个答案:

答案 0 :(得分:0)

在你的listView xml中写下这个。 机器人:addStatesFromChildren = “假”

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

答案 1 :(得分:0)

好的,我已经解决了它,使用自定义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);
    }

}