4

假设我在微调器列表中有 10 个项目。并且项目编号 3 已被选中。现在的问题是,当用户想要更改他的选择时,我想给出某种指示,表明 this(item number 3) 已经是选中的项目。我想通过复选标记或设置某种背景或类似方式来实现这一点。

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

4

1 回答 1

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;
    }

}
于 2014-06-12T05:16:47.833 回答