Android Custom Multispinner:实现FadingEdge滚动

时间:2016-06-02 02:48:43

标签: android spinner android-spinner fade

我是一个Android新手,并在这里使用了代码:https://stackoverflow.com/a/17577310/6412402 创建一个自定义的Multispinner(它很棒!)我的图形设计师朋友讨厌我/当前的布局,并想要一个类似iOS的滚动,用户已经通过视图淡出的选项(类似于scrollview的fadingedge长度)我给这个自定义微调器一个类似于此的渐弱边长: Fading Scroll

除了淡化滚动图像仅用于文本,我希望自定义微调器中的项具有此属性。老实说,它不必看起来像我们只需要某种褪色属性的图片。请帮忙! 以下是java类Multispinner中的代码:

package packagename;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import java.util.List;

public class MultiSpinner extends Spinner {

private CharSequence[] entries;
private boolean[] selected;
private MultiSpinnerListener listener;

public MultiSpinner(Context context, AttributeSet attrs) {
    super(context, attrs);
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MultiSpinner);
    entries = a.getTextArray(R.styleable.MultiSpinner_android_entries);
    if (entries != null) {
        selected = new boolean[entries.length]; // false-filled by default
    }
    a.recycle();
}
private OnMultiChoiceClickListener mOnMultiChoiceClickListener = new OnMultiChoiceClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
        selected[which] = isChecked;
    }
};
private DialogInterface.OnClickListener mOnClickListener = new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        // build new spinner text & delimiter management
        StringBuffer spinnerBuffer = new StringBuffer();
        for (int i = 0; i < entries.length; i++) {
            if (selected[i]) {
                spinnerBuffer.append(entries[i]);
                spinnerBuffer.append(", ");
            }
        }

        // Remove trailing comma
        if (spinnerBuffer.length() > 2) {
            spinnerBuffer.setLength(spinnerBuffer.length() - 2);
        }

        // display new text
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),
                android.R.layout.simple_spinner_item,
                new String[] { spinnerBuffer.toString() });
        setAdapter(adapter);

        if (listener != null) {
            listener.onItemsSelected(selected);
        }

        // hide dialog
        dialog.dismiss();
    }
};

@Override
public boolean performClick() {
    new AlertDialog.Builder(getContext())
            .setMultiChoiceItems(entries, selected, mOnMultiChoiceClickListener)
            .setPositiveButton(android.R.string.ok, mOnClickListener)
            .show();
    return true;
}

public void setMultiSpinnerListener(MultiSpinnerListener listener) {
    this.listener = listener;
}

public interface MultiSpinnerListener {
    public void onItemsSelected(boolean[] selected);
}
}

和我的xml代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fadingEdgeLength="400dp"
android:requiresFadingEdge="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="edu.ucsb.cs.cs185.project.cookey.ScrollingActivity"
tools:showIn="@layout/activity_scrolling">

<packagename.MultiSpinner
    android:id="@+id/multispinner"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:entries="@array/multispinner_entries"
    android:fadingEdgeLength="300dp"
    android:requiresFadingEdge="vertical"/>
</android.support.v4.widget.NestedScrollView>

0 个答案:

没有答案