2013-03-14 34 views
6

Tôi muốn hiển thị chỉ báo tải trong AutoCompleteTextView trong khi tải dữ liệu từ dịch vụ web. Tốt hơn là nó sẽ được hiển thị ở phía bên phải của tự động hoàn thành (hoạt hình f.i. như trong điều khiển tiến trình - bánh xe quay). Làm thế nào điều này có thể được thực hiện?Cách thêm chỉ báo tải vào AutoCompleteTextView

Trả lời

5

Tôi muốn hiển thị chỉ báo tải trong AutoCompleteTextView trong khi tải dữ liệu từ dịch vụ web.

Đặt một ProgressBar với một cái nhìn không xác định trên bên phải của widget và mở rộng các lớp AutoCompleTextView như thế này:

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

} 

Vượt qua tham chiếu đến ProgressBar với phương pháp setLoadingIndicator(). Điều này giả định rằng bạn đang thực hiện các yêu cầu dịch vụ web trong bộ điều hợp (của AutoCompleteTextView)/bộ lọc của bộ điều hợp.

1

Bạn muốn sử dụng thanh tiến trình không phải là người quay. Sử dụng giao diện đồ họa để đặt chúng trên các trường AutoCompleteTextView của bạn và đặt id của chúng. Tôi đặt tôi thành 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" /> 

Sau đó, trong hoạt động của bạn/đoạn:

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

Nó được tự động có thể nhìn thấy vì vậy chúng tôi bước đầu thiết lập nó để ẩn. Sau đó, chúng tôi đặt nó thành VISIBLE trên addTextChangedListener của bạn trên AutoCompleteTextView. Sau đó, bất cứ khi nào nhiệm vụ đó hoàn thành, bạn đặt nó trở lại ẩn.

0

Để thêm một thanh tiến trình trong vòng một AutoCompleteView, bạn có thể dễ dàng làm như sau:

Đầu tiên tạo ra một lớp Tuỳ chỉnh AutoComplete như vậy:

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

Sau đó, Thêm phần này vào XML của bạn

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

NB: Sử dụng đường dẫn đầy đủ đến lớp Tự động hoàn thành tùy chỉnh của bạn

Sau đó, trong Hoạt động hoặc đoạn của bạn, bạn sẽ có được một tham chiếu đến chỉ báo cũng như AutoComplete. Gắn thanh tiến trình để Autocomplete như vậy:

city.setLoadingIndicator(indicator); 

và bạn có thể tiếp tục hiển thị hoặc loại bỏ các chỉ báo khi có mong muốn như vậy:

city.dispayIndicator(); 

city.removeIndicator(); 

Tôi hy vọng điều này sẽ giúp người .

Các vấn đề liên quan