java Gallery项目背景未随setSelection更改
我有一个带有文本项的图库。我可以使用UI或使用setSelection(position)以编程方式更改所选项目。但是,当我调用此方法时,有时项目背景不会更改为选定状态。我注意到,如果屏幕上已经绘制了名为by setSelection的项目,则其背景不会更新
这是代码。欢迎任何帮助
public class Test3 extends Activity {
private static String[] items = {"0", "1", "2", "3", "4", "5"};
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
LinearLayout layout = new LinearLayout (this);
layout.setOrientation (LinearLayout.VERTICAL);
setContentView (layout);
final Gallery gallery = new Gallery (this);
layout.addView (gallery);
gallery.setSpacing (0);
gallery.setAdapter (new Adapter (this));
gallery.setSelection (0);
ListView list = new ListView (this);
layout.addView (list);
list.setAdapter (new ArrayAdapter <String> (this,
安卓.R.layout.simple_list_item_1, items));
list.setOnItemClickListener (new OnItemClickListener () {
public void onItemClick (AdapterView <?> parent, View view, int position,
long id)
{
gallery.setSelection (position);
}
});
}
private class Adapter extends ArrayAdapter <String> {
public Adapter (Context context) {
super (context, 安卓.R.layout.simple_gallery_item, items);
}
public View getView (int position, View convertView, final ViewGroup parent)
{
TextView view = new TextView (getContext ());
view.setText (getItem (position));
view.setBackgroundResource (R.drawable.gallery_background);
view.setGravity (Gravity.CENTER);
view.setLayoutParams (new Gallery.LayoutParams (Test3.this
.getWindowManager ().getDefaultDisplay ().getWidth () / 3,
Gallery.LayoutParams.FILL_PARENT));
return view;
}
}
}
画廊背景。xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:安卓="http://schemas.安卓.com/apk/res/安卓">
<item 安卓:state_selected="true" 安卓:drawable="@drawable/highlight_selected" />
<item 安卓:state_checked="true" 安卓:drawable="@drawable/highlight_selected" />
<item 安卓:state_focused="true" 安卓:drawable="@drawable/highlight_selected" />
<item 安卓:state_pressed="true" 安卓:drawable="@drawable/highlight_pressed" />
<item 安卓:drawable="@drawable/highlight_disabled" />
</selector>
# 1 楼答案
请尝试
invalidating
库适配器或notifyDataSetChanged
库适配器