RecycleView的特简单使用,数据是本地的
瀑布流(RecyclerView) 的使用
上拉刷新,下拉加载更多(本地数据)。分割线。
public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
private RecyclerView res;
private ArrayList<String> arr;
private RecyclerViewAdapter adapter;
private SwipeRefreshLayout swip;
private GridLayoutManager gridmanager;
private LinearLayoutManager linearlayout;
private ProgressBar bar;
private int widthPixels;
private SwipeRefreshLayout swipeRefreshLayout;
private StaggeredGridLayoutManager staggeredGridLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
private void initView() {
//找控件
res = (RecyclerView) findViewById(R.id.rec);
bar = (ProgressBar) findViewById(R.id.bar);
//有了他就可以实现下拉刷新,上拉加载更多
swip = (SwipeRefreshLayout) findViewById(R.id.swip);
}
//加载数据
private void initData() {
arr = new ArrayList<>();
for (int i = 0; i < 30; i++) {
arr.add("haha");
}
//设置布局管理器(现在用的是线性的)。
res.setLayoutManager(linearlayout);
//瀑布流(RecyclerView)的适配器
adapter = new RecyclerViewAdapter();
res.setAdapter(adapter);
//
adapter.setData(arr);
//线性布局的
linearlayout = new LinearLayoutManager(this);
//九宫格布局的
gridmanager = new GridLayoutManager(this, 3);
//瀑布流的
staggeredGridLayoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
//划分隔线的 drawline是下面的类
res.addItemDecoration(new drawline());
//SwipeRefreshLayout的监听 ,
swip.setOnRefreshListener(this);
//上拉加载更多
res.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (dy > 0) {
//取决于你的瀑布流(recyclerView)用的什么样的布局
// int last = gridmanager.findLastVisibleItemPosition();
int last_grid = linearlayout.findLastVisibleItemPosition();
// staggeredGridLayoutManager.findLastVisibleItemPositions();
if (last_grid + 1 == adapter.getItemCount()) {
//进度条显示 加载数据
bar.setVisibility(View.VISIBLE);
//加载数据
//省略
new Thread() {
@Override
public void run() {
super.run();
try {
sleep(2000);
runOnUiThread(new Runnable() {
@Override
public void run() {
bar.setVisibility(View.GONE);
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
}
}
}
});
}
//下拉刷新
@Override
public void onRefresh() {
new Thread() {
@Override
public void run() {
super.run();
try {
sleep(3000);
runOnUiThread(new Runnable() {
@Override
public void run() {
swip.setRefreshing(false);
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
}
//自定义分割线
class drawline extends RecyclerView.ItemDecoration {
private int width;
//自己写的方法
private void getScreenSize(){
DisplayMetrics displayMetrics = new DisplayMetrics();
widthPixels = displayMetrics.widthPixels;
width = getWindowManager().getDefaultDisplay().getWidth();
}
//重写的方法
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDraw(c, parent, state);
Paint paint = new Paint();
paint.setColor(Color.RED);
getScreenSize();
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View childAt = parent.getChildAt(i);
int bottom = childAt.getBottom();
int left = childAt.getLeft();
int right = childAt.getRight();
c.drawRect(left,bottom,width,bottom+10,paint);
}
}
}
//RecyclerView适配器
class RecyclerViewAdapter extends RecyclerView.Adapter<MyViewHoder> {
private ArrayList<String> arrs = new ArrayList<>();
@Override
public MyViewHoder onCreateViewHolder(ViewGroup parent, int viewType) {
View inflate = View.inflate(MainActivity.this, R.layout.item, null);
MyViewHoder myhoder = new MyViewHoder(inflate);
return myhoder;
}
@Override
public void onBindViewHolder(MyViewHoder holder, int position) {
holder.textView.setText(arrs.get(position));
}
@Override
public int getItemCount() {
return arrs.size();
}
public void setData(ArrayList<String> arr) {
if (arr != null) {
arrs.addAll(arr);
}
}
}
//viewhoder
class MyViewHoder extends RecyclerView.ViewHolder {
private final ImageView imageView;
private final TextView textView;
public MyViewHoder(View v) {
super(v);
imageView = (ImageView) v.findViewById(R.id.image_item);
textView = (TextView) v.findViewById(R.id.text11);
}
}
}