2013-03-14 64 views

回答

5

我想在AutoCompleteTextView中顯示加載指示器,而 從網絡服務加載數據。

將一個ProgressBar對widget的右側不確定的外觀和延長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服務請求。

1

你想使用進度條而不是spinners。使用圖形視圖將它們放置在AutoCompleteTextView字段上並設置其ID。我設定了我的進度加載。

<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上可見。然後,無論何時完成任務,都將其設置回隱藏狀態。

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> 

注意:使用自定義自動完成類的完整路徑

然後,在您的活動或片段中,您將獲得對該指標的參考以及自動完成。安裝進度條來自動完成,像這樣:

city.setLoadingIndicator(indicator); 

和期望的一樣,所以當你可以進一步顯示或刪除指示:

city.dispayIndicator(); 

city.removeIndicator(); 

我希望這可以幫助別人。

相關問題