android-从代码而不是从xml创建选择器时出现意外结果
作者:互联网
我在将一些有效的XML文件转换为代码时遇到麻烦.
我有一个ListView,并且我需要能够在运行时从高级资源中未知的资源中切换其按下/选中的可绘制对象(因此为什么不使用XML);
以下配置非常有效:
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:choiceMode="singleChoice" >
</ListView>
</LinearLayout>
选择器.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/blue" android:state_pressed="true"/>
<item android:drawable="@drawable/green" android:state_checked="true"/>
<item android:drawable="@drawable/orange"/>
</selector>
list_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/selector"
android:padding="10dp" />
Main.java:
public class Main extends Activity {
StateListDrawable selector;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listView1 = (ListView) findViewById(R.id.listView1);
StringAdapter adapter = new StringAdapter(this, R.layout.list_row);
adapter.add("one"); adapter.add("two"); adapter.add("three"); adapter.add("four");
adapter.add("five"); adapter.add("six"); adapter.add("seven"); adapter.add("eight");
adapter.add("nine"); adapter.add("ten"); adapter.add("eleven"); adapter.add("twelve");
listView1.setAdapter(adapter);
}
private class StringAdapter extends ArrayAdapter<String>{
public StringAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final CheckedTextView tv = (CheckedTextView ) inflater.inflate(R.layout.list_row, parent, false);
tv.setText(getItem(position));
return tv;
}
}
}
结果是这样的,很棒(按下:蓝色,选中:绿色,否则:橙色):
但是,如果我删除了
android:background="@drawable/selector
从list_row_xml,并通过代码应用它:
Drawable blue = getResources().getDrawable(R.drawable.blue);
Drawable green = getResources().getDrawable(R.drawable.green);
Drawable orange = getResources().getDrawable(R.drawable.orange);
selector = new StateListDrawable();
selector.addState(new int[] { android.R.attr.state_pressed }, blue);
selector.addState(new int[] { android.R.attr.state_checked }, green);
selector.addState(new int[] { }, orange);
tv.setBackgroundDrawable(selector);
我得到以下内容(一切都得到state_pressed可绘制的蓝色):
什么地方出了错?我很确定我将选择器正确地转换为代码.
解决方法:
我复制了您的代码,一切正常.
不过,您应该确保在getView()中创建选择器的新实例.否则,如果对所有项目使用相同的选择器,则按一个项目将影响所有项目.
这是您的getView()方法的外观
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final CheckedTextView tv = (CheckedTextView) inflater.inflate(R.layout.list_row, parent, false);
tv.setText(getItem(position));
Drawable blue = getResources().getDrawable(R.drawable.blue);
Drawable green = getResources().getDrawable(R.drawable.green);
Drawable orange = getResources().getDrawable(R.drawable.orange);
selector = new StateListDrawable();
selector.addState(new int[] { android.R.attr.state_pressed }, blue);
selector.addState(new int[] { android.R.attr.state_checked }, green);
selector.addState(new int[] {}, orange);
tv.setBackgroundDrawable(selector);
return tv;
}
当然,您可以进行一些优化,但这将起作用.
标签:listview,android-xml,statelistdrawable,android 来源: https://codeday.me/bug/20191101/1981661.html