Android的UI真是个坑。
依赖库:
用RecyclerView和SwipeRefreshLayout实现一个简单的可以顶端下拉刷新和上划自动加载的列表控件。
layout文件:
<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/swipeContainer"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" >
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/recyclerView" />
</android.support.v4.widget.SwipeRefreshLayout>
依赖库:
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:design:23.3.0'
compile 'com.android.support:cardview-v7:23.3.0'
compile 'com.android.support:recyclerview-v7:23.3.0'
compile 'com.jakewharton:butterknife:7.0.1'
代码段:
@Bind(R.id.recyclerView)
RecyclerView mRecyclerView;
MyRecyclerViewAdapter mAdapter = null;
LinearLayoutManager mLinearLayoutManager = null;
@Bind(R.id.swipeContainer)
SwipeRefreshLayout mSwipeRefreshLayout;
boolean isRefush = false;
mBingDataManager = new BingDataManager(MainActivity.this);
mLinearLayoutManager = new LinearLayoutManager(this);
//mLinearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);//设置横向布局
//设置布局管理器,这里用线性显示 类似于listview
mRecyclerView.setLayoutManager(mLinearLayoutManager);
//如果可以确定每个item的高度是固定的,设置这个选项可以提高性能
mRecyclerView.setHasFixedSize(true);
//设置默认Item增加、移除动画
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
//添加分割线
mRecyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);//默认outRect的几个值定义为0,outRect.set(0, 0, 0, 0);
if (parent.getChildAdapterPosition(view) != 0) {
outRect.top = 20;//自定义间隔距离
}
}
});
//设置adapter
mAdapter = new MyRecyclerViewAdapter(MainActivity.this, mBingDataManager.intiDataList());
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override//没有移动距离不能触发
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int lastVisibleItem = mLinearLayoutManager.findLastVisibleItemPosition();//完全显示的.findLastCompletelyVisibleItemPosition()
int totalItemCount = mLinearLayoutManager.getItemCount();
// dy>0 表示列表向下滚动
//lastVisibleItem + 1 > totalItemCount - 1 自动加载一个
//!isRefush非刷新时
if (!isRefush && dy > 0 && lastVisibleItem + 1 >= totalItemCount) {
loadMore();//加载更多
mAdapter.notifyItemInserted(totalItemCount);
}
}
});
//最多四种颜色
mSwipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light);
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
isRefush = true;
new Thread(new Runnable() {
@Override
public void run() {
reload();//重新加载数据
runOnUiThread(new Runnable() {
@Override
public void run() {
mAdapter.notifyDataSetChanged();//更新
mSwipeRefreshLayout.setRefreshing(false);
isRefush = false;
}
});
}
}).start();
}
});
还有RecyclerView自带的适配器部分,和普通的Adapter类似,只是把ViewHolder (RecyclerView.ViewHolder)也默认加入了。
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.MyViewHolder> {
private LayoutInflater mLayoutInflater;
private Context mContext;
private List<BingDataBean> itemDataList;
static final String TAG = "MyRecyclerViewAdapter";
public MyRecyclerViewAdapter(Context context,List<BingDataBean> itemDataList) {
this.itemDataList=itemDataList;
this.mContext = context;
this.mLayoutInflater = LayoutInflater.from(mContext);
}
//创建新View,被LayoutManager所调用
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new MyViewHolder(mLayoutInflater.inflate(R.layout.cardview_item, parent, false));
}
//将数据与界面进行绑定的操作
@Override
public void onBindViewHolder(final MyRecyclerViewAdapter.MyViewHolder holder, int position) {
holder.textView.setText(itemDataList.get(position).getImages().get(0).getCopyright());
Ion.with(holder.imageView).load(itemDataList.get(position).getImages().get(0).getUrl());
}
//获取数据的数量
@Override
public int getItemCount() {
return itemDataList == null ? 0 : itemDataList.size();
}
//自定义的ViewHolder,持有每个Item的的所有界面元素
public static class MyViewHolder extends RecyclerView.ViewHolder {
@Bind(R.id.imageView)
ImageView imageView;
@Bind(R.id.textView)
TextView textView;
<span style="white-space:pre"> </span>// 在 ViewHolder 中添加点击事件,也可以在onBindViewHolder中添加
MyViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e(TAG, "onClick--> position = " + getAdapterPosition());
}
});
}
}
}