6

我想在从 web 服务加载数据时在 AutoCompleteTextView 中显示加载指示器。最好它应该显示在自动完成的右侧(fi 动画,如正在进行的控制 - 旋转轮)。如何做到这一点?

4

3 回答 3

7

我想在从 web 服务加载数据时在 AutoCompleteTextView 中显示加载指示器。

ProgressBar在小部件的右侧放置一个不确定的外观并AutoCompleTextView像这样扩展类:

public class AutoCompleteLoadding extends AutoCompleteTextView {

    private ProgressBar mLoadingIndicator;

    public void setLoadingIndicator(ProgressBar view) {
        mLoadingIndicator = view;
    }

    @Override
    protected void performFiltering(CharSequence text, int keyCode) {
        // the AutoCompleteTextview is about to start the filtering so show
        // the ProgressPager
        mLoadingIndicator.setVisibility(View.VISIBLE);
        super.performFiltering(text, keyCode);
    }

    @Override
    public void onFilterComplete(int count) {
        // the AutoCompleteTextView has done its job and it's about to show
        // the drop down so close/hide the ProgreeBar
        mLoadingIndicator.setVisibility(View.INVISIBLE);
        super.onFilterComplete(count);
    }

}

ProgressBar使用方法传递对 的引用setLoadingIndicator()。这假设您在适配器(的AutoCompleteTextView)/适配器的过滤器中发出 Web 服务请求。

于 2013-03-14T09:39:03.210 回答
1

您想使用进度条而不是微调器。使用图形视图将它们放置在您的 AutoCompleteTextView 字段上并设置它们的 id。我将我的设置为progressLoading。

<ProgressBar
    android:id="@+id/progressLoading"
    style="?android:attr/progressBarStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/autoCompleteTextViewId"
    android:layout_alignTop="@+id/autoCompleteTextViewId"
    android:paddingTop="14dip"
    android:paddingRight="10dip" />

然后在您的活动/片段中:

final ProgressBar barProgress = (ProgressBar) findViewById(R.id.progressLoading);
originProgress.setVisibility(View.GONE);

它是自动可见的,因此我们最初将其设置为隐藏。然后,我们在 AutoCompleteTextView 上的 addTextChangedListener 上将其设置为 VISIBLE。然后,每当该任务完成时,您将其设置回隐藏。

于 2013-10-20T23:58:27.697 回答
0

要在 AutoCompleteView 中添加进度条,您可以轻松执行以下操作:

首先创建一个自定义自动完成类,如下所示:

public class AutoCompleteWithLoading extends AutoCompleteTextView{
    private ProgressBar mLoadingIndicator;


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

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

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

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public AutoCompleteWithLoading(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @TargetApi(Build.VERSION_CODES.N)
    public AutoCompleteWithLoading(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes, Resources.Theme popupTheme) {
        super(context, attrs, defStyleAttr, defStyleRes, popupTheme);
    }


    public void setLoadingIndicator(ProgressBar progressBar) {
        mLoadingIndicator = progressBar;
    }

    public void dispayIndicator(){
        if(mLoadingIndicator != null){
            mLoadingIndicator.setVisibility(View.VISIBLE);
        }
        this.setText("");
    }

    public void removeIndicator(){
        if(mLoadingIndicator != null){
            mLoadingIndicator.setVisibility(View.GONE);
        }
    }
}

然后,将此添加到您的 XML

         <thriviastudios.com.views.view.AutoCompleteWithLoading
                android:padding="10dp"
                android:layout_margin="5dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                style="@style/NotPresentationText"
                android:id="@+id/activity_activities_search_city_text_view"
                android:popupBackground="@color/app_bg"
                android:layout_centerInParent="true"
                android:background="@color/app_bg_darker"
                android:singleLine="true"
                android:hint="City"
                android:imeOptions="actionDone"
                android:textColorHint="@color/white"
                />


                <ProgressBar
                    android:id="@+id/loading_indicator"
                    style="?android:attr/progressBarStyleSmall"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical|right"
                    android:layout_marginRight="20dp"
                    android:visibility="gone"/>
            </FrameLayout>

注意:使用自定义 AutomComplete 类的完整路径

然后,在您的 Activity 或 Fragment 中,您将获得对指标以及 AutoComplete 的引用。将进度条附加到自动完成,如下所示:

city.setLoadingIndicator(indicator);

并且您可以在需要时进一步显示或删除指示器,如下所示:

city.dispayIndicator();

city.removeIndicator();

我希望这可以帮助别人。

于 2017-01-25T16:14:50.267 回答