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,并添加了一些逻辑来显示拉到刷新动画,以防止简单地将动画恢复到原始状态。过了一段时间,然后我会告诉这个行为来回动画。

尽管重写代码看起来很丑,但似乎可行。我会重构我的代码,使其清洁和模块化