Repository navigation
Expand file tree
/
Copy pathMainActivity.java
More file actions
154 lines (118 loc) · 5.26 KB
/
Copy pathMainActivity.java
File metadata and controls
154 lines (118 loc) · 5.26 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package com.example.asus.myload;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private List<String> data = new ArrayList<>();
private LinearLayoutManager layoutManager;
private boolean isReady = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = (RecyclerView) findViewById(R.id.recy);
layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
for (int i = 0; i < 50; i++) {
data.add("Data" + i);
}
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setAdapter(new MyRecyclerViewAdapter());
mRecyclerView.addOnScrollListener(new MyScroll());
}
class MyScroll extends RecyclerView.OnScrollListener {
int minBottom;
int bottom;
boolean isReady = false;
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
if ((layoutManager.findLastCompletelyVisibleItemPosition() == layoutManager.getItemCount() - 1)) {
if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {//滑动
isReady = true;
//判断footer是否还是完全显示
if (layoutManager.findLastCompletelyVisibleItemPosition() == layoutManager.getItemCount() - 1) {
Log.e("DRAGGING", "松手加载");
int position = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastVisibleItemPosition();
View lastItemView = layoutManager.findViewByPosition(position);
bottom = lastItemView.getBottom();
Log.e("DRAGGING", "Bottom" + bottom);
if (minBottom > bottom) {
minBottom = bottom;
}
}
} else if (newState == RecyclerView.SCROLL_STATE_SETTLING) {//抛
Log.e("SETTLING", "抛");
if ((bottom >= minBottom) && isReady) {
Log.e("SETTLING", "加载,上滑");
isReady = false;
} else {
Log.e("SETTLING", "不加载");
isReady = false;
}
} else if (newState == RecyclerView.SCROLL_STATE_IDLE) {//闲置
Log.e("IDLE", "Bottom" + bottom + "---" + minBottom);
if ((bottom >= minBottom) && isReady) {
Log.e("IDLE", "加载,上滑");
isReady = false;
} else {
Log.e("IDLE", "不加载");
isReady = false;
}
}
} else if (layoutManager.findLastCompletelyVisibleItemPosition() != layoutManager.getItemCount() - 1) {//没有在底部变化的时候
isReady = false;
} else if (newState == RecyclerView.SCROLL_STATE_SETTLING) {//抛
isReady = false;
Log.e("Yunr", "外部抛");
}
}
}
class MyRecyclerViewAdapter extends RecyclerView.Adapter<MainActivity.ViewHolder> {
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == 1) {
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.recy_item, parent, false);
return new ViewHolder(view);
} else if (viewType == 2) {
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.recy_item, parent, false);
return new ViewHolder(view);
} else {
return null;
}
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
if (getItemViewType(position) == 1) {
holder.textView.setText(data.get(position));
} else {
holder.textView.setText("上拉加载");
}
}
@Override
public int getItemCount() {
return data.size() + 1;
}
@Override
public int getItemViewType(int position) {
if (position == getItemCount() - 1)
return 2;
return 1;
}
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView textView;
public ViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.recycler_tv_item);
}
}
}