gpt4 book ai didi

java - 画廊项目背景未随 setSelection 更改

转载 作者:行者123 更新时间:2023-12-01 15:48:07 26 4
gpt4 key购买 nike

我有一个包含文本项目的图库。我可以使用 UI 或使用 setSelection(position) 以编程方式更改所选项目。但是,当我调用此方法时,有时项目背景不会更改为选定状态。我注意到,如果 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,
android.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, android.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;
}
}
}

gallery_background.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@drawable/highlight_selected" />
<item android:state_checked="true" android:drawable="@drawable/highlight_selected" />
<item android:state_focused="true" android:drawable="@drawable/highlight_selected" />
<item android:state_pressed="true" android:drawable="@drawable/highlight_pressed" />
<item android:drawable="@drawable/highlight_disabled" />
</selector>

最佳答案

尝试使图库无效notifyDataSetChanged图库适配器。

关于java - 画廊项目背景未随 setSelection 更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6712342/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com