突出显示预先选择的微调项目

时间:2013-02-07 16:21:49

标签: android spinner

假设我在微调框列表中有10个项目。 并且已选择项目编号3。现在问题是当用户想要改变他的选择时,我想给出某种指示(项目编号3)已经选择了项目。我希望通过勾选标记或设置某种背景或以类似的方式实现此目的。

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

我使用自定义适配器来实现此功能。只需将其从BaseAdapter延长,并为SpinnerDroppdown控件增加观看次数。

List<String> stagesValues = new ArrayList<>(stagesResults.values());
mStageSpn.setAdapter(new DropdownAdapter(mContext, stagesValues, mStageSpn));

public class DropdownAdapter extends BaseAdapter {

    private final LayoutInflater mInflater;
    private List<String> mData;
    private Spinner mStageSpn;

    public DropdownAdapter(Context context, List<String> data, Spinner stageSpn) {
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mData = data;
        mStageSpn = stageSpn;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int arg0) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = mInflater.inflate(android.R.layout.simple_spinner_item, null);
        ((TextView) view.findViewById(android.R.id.text1)).setText(mData.get(mStageSpn.getSelectedItemPosition()));

        return view;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        View view = mInflater.inflate(R.layout.spinner_item, null);
        if (mStageSpn.getSelectedItemPosition() == position)
            view.setBackgroundColor(Color.RED);
        ((TextView) view.findViewById(R.id.text_id)).setText(mData.get(position));

        return view;
    }

}
相关问题