删除Android Spinner onClick项目背景

时间:2017-03-08 02:35:11

标签: android android-spinner

我正在尝试删除Spinner项目的onClick的默认突出显示效果。我有自己特定的微调背景,其角落有半径。当我应用新背景时,它仍然在我的自定义背景下具有默认背景。我尝试过这样的样式:

<style name="ExternalPSpinnerTheme" parent="Widget.AppCompat.Spinner.DropDown">
    <item name="android:listSelector">@color/transparent</item>
    <item name="android:listChoiceBackgroundIndicator">@color/transparent</item>
    <item name="android:listChoiceIndicatorSingle">@color/transparent</item>
    <item name="android:selectableItemBackground">@color/transparent</item>
    <item name="android:background">@drawable/spinner_sortfriend_background</item>
    <item name="android:cacheColorHint">@android:color/transparent</item>
    <item name="android:popupBackground">@drawable/spinner_sortfriend_background</item>
    <item name="android:dropDownSelector">@color/transparent</item>
</style>

我已尝试将父级更改为Widget.AppCompat.Spinner,但它无法正常工作.. PopupBackgroundbackground可以正常工作。

另一个问题是如何隐藏分隔线并添加自己的微调器自定义实现,我的问题是如何删除背景上的默认onClick突出显示效果。我是如何尝试实现它的?这是如何:

if (position > 0 &&
   (position + 1) < mDataset.size()){
    divider.setVisibility(View.VISIBLE);
    convertView.setBackground(ContextCompat.getDrawable(context, R.drawable.spinner_dropdown_background_middle_white_to_migrey));
    } else if (position == 0){
        divider.setVisibility(View.VISIBLE);
        convertView.setBackground(ContextCompat.getDrawable(context, R.drawable.spinner_dropdown_background_top_white_to_migrey));
    } else if (position + 1 == mDataset.size()){
        divider.setVisibility(View.GONE);
        convertView.setBackground(ContextCompat.getDrawable(context, R.drawable.spinner_dropdown_background_bottom_white_to_migrey));
    }

我仍然没有得到正确的答案,我甚至在另一个问题上尝试了解决方案。我刚刚意识到这一点,只有第一个项目的背景与我的自定义背景点击效果重叠,但列表末尾的那个没有重叠......很奇怪..

任何解决方案?

2 个答案:

答案 0 :(得分:0)

在xml中创建自定义Spinner布局,如下所示

<RelativeLayout
            android:id="@+id/relativeLayout_multiChoiceDropDown"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/drawable_border_profile">

        <TextView
            android:id="@+id/textView_multiChoiceDropDownText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            android:layout_marginBottom="@dimen/margin_5dp"
            android:layout_marginEnd="@dimen/margin_5dp"
            android:layout_marginStart="@dimen/margin_5dp"
            android:layout_marginTop="@dimen/margin_5dp"
            android:layout_toStartOf="@+id/button_multiChoiceDropDownArrow"
            android:padding="@dimen/margin_5dp"
            android:text="Select Item"
            android:textColor="@color/black"
            android:textSize="@dimen/text_size_16"/>


        <Button
            android:id="@+id/button_multiChoiceDropDownArrow"
            android:layout_width="@dimen/profile_multi_choice_width"
            android:layout_height="@dimen/profile_multi_choice_height"
            android:layout_alignParentEnd="true"
            android:layout_centerVertical="true"
            android:layout_margin="@dimen/margin_5dp"
            android:background="@drawable/dropdownarrow"/>
    </RelativeLayout>

drawable_border_profile.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#80FFFFFF"/>
    <stroke
        android:width="0.5dp"
        android:color="@color/light_gray"/>
    <padding
        android:bottom="1dp"
        android:left="1dp"
        android:right="1dp"
        android:top="1dp"/>
</shape>

然后在RelativeLayout上设置OnClickListener。在那你可以打开弹出窗口

LayoutInflater inflater = (LayoutInflater)parentActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.pop_up_window, null);
    RelativeLayout layout1 = holder.relativeLayout_multiChoiceDropDown;

    pw = new PopupWindow(layout, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);

    pw.setBackgroundDrawable(new BitmapDrawable());
    pw.setTouchable(true);
    pw.setOutsideTouchable(true);
    pw.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

    pw.setTouchInterceptor(new View.OnTouchListener()
    {
        public boolean onTouch(View v, MotionEvent event)
        {
            if (event.getAction() == MotionEvent.ACTION_OUTSIDE)
            {
                pw.dismiss();
                return true;
            }
            return false;
        }
    });

pw.setContentView(layout);
pw.showAsDropDown(layout1, -5, 0);

final ListView list = (ListView) layout.findViewById(R.id.dropDownList);
Adapter_DropDown adapter = new Adapter_DropDown(parentActivity, items);
list.setAdapter(adapter);

pop_up_window.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/PopUpView"
              xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="@color/qatool_basic_info_dropdown"
              android:orientation="vertical">

<ListView
    android:id="@+id/dropDownList"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:divider="#000"
    android:dividerHeight="0.5dp">
</ListView>

答案 1 :(得分:0)

您可以使用customArray Adapter

public class Adapter_Spinner extends ArrayAdapter
{
    Context context;
    List<Response_IdDesc> dataArray;

    public Adapter_Spinner(Context context, int textViewResourceId, List<Response_IdDesc> dataArray)
    {
        super(context, textViewResourceId, dataArray);
        this.dataArray = dataArray;
        this.context = context;

    }

    @Override public Response_IdDesc getItem(int position)
    {
        return dataArray.get(position);
    }

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

    @Override public int getPosition(Object item)
    {
        return dataArray.indexOf(item);
    }

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

        LayoutInflater inflater = LayoutInflater.from(context);

        View view = inflater.inflate(R.layout.rowitem_spinner_profile, parent, false);
        TextView label = (TextView) view.findViewById(R.id.textview_spinner);

        label.setText(dataArray.get(position).DESC);

        return view;
    }
}

然后像这样设置你的微调器适配器

Adapter_Spinner adapter_spinner = new Adapter_Spinner(parentActivity, R.layout.rowitem_spinner, list);
spinner.setAdapter(adapter_spinner);