我已經從這個小時開始苦苦掙扎,我找不到解決方案,希望有人能幫助我。
我有一個ListView與自定義適配器擴展BaseAdapter和列表的每個元素擴展一個LinearLayout只包含一個TextView。我基本上從開始。
這是列表元素的list_item.xml佈局文件:Android:自定義選擇器狀態永遠不會變「真」
<?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的的文字顏色(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似乎工作,但只有在狀態「假」,因爲如果我在選擇狀態列表的開頭添加一行<item app:state_playing="false" android:color="@color/yellow"/>
爲文本選擇器和背景選擇器一切工作正常,我可以看到帶有紅色背景的LinearLayout和帶有黃色文本的文本!
我真的不知道該怎麼去嘗試了...(即使我默認設置爲在XML佈局中的對象(app:state_playing="true"
)的財產這是行不通的!
任何幫助,暗示或經驗共享極爲賞識!
不知道這個?如果需要,我可以提供更多信息! – Andre