android how not to mark certain view in the row, but not the row itself in listview
i have a listview in which each row contains a checkbox and a horizontal linearLayout right to the checkbox. the linear layout's background is a drawable, and the layout itself contains two textViews side by side (big text and small text -sometimes it is hidden tho):
What i want that when someone clicks on a row, the checkbox will flip its status, and it does. I never want the row itself to change color, however, what happens now is that when i click on the LinearLayout (that contains the textViews), the row itself and the linear layout changes the color to orange,
but i want only the linearLayout with the textViews to change its color. I managed to do it with TextView with drwable background but here it has to be LinearLayout becuase it has to contain two textViews.
so my question is, how do i make this heppen, meaning that when someone clicks on the linearLayout, then the LinearLayout will change its color to orange, without the line to change its color?? Thanks!
here is my row.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/LinearLayout11"
android:layout_height="wrap_content"
android:gravity="center_vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent">
<CheckBox
android:paddingLeft="6dp"
android:button="@drawable/custom_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:focusable="false"
android:id="@+id/interest_checkbox">
</CheckBox>
<LinearLayout
android:gravity="center_vertical"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/LinearLayout12"
android:layout_gravity="center_horizontal"
android:paddingLeft="6dp"
android:weightSum="1"
android:paddingRight="6dp"
android:layout_marginRight="6dp"
android:background="@drawable/row_state">
<TextView
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="0dp"
android:layout_weight="1"
android:id="@+id/interest_summary" android:text="Big Text">
</TextView>
<TextView
android:layout_height="wrap_content"
android:textColor="@color/back"
android:gravity="left"
android:textAppearan开发者_如何学Cce="?android:attr/textAppearanceSmall"
android:layout_width="0dp"
android:id="@+id/TextView11"
android:layout_weight="1"
android:text="small text">
</TextView>
</LinearLayout>
</LinearLayout>
and here is row_state.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
android:state_pressed="true"
android:drawable="@drawable/row_clicked"
/> <!-- pressed -->
<item
android:state_focused="true"
android:drawable="@android:drawable/editbox_background_normal"
/> <!-- focused -->
<item
android:drawable="@drawable/row"
/> <!-- default -->
</selector>
You need to override bindView of your SimpleCursorAdapter:
SimpleCursorAdapter myAdapter = new SimpleCursorAdapter(...) {
public void bindView(View view, android.content.Context context, Cursor cursor) {
final LinearLayout layout = view.findViewById(R.id.somelinearlayout);
// Add an onclick handler to the LinearLayout
layout.setOnClickListener(new OnClickListener() {
// Add click handler that alters the background
});
}
}
set Background of CheckBox too with similar state list.. just like row_state.xml and probably u can do this..
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
android:state_pressed="true"
android:drawable="@drawable//*default checkBox color*/"
/> <!-- pressed -->
<item
android:state_focused="false"
android:drawable="@drawable//*default checkBox color*/"
/> <!-- focused -->
<item
android:drawable="@drawable//*default checkBox color*/"
/> <!-- default -->
</selector>
精彩评论