选定的微调器项目被微调器中的第一行替换

时间:2016-10-10 05:30:16

标签: android android-spinner android-adapter

我一直在研究一个微调器,它有一个自定义布局用于下拉列表。这是我的问题,当我选择时,让我们说下拉列表中的第二项,它被选中并且下拉关闭。当我重新打开下拉列表时,第一行保持不变,但第二行重命名为第一行。但它告诉我何时将log.d打印到控制台,该位置:2被选中。

这是我的代码,

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

        TextView textView = (TextView) convertView.inflate(getContext(), android.R.layout.simple_spinner_item, null);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
        textView.setTextColor(ContextCompat.getColor(getActivity().getApplicationContext(), R.color.blue));
        if(list.getIsRecent()){
            textView.setText("Most Recent ");
        } else {
            textView.setText(list.get(position).getName());
        }
        return textView;
    }

    public View getCustomView(final int position, View convertView, ViewGroup parent) {
        boolean isRecent;
        isRecent = list.get(position).getIsRecent();
        if (convertView == null) {
                convertView = inflater.inflate(R.layout.custom_spinner_layout, parent, false);
                savedSearchName = (TextView) convertView
                        .findViewById(R.id.custom_spinner_layout_text_view);


            btnDelete = (ImageButton) convertView
                        .findViewById(R.id.custom_spinner_layout_button);

            relativeLayoutContainer = (RelativeLayout) convertView.findViewById(R.id.relativeLayout_item_name_container);
            relativeLayoutContainer.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.d(TAG,"relativeLayoutContainer: setOnClickListener: position"+position);
                    Method method = null;
                    if(mSavedSearchCriteriaSpinner != null) {
                        mSavedSearchCriteriaSpinner.setSelection(position);
                        try {
                            method = Spinner.class.getDeclaredMethod("onDetachedFromWindow");
                            method.setAccessible(true);
                            method.invoke(mSavedSearchCriteriaSpinner);
                        } catch (Exception  e) {
                            e.printStackTrace();
                        }
                    }
                }
            });


            if(isRecent){
                btnDelete.setVisibility(View.INVISIBLE);
                savedSearchName.setText(list.get(position).getName() + "MOSTRECENT");
            } else {
                savedSearchName.setText(list.get(position).getName());
                btnDelete.setVisibility(View.VISIBLE);

                btnDelete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //AlertDialog
                });
            }

        }
        return convertView;
    }

截图: 初始微调器 Initial Spinner

Spinner DropDown Spinner dropdown

选择的位置1:Qwerty position 1 selected: qwerty

重新开启下拉列表 Reopening dropdown

不,这不是一个家庭作业问题。

1 个答案:

答案 0 :(得分:1)

getView()中,您使用了convertView错误。这可以是null,也可以是需要回收的旧视图。如果它是null,您必须像在进行中那样夸大新视图。如果没有,这是一个需要回收的旧项目(即您需要修改文本,您甚至不需要再次设置文本大小和颜色):

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView textView = convertView;
        if (convertView == null) {
            LayoutInflater li = LayoutInflater.from(parent);
            textView = (TextView) li.inflate(getContext(), android.R.layout.simple_spinner_item, null);
            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
            textView.setTextColor(ContextCompat.getColor(getActivity().getApplicationContext(), R.color.blue));
        }
        if(list.getIsRecent()){
            textView.setText("Most Recent ");
        } else {
            textView.setText(list.get(position).getName());
        }
        return textView;
    }

此外,getCustomView将btnDelete和savedSearchName存储在对象的字段中,可能这会弄乱旧内容。另外,总是更新convertView(现在,如果convertView不为null,则不要更新它):

public View getCustomView(final int position, View convertView, ViewGroup parent) {
    boolean isRecent;
    isRecent = list.get(position).getIsRecent();
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.custom_spinner_layout, parent, false);
    }

    TextView savedSearchName = (TextView) convertView.findViewById(R.id.custom_spinner_layout_text_view);


    ImageButton btnDelete = (ImageButton) convertView.findViewById(R.id.custom_spinner_layout_button);

    RelativeLayout relativeLayoutContainer = (RelativeLayout) convertView.findViewById(R.id.relativeLayout_item_name_container);
    relativeLayoutContainer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(TAG,"relativeLayoutContainer: setOnClickListener: position"+position);
            Method method = null;
            if(mSavedSearchCriteriaSpinner != null) {
                mSavedSearchCriteriaSpinner.setSelection(position);
                try {
                    method = Spinner.class.getDeclaredMethod("onDetachedFromWindow");
                    method.setAccessible(true);
                    method.invoke(mSavedSearchCriteriaSpinner);
                } catch (Exception  e) {
                    e.printStackTrace();
                }
            }
        }
    });


    if(isRecent){
        btnDelete.setVisibility(View.INVISIBLE);
        savedSearchName.setText(list.get(position).getName() + "MOSTRECENT");
    } else {
        savedSearchName.setText(list.get(position).getName());
        btnDelete.setVisibility(View.VISIBLE);

        btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    //AlertDialog
            });
    }

    return convertView;
}

关于在字段中存储savedSearchNamebtnDelete,我猜是因为您想避免再次搜索它们。在那种情况下,我建议使用持有人模式。请参阅以下Google guide中的在视图持有者中保留视图对象