Android 微调器内的进度条

Progress Bar inside Spinner in Android

当我的 Android 应用程序的 Spinner 上有 0 个项目时,我试图在 Spinner 中添加 ProgressBar,但到目前为止没有成功。 我的 Spinner 使用简单的 ArrayAdapter,因为它只显示文本。

我曾尝试如下更改为自定义适配器,但没有成功。

public class SpinnerAdapter  extends BaseAdapter {

    ArrayList<String> result;

Context context;
private static LayoutInflater inflater = null;

public SpinnerAdapter(Context context, ArrayList<String> items) {
    result = items;
    this.context = context;
    inflater = (LayoutInflater)context.
            getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

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

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    ProgressBar bar = (ProgressBar) ((Activity)context).findViewById(R.id.spinner_item_progress_bar);

    if(result.size() > 0) {
        bar.setVisibility(View.INVISIBLE);
    } else {
        bar.setIndeterminate(true);
        bar.setVisibility(View.VISIBLE);
    }

    View rowView;

    if(convertView == null) {
        rowView = inflater.inflate(R.layout.spinner_item, null);
        TextView tv = (TextView) ((Activity)context).findViewById(R.id.spinner_item_text_view);
        tv.setText(result.get(position));
    }
    else {
        rowView = convertView;
        }

        return rowView;
    }
}

还有我的 SpinnerItem:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ProgressBar
        android:id="@+id/spinner_item_progress_bar"
        style="?android:attr/progressBarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/spinner_item_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />
</LinearLayout>

我在我的 iOS 应用程序上做了同样的事情,我将添加一张图片,这样我就可以在我的 Android 应用程序上显示我想要的行为。

因此,这是一个带有 UIActivityIndi​​catorView 的 "Loading ..." UIView。当 UIPickerView 中有 0 个项目时,此视图将添加到 UIPickerView。每当一个项目被添加到选择器时,这个 UIView 就会被删除。在 iOS.

上完美运行

关于如何在 Android 上执行此操作的任何线索?

谢谢。

例如,您可以使用 "ViewSwitcher" 来完成。将两者都放在 viewswitcher 中,然后在您有数据时放在代码中,只需在切换器 "showNext()" 或 "showPrevius()"...

上调用方法
<ViewSwitcher
        android:id="@+id/switcher"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

<ProgressBar
        android:id="@+id/spinner_item_progress_bar"
        style="?android:attr/progressBarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
<Spinner
       android:id="@+id/spinner_item_progress_bar"
        style="?android:attr/progressBarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"


</ViewSwitcher>

问题已解决。所以,我保留了基本的 ArrayAdapter 而不是重载 BaseAdapter。然后,我转到包含 Spinner 的 Fragment 的布局文件并添加以下内容:

<RelativeLayout
    android:padding="5dp">

    <Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/search_spinner" />

        <LinearLayout
            android:orientation="horizontal"
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ProgressBar
                android:id="@+id/progressBar"
                style="?android:attr/progressBarStyleSmall"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

                <TextView
                    android:id="@+id/spinner_item_progress_bar_label"
                    android:text="Carregando..."
                    android:paddingLeft="8dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
    </LinearLayout>
</RelativeLayout>

然后,在我的 Fragment class 上,当我希望栏可见时:

ProgressBar bar = (ProgressBar) getActivity().findViewById(R.id.progressBar);
bar.setIndeterminate(true);
bar.setVisibility(View.VISIBLE);

当我想让它消失时:

bar.setVisibility(View.INVISIBLE);

结局是这样的,我比较满意: