0

在图片链接中给出我想要的微调器

在上图中,我展示了当用户触摸下拉微调器时,它将调用 Web api 来获取微调器的数据。然后,那一刻,我只想在视图本身的左侧或右侧某处的微调器视图上显示加载器,就像在图像中一样,而不是在从 Web 服务动态获取数据并隐藏该进度时显示在整个屏幕上稍后当网络服务完全命中时(忽略图像中的搜索栏)。

4

2 回答 2

0

这是微调器的图像,即显示加载器

在此,我在开始按钮上显示加载程序,并在按下停止按钮时隐藏加载程序,以便您可以根据需要使用。因此,为此我制作了三个类 CustomSpinner、Spinner_Custom_adapter 和 Activity 类来使用它

在主布局文件中

 <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:orientation="vertical">

    <www.your_packagename.com.spinnerwithloaderex.CustomSpinner
        android:id="@+id/custm_spnr"
        android:layout_width="120dp"
        android:layout_height="40dp"
        android:layout_centerInParent="true"
        android:layout_gravity="center"
        android:background="@drawable/dropdown_create_sales"
        android:paddingRight="15dp"
        android:text="Hello World!" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp">

        <Button
            android:id="@+id/start_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Start" />

        <Button
            android:id="@+id/stop_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Stop" />

    </LinearLayout>
</LinearLayout>

CustomSpinner 类

public class CustomSpinner extends android.support.v7.widget.AppCompatSpinner {
private Spinner_Custom_adapter spinner_custom_adapter;

public CustomSpinner(Context context) {
    super(context);
}

public CustomSpinner(Context context, int mode) {
    super(context, mode);
}

public CustomSpinner(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public CustomSpinner(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

public CustomSpinner(Context context, AttributeSet attrs, int defStyleAttr, int mode) {
    super(context, attrs, defStyleAttr, mode);
}

public CustomSpinner(Context context, AttributeSet attrs, int defStyleAttr, int mode, Resources.Theme popupTheme) {
    super(context, attrs, defStyleAttr, mode, popupTheme);
}

public void setItems(Activity activity, ArrayList<String> spnr_Arr) {
    spinner_custom_adapter = new Spinner_Custom_adapter(activity, spnr_Arr);
    setAdapter(spinner_custom_adapter);
}

public Spinner_Custom_adapter getSpinner_custom_adapter() {
    return spinner_custom_adapter;
}

public void showLoader() {
    setEnabled(false);
    spinner_custom_adapter.showLoader(true, true);
}

public void dismissLoader() {
    setEnabled(true);
    spinner_custom_adapter.showLoader(false, true);
}

}

Custom_Adapter 类

public class Spinner_Custom_adapter<T> extends ArrayAdapter<T> {

private LayoutInflater flater;
private ProgressBar spinner_progress;
private TextView txtTitle;
private Boolean showOrNot = false;

Spinner_Custom_adapter(Activity context, ArrayList<T> list) {
    super(context, R.layout.loader_spinner_lt, R.id.title, list);
    flater = context.getLayoutInflater();
}

@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = flater.inflate(R.layout.loader_spinner_lt, parent, false);
    }
    Object object = getItem(position);
    String rowItem = null;
    if (object instanceof String) {
        rowItem = (String) object;
    }

    TextView txtTitle = (TextView) convertView.findViewById(R.id.title);
    txtTitle.setText(rowItem);
    ProgressBar spinner_progress = (ProgressBar) convertView.findViewById(R.id.spinner_progress);
    this.txtTitle = txtTitle;
    this.spinner_progress = spinner_progress;
    showLoader(showOrNot, false);
    return convertView;
}

void showLoader(Boolean showOrNot, boolean notifyListOrNot) {
    if (txtTitle != null && spinner_progress != null) {
        this.showOrNot = showOrNot;

        spinner_progress.setVisibility(showOrNot ? View.VISIBLE : View.GONE);
        if (notifyListOrNot) {
            notifyDataSetChanged();
        }
    }
}

}

Spinner 单视图布局 xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_vertical"
    android:gravity="center_vertical"
    android:orientation="horizontal">

    <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/title"
        style="?android:attr/spinnerDropDownItemStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/spinner_progress"
        android:ellipsize="end"
        android:singleLine="true"
        android:text="Strawberry"
        android:textColor="#CC0033"
        android:textSize="16dp" />

    <ProgressBar
        android:id="@+id/spinner_progress"
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:visibility="gone" />

</RelativeLayout>

并使用它

       custm_spnr = (CustomSpinner) findViewById(R.id.custm_spnr);
    ArrayList<String> items = new ArrayList<>();
    items.add("Abcdefg");
    items.add("hijklm");
    items.add("nopqr");
    items.add("stu");
    items.add("vwxyza1b1c1");
    items.add("d1e1f11g1h1");
    custm_spnr.setItems(MainActivity.this, items);

    findViewById(R.id.start_btn).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            custm_spnr.showLoader();
        }
    });

    findViewById(R.id.stop_btn).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            custm_spnr.dismissLoader();
        }
    });
于 2018-05-25T13:03:04.120 回答
0

只需为您的微调器创建一个自定义适配器。按照如何在 android 中使用 CustomAdapter 创建 Spinner-list 中的说明进行操作。将加载视图放在适配器的 getView 方法中膨胀的布局中,并通过用于获取结果的异步任务的回调来操作它。

于 2018-05-24T14:29:27.950 回答