Android 在自定义微调器上选择项目时更改ImageView
当用户在自定义微调器上选择某个项目时,如何更改ImageView? spinner_row.xmlAndroid 在自定义微调器上选择项目时更改ImageView,android,spinner,Android,Spinner,当用户在自定义微调器上选择某个项目时,如何更改ImageView? spinner_row.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:id="@id/sort" android:layout_width="match_parent" android:layout_height="60dp" andr
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:id="@id/sort"
android:layout_width="match_parent"
android:layout_height="60dp"
android:weightSum="6">
<ImageView
android:id="@+id/checkbox"
android:layout_weight="1"
android:scaleType="fitCenter"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
<TextView
android:id="@+id/sort_option"
android:layout_weight="5"
android:layout_width="0dp"
android:layout_height="match_parent"
android:textSize="@dimen/text_size"
android:textColor="@color/blue"
android:gravity="center_vertical" />
SortSpinnerAdapter.java
public class SortSpinnerAdapter extends ArrayAdapter<String>{
private Activity activity;
private List<String> data;
public Resources res;
SpinnerModel tempValues=null;
LayoutInflater inflater;
int selectedItem=0;
public SortSpinnerAdapter(
Results activitySpinner,
int textViewResourceId,
List<String> objects
)
{
super(activitySpinner, textViewResourceId, objects);
activity = activitySpinner;
data = objects;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getDropDownView(int position, View convertView,ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
View row = inflater.inflate(R.layout.spinner_row, parent, false);
tempValues = null;
TextView label = (TextView)row.findViewById(R.id.sort_option);
final ImageView companyLogo = (ImageView)row.findViewById(R.id.checkbox);
if(position==selectedItem){
label.setText(data.get(position));
companyLogo.setImageResource(R.drawable.dot_fill);
}
else
{
label.setText(data.get(position));
companyLogo.setImageResource(R.drawable.dot_empty);
}
return row;
}
public int getSelectedItem() {
return selectedItem;
}
public void setSelectedItem(int selectedItem) {
this.selectedItem = selectedItem;
}
}
公共类SortSpinnerAdapter扩展了ArrayAdapter{
私人活动;
私人名单数据;
公共资源;
SpinnerModel tempValues=null;
充气机;
int-selectedItem=0;
公共分拣机分拣机(
结果activitySpinner,
int textViewResourceId,
列出对象
)
{
超级(activitySpinner、textViewResourceId、对象);
活动=活动微调器;
数据=对象;
充气器=(LayoutInflater)activity.getSystemService(Context.LAYOUT\u充气器\u SERVICE);
}
@凌驾
公共视图getDropDownView(int位置、视图转换视图、视图组父视图){
返回getCustomView(位置、转换视图、父级);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
返回getCustomView(位置、转换视图、父级);
}
公共视图getCustomView(int位置、视图转换视图、视图组父视图){
视图行=充气器。充气(R.layout.spinner\u行,父级,false);
tempValues=null;
TextView标签=(TextView)row.findViewById(R.id.sort_选项);
final ImageView companyLogo=(ImageView)row.findViewById(R.id.checkbox);
如果(位置==选择编辑项){
label.setText(data.get(position));
companyLogo.setImageResource(R.drawable.dot_fill);
}
其他的
{
label.setText(data.get(position));
companyLogo.setImageResource(R.drawable.dot_empty);
}
返回行;
}
public int getSelectedItem(){
返回selectedItem;
}
公共无效设置selectedItem(int selectedItem){
this.selectedItem=selectedItem;
}
}
Acivity.java
final Spinner customSpinner = (Spinner) findViewById(R.id.sortSpinner);
final SortSpinnerAdapter adapter = new SortSpinnerAdapter(res,R.layout.spinner_row,lol);
customSpinner.setAdapter(adapter);
sort.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
customSpinner.performClick();
customSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View v, int position, long id) {
adapter.setSelectedItem(position);
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
}
});
final微调器customSpinner=(微调器)findViewById(R.id.sortSpinner);
最终SortSpinnerAdapter=新SortSpinnerAdapter(res,R.layout.spinner_行,lol);
customSpinner.setAdapter(适配器);
sort.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
customSpinner.performClick();
customSpinner.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView父视图、视图v、整型位置、长id){
适配器。设置选择项(位置);
}
@凌驾
未选择公共无效(AdapterView父视图){
//你的代码在这里
}
});
}
});
我试图:
customSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
customSpinner.setSelection(i);
for (int j = 0; j < adapterView.getCount(); j++) {
View v = adapterView.getChildAt(j);
ImageView iv = (ImageView) v.findViewById(R.id.checkbox);
if (i != j)
iv.setImageResource(R.drawable.dot_empty);
else
iv.setImageResource(R.drawable.dot_fill);
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
customSpinner.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView AdapterView、View视图、int i、long l){
customSpinner.setSelection(一);
对于(int j=0;j
但这是行不通的。我还尝试了上千个教程和stackoverflow答案,但没有一个能正常工作
我能做些什么来解决这个问题?点_填充和点_空是“我的自定义单选按钮”。谢谢你的帮助;-) 什么“不起作用”?如果是坠机,你有logcat stacktrace吗?它没有坠机,但什么也没发生。我点击这个项目,图像并没有改变(当我使用上面帖子中的这个循环时)。我还尝试在方法getCustomView()中的行上设置onClickListener,并在更改单击图像后,但微调器并没有消失。