【问题标题】:How to add loading indicator to AutoCompleteTextView如何将加载指示器添加到 AutoCompleteTextView
【发布时间】:2017-01-26 00:14:50
【问题描述】:

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

【问题讨论】:

    标签: android autocompletetextview


    【解决方案1】:

    我想在 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);
        }
    
    }
    

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

    【讨论】:

      【解决方案2】:

      您想使用进度条而不是微调器。使用图形视图将它们放置在您的 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。然后,每当该任务完成时,您将其设置回隐藏。

      【讨论】:

        【解决方案3】:

        要在 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();
        

        我希望这对某人有所帮助。

        【讨论】:

          猜你喜欢
          • 2015-09-28
          • 1970-01-01
          • 2013-10-30
          • 1970-01-01
          • 2023-03-21
          • 2012-03-12
          • 1970-01-01
          • 1970-01-01
          • 2011-06-03
          相关资源
          最近更新 更多