2013-03-14 9 views
6

Webサービスからデータをロードする際に、AutoCompleteTextViewにローディングインジケータを表示したいと考えています。オートコンプリートの右側に表示することをお勧めします(進行制御のようなf.iアニメーション - 回転ホイール)。どのようにこれを行うことができますか?AutoCompleteTextViewにローディングインジケータを追加する方法

答えて

5

ウェブサービスからデータをロードしている間に、ローディングインジケータをAutoCompleteTextViewに表示したいとします。

ウィジェットの右側に不確定な表情でProgressBarを入れて、このようなAutoCompleTextViewクラス拡張:setLoadingIndicator()方法でProgressBarへの参照を渡す

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); 
    } 

} 

を。これは、あなたがアダプタ()のアダプタ/アダプタのフィルタでWebサービス要求を行っていることを前提としています。

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に設定します。その後、そのタスクが完了するたびに、それを非表示に戻します。

0

AutoCompleteView以内にプログレスバーを追加するには、簡単に次の操作を行うことができます。

まずそのようなカスタムオートコンプリートクラスを作成します。あなたのXMLに

これを追加、その後

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); 
     } 
    } 
} 

  <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> 

注:カスタムオートコンプリートクラスのフルパスを使用してください。

次に、アクティビティまたはフラグメントで、インジケータとオートコンプリートの参照を取得します。私はこれが誰かの役に立てば幸い

city.dispayIndicator(); 

city.removeIndicator(); 

:そうのようにオートコンプリートにプログレスバーを取り付けます

city.setLoadingIndicator(indicator); 

などのように必要なときには、さらにインジケータを表示したり、削除することができます。

関連する問題