在自定义微调器上的项目选择上更改ImageView

时间:2014-10-09 14:40:21

标签: 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"
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;
}
}

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
                }

            });

        }
    });

我试图:

               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) {

                }
            });

但它不起作用。我也尝试了大约一千个教程和stackoverflow答案,但没有任何工作正常。

我该怎么做才能解决这个问题? dot_filled和dot_empty是“我的自定义radiobuttons”。谢谢你的帮助; - )

0 个答案:

没有答案