3

我發現大部分材料都是關於在AppBarLayout下面添加「拉刷新」的,比如SwipeRefreshLayout,我不知道如何在AppBarLayout中這樣做,這意味着:如何在AppBarLayout中添加拉動以刷新

enter image description here

當拉下,拉來刷新指示符appeaars在AppBarLayout。

該如何實現?由於

================= UPDATE ===================

我終於解在我自己的路上! Here是錄製簡單UI的視頻鏈接。

關鍵是我從github改寫appbarlayout-spring-behavior。這真的很有幫助。簡單地說,我重寫了AppBarLayoutSpringBehavior,在拖動時添加了Pull-To-Refresh-Callback,並添加了一些邏輯來顯示拉到刷新動畫,以防止簡單地將動畫恢復到原始狀態。過了一段時間,然後我會告訴這個行爲來回動畫。

儘管重寫代碼看起來很醜,但似乎可行。我將修改我的代碼,使其乾淨和模塊化

回答

0

你可以嘗試這樣的

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.v4.widget.SwipeRefreshLayout 
     android:id="@+id/swipeRefreshLayout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:paddingTop="?attr/actionBarSize"> 

     <android.support.v7.widget.RecyclerView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_below="@+id/appBar"/> 
    </android.support.v4.widget.SwipeRefreshLayout> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <android.support.v7.widget.Toolbar 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize"/> 
    </android.support.design.widget.AppBarLayout> 
</RelativeLayout> 

在Java類視圖層次,你可以用你的SwipeRefreshLayout這樣

swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout); 
swipeRefreshLayout.setProgressViewOffset(false, 0, (int) (swipeRefreshLayout.getPaddingTop() * 1.5)); 
swipeRefreshLayout.setOnRefreshListener(() -> { 
    swipeRefreshLayout.setRefreshing(true); 
    // long process here 
    new Handler().postDelayed(() -> runOnUiThread(() -> swipeRefreshLayout.setRefreshing(false)), 2000); 
}); 
0

編輯在AppBarLayout中添加ProgressBar。

你需要用SwipeRefreshLayout像你說的來包裹ReyclerView和還添加了進度條到自定義工具欄:

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.myapplication.MainActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay"> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:gravity="center"> 

       <TextView 
        android:layout_width="0dp" 
        android:layout_height="match_parent" 
        android:layout_gravity="start" 
        android:layout_weight="1" 
        android:gravity="start|center" 
        android:text="@string/app_name" 
        android:textColor="@android:color/white" /> 

       <ProgressBar 
        android:id="@+id/a_main_progress" 
        android:layout_width="24dp" 
        android:layout_height="24dp" 
        android:layout_marginEnd="10dp" 
        android:layout_marginRight="10dp" 
        android:visibility="invisible" /> 

      </LinearLayout> 
     </android.support.v7.widget.Toolbar> 

    </android.support.design.widget.AppBarLayout> 

    <android.support.v4.widget.SwipeRefreshLayout 
     android:id="@+id/a_main_swipe" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/a_main_recycler" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

     </android.support.v7.widget.RecyclerView> 

    </android.support.v4.widget.SwipeRefreshLayout> 

</android.support.design.widget.CoordinatorLayout> 

您還需要設置監聽器在您的MainActivity:

MainActivity.java

public class MainActivity extends AppCompatActivity { 

    private SwipeRefreshLayout swipeRefreshLayout; 
    private ProgressBar progressBar; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.a_main_swipe); 
     progressBar = (ProgressBar) findViewById(R.id.a_main_progress); 

     swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 
      @Override 
      public void onRefresh() { 
       progressBar.setVisibility(View.VISIBLE); 
       Handler handler = new Handler(); 
       handler.postDelayed(new Runnable() { 
        @Override 
        public void run() { 
         // Simulates a long running task (updating data) 
         swipeRefreshLayout.setRefreshing(false); 
         progressBar.setVisibility(View.INVISIBLE); 
        } 
       }, 2000); 
      } 
     }); 

     MainAdapter mainAdapter = new MainAdapter(Arrays.asList("Hello", "World", "Bye")); 
     RecyclerView recyclerView = (RecyclerView) findViewById(R.id.a_main_recycler); 
     recyclerView.setAdapter(mainAdapter); 
     recyclerView.setLayoutManager(new LinearLayoutManager(this)); 
     recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL)); 
    } 
} 

這是一個簡單的適配器代碼,如果你需要它太:

MainAdapter.java

public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> { 

    private List<String> strings; 

    public MainAdapter(List<String> strings) { 
     this.strings = strings; 
    } 

    @Override 
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext()); 
     View view = layoutInflater.inflate(android.R.layout.simple_list_item_1, parent, false); 
     return new ViewHolder(view); 
    } 

    @Override 
    public void onBindViewHolder(ViewHolder holder, int position) { 
     holder.bind(strings.get(position)); 
    } 

    @Override 
    public int getItemCount() { 
     return strings != null ? strings.size() : 0; 
    } 

    public static class ViewHolder extends RecyclerView.ViewHolder { 
     private final TextView textView; 

     public ViewHolder(View itemView) { 
      super(itemView); 
      textView = (TextView) itemView.findViewById(android.R.id.text1); 
     } 

     public void bind(String s) { 
      textView.setText(s); 
     } 
    } 
} 

這是更新的結果:

Result ProgressBar

+0

這應該被接受爲正確的答案。 –

+0

我需要appbarlayout內部的拉到刷新指示器。當Appbarlayout移動時它應該一起移動。 – mianlaoshu

+0

你的意思是作爲AppBarLayout內的圖標? –

0

我終止了我以自己的方式解決了問題。 Here是錄製簡單UI的視頻鏈接。

關鍵是我從github改寫appbarlayout-spring-behavior。這真的很有幫助。簡單地說,我重寫了AppBarLayoutSpringBehavior,在拖動時添加了Pull-To-Refresh-Callback,並添加了一些邏輯來顯示拉到刷新動畫,以防止簡單地將動畫恢復到原始狀態。過了一段時間,然後我會告訴這個行爲來回動畫。

儘管重寫代碼看起來很醜,但似乎可行。我會重構我的代碼,使其清潔和模塊化