SwipeRefreshLayout
SwipeRefreshLayout 是一个下拉刷新的控件。并且提供了一些方法:
(1)setOnRefreshListener(OnRefreshListener): 为布局添加一个Listener,就是那个下拉的监听,方法里面就是实现发生了下拉后要做什么的操作。
(2)setRefreshing(boolean): 显示或隐藏刷新进度条
(3)isRefreshing(): 检查是否处于刷新状态
(4)setColorSchemeColors(): 设置进度条的颜色。
RecyclerView
recyclerView是一个很强大的滑动组件
老实说对于组件的介绍我真是是词穷啊。算了还是直接上主菜吧。
XML的主要代码如下:
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/srl_swiperefresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rlv_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
Java代码如下:
Adapter:实现显示的内容的适配,就是RecyclerView的适配。实例中只是实现一个Textview的显示。还有的就是对上拉出现的加载动画进行控制。
public class RefreshAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
Context mContext;
LayoutInflater mInflater;
List<String> mDatas = new ArrayList<>();
private static final int TYPE_ITEM = 0;
private static final int TYPE_FOOTER = 1;
//上拉加载更多
public static final int PULLUP_LOAD_MORE = 0;
//正在加载中
public static final int LOADING_MORE = 1;
//没有加载更多 隐藏
public static final int NO_LOAD_MORE = 2;
//上拉加载更多状态-默认为0
private int mLoadMoreStatus = 0;
public RefreshAdapter(Context mContext, List<String> mDatas) {
this.mContext = mContext;
this.mDatas = mDatas;
mInflater = LayoutInflater.from(mContext);
}
/**
* 更新加载更多状态
*
* @param status
*/
public void changeMoreStatus(int status) {
mLoadMoreStatus = status;
notifyDataSetChanged();
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//实现ViewHolder
Log.e("dsfadf", "onCreateViewHolder: "+"假的吧");
if (viewType == TYPE_ITEM) {
View itemView = mInflater.inflate(R.layout.item_refresh_recylerview, parent, false);
return new ItemViewHolder(itemView);
} else if (viewType == TYPE_FOOTER) {
View itemView