【问题标题】:Show loader on Spinner (Dropdown) when it is fetching data from web service从 Web 服务获取数据时在 Spinner (Dropdown) 上显示加载器
【发布时间】:2018-11-03 19:31:16
【问题描述】:

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

【问题讨论】:

    标签: android android-layout spinner android-progressbar custom-arrayadapter


    【解决方案1】:

    在这里,我在开始按钮上显示加载程序,并在按下停止按钮时隐藏加载程序,以便您可以根据需要使用。因此,为此我制作了三个类 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();
            }
        }
    }
    

    }

    微调器单视图布局 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();
            }
        });
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 已经通过在自定义布局 xml 中采用进度条并根据需要隐藏/显示来尝试它,但是当我将加载器可见性设置为可见时它没有显示加载器,只显示文本
      • 在这种情况下,在适配器中使用一个标志来设置进度条的可见性,在 createView 方法中使用它来设置进度条的可见性,并在每次可见性更改后调用 adapter.notifyDatasetChanged()。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-27
      • 2016-08-10
      • 2022-06-15
      • 2011-02-06
      相关资源
      最近更新 更多