我已经从几个小时以来一直在努力解决这个问题,我找不到解决方案,我希望有人能帮助我。
我有一个带有扩展 BaseAdapter 的自定义适配器的 ListView,并且列表的每个元素都扩展了一个仅包含 TextView 的 LinearLayout。我基本上从如何添加自定义按钮状态开始。
这是列表元素的 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 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"/>
用于文本选择器和<item android:drawable="@color/red" app:state_playing="false"/>
背景选择器,一切都按预期工作,我可以看到带有红色背景的LinearLayout和带有黄色文本的文本!
我真的不知道该尝试什么了......(即使我将属性默认设置为app:state_playing="true"
xml 布局中的对象()它也不起作用!
非常感谢任何帮助、提示或经验分享!