-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathScrollingActivity.java
112 lines (97 loc) · 4.63 KB
/
ScrollingActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package com.yan.refreshloadlayouttest.testactivity;
import android.os.Bundle;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import androidx.core.content.ContextCompat;
import androidx.appcompat.widget.Toolbar;
import android.util.Log;
import android.view.View;
import com.yan.pullrefreshlayout.PRLCommonUtils;
import com.yan.pullrefreshlayout.PullRefreshLayout;
import com.yan.refreshloadlayouttest.R;
import com.yan.refreshloadlayouttest.widget.house.StoreHouseHeader;
import static com.yan.pullrefreshlayout.PRLCommonUtils.dipToPx;
import java.util.ArrayList;
public class ScrollingActivity extends BaseActivity {
private static final String TAG = "NestedActivity";
private PullRefreshLayout refreshLayout;
private AppBarLayout appBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrolling);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
appBar = findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG).setAction("Action", null).show();
}
});
initRecyclerView();
initRefreshLayout();
refreshLayout.postDelayed(new Runnable() {
@Override
public void run() {
refreshLayout.autoRefresh();
}
}, 150);
}
private void initRecyclerView() {
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rv_data);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
ArrayList<SimpleItem> datas = new ArrayList<>();
datas.add(new SimpleItem(R.drawable.img1, "夏目友人帐"));
datas.add(new SimpleItem(R.drawable.img2, "夏目友人帐"));
datas.add(new SimpleItem(R.drawable.img3, "夏目友人帐"));
datas.add(new SimpleItem(R.drawable.img4, "夏目友人帐"));
datas.add(new SimpleItem(R.drawable.img5, "夏目友人帐"));
datas.add(new SimpleItem(R.drawable.img6, "夏目友人帐"));
SimpleAdapter adapter = new SimpleAdapter(this, datas);
recyclerView.setAdapter(adapter);
recyclerView.setItemAnimator(new DefaultItemAnimator());
}
private void initRefreshLayout() {
this.refreshLayout = findViewById(R.id.refreshLayout);
findViewById(R.id.container).setBackgroundColor(ContextCompat.getColor(getBaseContext(), R.color.colorPrimaryDark));
StoreHouseHeader header = new StoreHouseHeader(getBaseContext());
header.setPadding(0, dipToPx(getApplicationContext(), 20), 0, dipToPx(getApplicationContext(), 20));
header.initWithString("PullRefreshLayout");
refreshLayout.setHeaderView(header);
this.refreshLayout.setOnRefreshListener(new PullRefreshLayout.OnRefreshListenerAdapter() {
@Override
public void onRefresh() {
Log.e(TAG, "refreshLayout onRefresh: ");
ScrollingActivity.this.refreshLayout.postDelayed(new Runnable() {
@Override
public void run() {
ScrollingActivity.this.refreshLayout.refreshComplete();
}
}, 3000);
}
});
appBar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
appBarLayout.setTag(verticalOffset);
}
});
refreshLayout.setOnTargetScrollCheckListener(new PullRefreshLayout.OnTargetScrollCheckListener() {
@Override
public boolean onScrollUpAbleCheck() {
int appbarOffset = ((appBar.getTag() instanceof Integer)) ? (int) appBar.getTag() : 0;
return PRLCommonUtils.canChildScrollUp(refreshLayout.getTargetView()) || appbarOffset != 0;
}
@Override
public boolean onScrollDownAbleCheck() {
return PRLCommonUtils.canChildScrollDown(refreshLayout.getTargetView());
}
});
}
}