我几个小时以来一直在努力解决这个问题,我找不到解决办法,希望有人可以帮助我 我有一个带有自定义适配器的ListView,它扩展了BaseAdapter,列表的每个元素都扩展了一个只包含TextView的LinearLayout。我基本上是从How to add a custom button state开始的 这是list元素的list_item.xml布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.example.mypackage"
android:id="@+id/newItem"
app:state_playing="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/item_selector"
android:orientation="horizontal" >
<TextView
android:id="@+id/textview"
style="@style/Text_View_Style_White"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:duplicateParentState="true"
android:ellipsize="marquee"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:padding="3dp"
android:scrollHorizontally="true"
android:singleLine="true"
android:textColor="@drawable/item_text_selector" />
</LinearLayout>
以下是BaseAdapter的相关代码:
class AlbumListAdapter extends BaseAdapter implements Checkable {
private Context mContext;
public AlbumListAdapter(Context context) {
mContext = context;
}
@Override
public int getCount() {
return listSize; // is a variable
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
AlbumViewHolder albumHolder = null;
if (convertView == null) {
// Inflate the layout
LayoutInflater li = getActivity().getLayoutInflater();
convertView = li.inflate(R.layout.list_item, null);
albumHolder = new AlbumViewHolder(mContext, null);
albumHolder.albumTitle = (TextView) convertView
.findViewById(R.id.textview);
convertView.setTag(albumHolder);
} else {
albumHolder = (AlbumViewHolder) convertView.getTag();
}
albumHolder.albumTitle.setText("Some text in the TextView");
albumHolder.albumTitle.setSelected(true);
if (myBooleanCondition)
{
albumHolder.setPlaying(true);
albumHolder.refreshDrawableState();
for (int i = 0; i < albumHolder.getDrawableState().length; i++) {
if (albumHolder.getDrawableState()[i] != 0)
System.out.println(TAG + " State i = "
+ i + " -> " + getResources().getResourceEntryName(albumHolder.getDrawableState()[i]));
}
}
return convertView;
}
}
这里是扩展LinearLayout的列表元素类:
static class AlbumViewHolder extends LinearLayout {
private static final int[] STATE_PLAYING_SET = { R.attr.state_playing };
private boolean mIsPlaying = false;
TextView albumTitle;
public AlbumViewHolder(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setPlaying(boolean isPlaying) {
mIsPlaying = isPlaying;
}
@Override
protected int[] onCreateDrawableState(int extraSpace) {
final int[] drawableState = super
.onCreateDrawableState(extraSpace + 1);
if (mIsPlaying) {
System.out.println(TAG
+ " - onCreateDrawableState() -> mIsPlaying = "
+ mIsPlaying);
mergeDrawableStates(drawableState, STATE_PLAYING_SET);
}
return drawableState;
}
}
这是attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="music">
<attr name="state_playing" format="boolean" />
<attr name="playing" format="boolean" />
</declare-styleable>
</resources>
这里有2个选择器尝试使用选择器状态state_playing
,一个用于每个元素的LinearLayout背景(item_selector.xml):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res/com.example.mypackage">
<item android:drawable="@color/blue_fluo3" app:state_playing="true"/>
<item android:drawable="@color/orange_fluo" android:state_pressed="true" app:state_playing="false"/>
<item android:drawable="@color/orange_fluo" android:state_activated="true" app:state_playing="false"/>
<item android:drawable="@android:color/transparent" app:state_playing="false"/>
</selector>
和一个用于TextView的textColor(item_text_selector.xml):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res/com.example.mypackage">
<item app:state_playing="true" android:color="@color/orange_fluo"/>
<item android:state_pressed="true" app:state_playing="false" android:color="@color/black"/>
<item android:state_activated="true" app:state_playing="false" android:color="@color/black"/>
<item app:state_playing="false" android:color="@android:color/white"/>
</selector>
两者的结果相同:state_playing似乎有效,但只有状态“false”,因为如果我在选择器状态列表的开头添加一行,<item app:state_playing="false" android:color="@color/yellow"/>
用于文本选择器,{ {1}}对于背景选择器,一切都按预期工作,我可以看到红色背景的LinearLayout和带黄色文本的文本!
我真的不知道该尝试什么...(即使我默认将属性设置为xml布局中的对象(<item android:drawable="@color/red" app:state_playing="false"/>
),它也不起作用!
非常感谢任何帮助,提示或经验分享!
答案 0 :(得分:0)
我看起来没问题,但我建议在容器布局中设置duplicateParentState,如下所示。我有一个模糊的记忆,就是我自己也有问题......
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.example.mypackage"
android:id="@+id/newItem"
android:duplicateParentState="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/item_selector"
android:orientation="horizontal" >
<TextView
android:id="@+id/textview"
style="@style/Text_View_Style_White"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:duplicateParentState="true"
android:ellipsize="marquee"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:padding="3dp"
android:scrollHorizontally="true"
android:singleLine="true"
android:textColor="@drawable/item_text_selector" />