简体   繁体   English

复合ListView行的StateList

[英]StateList for Compound ListView Row

When I click a row in my ListView the row is using the statelist but the individual elements are not. 当我单击ListView中的一行时,该行正在使用状态列表,但各个元素未使用。

  1. Setting duplicateParentState(true) on the image, text and image does nothing. 在图像,文本和图像上设置plicateParentState(true)不会执行任何操作。
  2. Setting the individual items to clickable(true) applies the statelist changes to each item, but does not change the parent even if addChildStates() (approximation of methodname) is set. 将单个项目设置为clickable(true)会将状态列表更改应用于每个项目,但是即使设置了addChildStates()(方法名的近似值)也不会更改父项。

So, I want to click a row in the ListView and have the states of the individual items updated. 因此,我想单击ListView中的一行,并更新各个项目的状态。 Obviously missing something. 显然缺少一些东西。

I have the following row in a ListView: 我在ListView中有以下行:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list_row"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@drawable/statelist_row" >

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.1" >

        <ImageView
            android:id="@+id/product_image"
            style="@style/list_image"
            android:contentDescription="@string/product_image" />

        <ProgressBar
            android:id="@+id/product_image_progress"
            style="@style/list_image"
            android:visibility="gone" />
    </FrameLayout>

    <TextView
        android:id="@+id/product_name"
        style="@style/list_text"
        android:layout_weight="0.9"
        android:gravity="center_vertical" />

    <ImageView
        style="@style/list_caret"
        android:layout_weight="0.1"
        android:contentDescription="@string/caret" />

</LinearLayout>

In the adapter I make the row clickable: 在适配器中,使该行可单击:

    holder.productListRow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(context, "CLICKED: '" + holder.productName.getText().toString() + "'", Toast.LENGTH_LONG).show();
        }
    });

I have a statelist that is applied in the styles: 我有一个应用于样式的状态列表:

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

    <item android:drawable="@color/selected_item" android:state_pressed="true" android:textColor="@color/white" android:color="@color/white"/>
    <item android:drawable="@color/std_background" android:color="@color/black"/>

</selector>

From colors: 从颜色:

<color name="list_text_color">@drawable/statelist_produce_row</color>

Finally, from styles: 最后,从样式:

<style name="list_text" parent="fill_parent">
    <item name="android:textSize">@dimen/list_text_size</item>
    <item name="android:textColor">@color/list_text_color</item>
    <item name="android:layout_gravity">left|center_vertical</item>
    <item name="android:padding">@dimen/list_text_padding</item>
    <item name="android:textStyle">bold</item>
</style>

Shouldn't you make the listview items clickable by setting an onItemClickListener for the listview instead of making each row clickable? 您是否不应该通过为列表视图设置onItemClickListener而不是使每一行都可以单击来使列表视图项目可点击?

Try this 尝试这个

mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        ...
}        
});

or if you're extending the ListActivity, you can directly implement onListItemClick(): 或者,如果要扩展ListActivity,则可以直接实现onListItemClick():

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM