recyclerview刷新界面能不刷新吗

下拉刷新和上拉刷新都用SwipeRefreshLayout 自带的进度条
&?xml version="1.0" encoding="utf-8"?&
&LinearLayout
xmlns:android="/apk/res/android"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.liu.swiperefreshlayoutrecyclerviewdemo.MainActivity"&
&android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"&
&android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"&
&/android.support.v7.widget.RecyclerView&
&/android.support.v4.widget.SwipeRefreshLayout&
&/LinearLayout&
&?xml version="1.0" encoding="utf-8"?&
&LinearLayout xmlns:android="/apk/res/android"
xmlns:app="/apk/res-auto"
android:orientation="vertical"
android:layout_margin="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"&
&android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
app:cardCornerRadius="10dp"
android:elevation="10dp"&
android:layout_margin="10dp"
android:textSize="18sp"
android:id="@+id/tvContent"
android:text="魂牵梦萦地"
android:layout_width="match_parent"
android:layout_height="wrap_content"/&
&/android.support.v7.widget.CardView&
&/LinearLayout&
package com.liu.swiperefreshlayoutrecyclerviewdemo.
import android.content.C
import android.support.v7.widget.RecyclerV
import android.view.LayoutI
import android.view.V
import android.view.ViewG
import android.widget.TextV
import android.widget.T
import com.liu.swiperefreshlayoutrecyclerviewdemo.R;
import java.util.L
import butterknife.BindV
import butterknife.ButterK
* Created by 刘楠 on
public class RefreshAdapter extends RecyclerView.Adapter&RecyclerView.ViewHolder& {
LayoutInflater mI
List&String&
public RefreshAdapter(Context context, List&String& datas) {
mContext =
mInflater = LayoutInflater.from(context);
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = mInflater.inflate(R.layout.item_refresh_recylerview, parent, false);
return new ItemViewHolder(itemView);
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if(holder instanceof ItemViewHolder){
ItemViewHolder itemViewHolder = (ItemViewHolder)
String str = mDatas.get(position);
itemViewHolder.mTvContent.setText(str);
public int getItemCount() {
return mDatas.size();
public int getItemViewType(int position) {
return super.getItemViewType(position);
public class ItemViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.tvContent)
TextView mTvC
public ItemViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this,itemView);
initListener(itemView);
private void initListener(View itemView) {
itemView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(mContext, "poistion "+ getAdapterPosition(), Toast.LENGTH_SHORT).show();
public void AddHeaderItem(List&String& items){
mDatas.addAll(0,items);
notifyDataSetChanged();
public void AddFooterItem(List&String& items){
mDatas.addAll(items);
notifyDataSetChanged();
package com.liu.swiperefreshla
import android.graphics.C
import android.os.B
import android.os.H
import android.support.v4.widget.SwipeRefreshL
import android.support.v7.app.AppCompatA
import android.support.v7.widget.LinearLayoutM
import android.support.v7.widget.RecyclerV
import android.view.W
import android.widget.T
import com.liu.swiperefreshlayoutrecyclerviewdemo.adapter.RefreshA
import java.util.ArrayL
import java.util.L
import butterknife.BindV
import butterknife.ButterK
public class MainActivity extends AppCompatActivity {
@BindView(R.id.recyclerView)
RecyclerView
mRecyclerV
@BindView(R.id.swipeRefreshLayout)
SwipeRefreshLayout mSwipeRefreshL
List&String& mDatas = new ArrayList&&();
private RefreshAdapter mRefreshA
private LinearLayoutManager mLinearLayoutM
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
initView();
initData();
initListener();
private void initView() {
mSwipeRefreshLayout.setColorSchemeColors(Color.RED,Color.BLUE,Color.GREEN);
private void initData() {
for (int i = 0; i & 10; i++) {
mDatas.add(" Item "+i);
initRecylerView();
private void initRecylerView() {
mRefreshAdapter = new RefreshAdapter(this,mDatas);
mLinearLayoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
mRecyclerView.setLayoutManager(mLinearLayoutManager);
mRecyclerView.setAdapter(mRefreshAdapter);
private void initListener() {
initPullRefresh();
initLoadMoreListener();
private void initPullRefresh() {
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
public void run() {
List&String& headDatas = new ArrayList&String&();
for (int i = 20; i &30 ; i++) {
headDatas.add("Heard Item "+i);
mRefreshAdapter.AddHeaderItem(headDatas);
//刷新完成
mSwipeRefreshLayout.setRefreshing(false);
Toast.makeText(MainActivity.this, "更新了 "+headDatas.size()+" 条目数据", Toast.LENGTH_SHORT).show();
private void initLoadMoreListener() {
mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
int lastVisibleI
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
//判断RecyclerView的状态 是空闲时,同时,是最后一个可见的ITEM时才加载
if(newState==RecyclerView.SCROLL_STATE_IDLE&&lastVisibleItem+1==mRefreshAdapter.getItemCount()){
new Handler().postDelayed(new Runnable() {
public void run() {
List&String& footerDatas = new ArrayList&String&();
for (int i = 0; i& 10; i++) {
footerDatas.add("footer
item" + i);
mRefreshAdapter.AddFooterItem(footerDatas);
Toast.makeText(MainActivity.this, "更新了 "+footerDatas.size()+" 条目数据", Toast.LENGTH_SHORT).show();
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
//最后一个可见的ITEM
lastVisibleItem=layoutManager.findLastVisibleItemPosition();
实现下拉刷新用SwipeRefreshLayout 自带的进度条, 上拉刷新用类似ListView的刷新 提示&加载中&等信息。
&load_more 布局
&?xml version="1.0" encoding="utf-8"?&
&LinearLayout xmlns:android="/apk/res/android"
android:id="@+id/loadLayout"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"&
&RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip"
android:layout_marginTop="5dip"
android:gravity="center"&
&ProgressBar
android:id="@+id/pbLoad"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_centerVertical="true"
android:indeterminate="false"/&
android:id="@+id/tvLoadText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:layout_marginLeft="4dip"
android:layout_toRightOf="@id/pbLoad"
android:clickable="true"
android:text="魂牵梦萦 魂牵梦萦 "
android:textColor="#000000"
android:textSize="16sp"/&
&/RelativeLayout&
&/LinearLayout&
package com.liu.swiperefreshlayoutrecyclerviewdemo.
import android.content.C
import android.support.v7.widget.RecyclerV
import android.view.LayoutI
import android.view.V
import android.view.ViewG
import android.widget.LinearL
import android.widget.ProgressB
import android.widget.TextV
import android.widget.T
import com.liu.swiperefreshlayoutrecyclerviewdemo.R;
import java.util.L
import butterknife.BindV
import butterknife.ButterK
* Created by 刘楠 on
public class RefreshAdapter extends RecyclerView.Adapter&RecyclerView.ViewHolder& {
LayoutInflater mI
List&String&
private static final int TYPE_ITEM
private static final int TYPE_FOOTER = 1;
//上拉加载更多
public static final int PULLUP_LOAD_MORE = 0;
//正在加载中
public static final int LOADING_MORE
//没有加载更多 隐藏
public static final int NO_LOAD_MORE
//上拉加载更多状态-默认为0
private int mLoadMoreStatus = 0;
public RefreshAdapter(Context context, List&String& datas) {
mContext =
mInflater = LayoutInflater.from(context);
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
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 = mInflater.inflate(R.layout.load_more_footview_layout, parent, false);
return new FooterViewHolder(itemView);
return null;
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof ItemViewHolder) {
ItemViewHolder itemViewHolder = (ItemViewHolder)
= mDatas.get(position);
itemViewHolder.mTvContent.setText(str);
} else if (holder instanceof FooterViewHolder) {
FooterViewHolder footerViewHolder = (FooterViewHolder)
switch (mLoadMoreStatus) {
case PULLUP_LOAD_MORE:
footerViewHolder.mTvLoadText.setText("上拉加载更多...");
case LOADING_MORE:
footerViewHolder.mTvLoadText.setText("正加载更多...");
case NO_LOAD_MORE:
//隐藏加载更多
footerViewHolder.mLoadLayout.setVisibility(View.GONE);
public int getItemCount() {
//RecyclerView的count设置为数据总条数+ 1(footerView)
return mDatas.size() + 1;
public int getItemViewType(int position) {
if (position + 1 == getItemCount()) {
//最后一个item设置为footerView
return TYPE_FOOTER;
return TYPE_ITEM;
public class ItemViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.tvContent)
TextView mTvC
public ItemViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
initListener(itemView);
private void initListener(View itemView) {
itemView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(mContext, "poistion " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
public class FooterViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.pbLoad)
ProgressBar
@BindView(R.id.tvLoadText)
@BindView(R.id.loadLayout)
LinearLayout mLoadL
public FooterViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this,itemView);
public void AddHeaderItem(List&String& items) {
mDatas.addAll(0, items);
notifyDataSetChanged();
public void AddFooterItem(List&String& items) {
mDatas.addAll(items);
notifyDataSetChanged();
* 更新加载更多状态
* @param status
public void changeMoreStatus(int status){
mLoadMoreStatus=
notifyDataSetChanged();
变更加载更多方法
private void initLoadMoreListener() {
mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
int lastVisibleI
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
//判断RecyclerView的状态 是空闲时,同时,是最后一个可见的ITEM时才加载
if(newState==RecyclerView.SCROLL_STATE_IDLE&&lastVisibleItem+1==mRefreshAdapter.getItemCount()){
//设置正在加载更多
mRefreshAdapter.changeMoreStatus(mRefreshAdapter.LOADING_MORE);
new Handler().postDelayed(new Runnable() {
public void run() {
List&String& footerDatas = new ArrayList&String&();
for (int i = 0; i& 10; i++) {
footerDatas.add("footer
item" + i);
mRefreshAdapter.AddFooterItem(footerDatas);
//设置回到上拉加载更多
mRefreshAdapter.changeMoreStatus(mRefreshAdapter.PULLUP_LOAD_MORE);
Toast.makeText(MainActivity.this, "更新了 "+footerDatas.size()+" 条目数据", Toast.LENGTH_SHORT).show();
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
//最后一个可见的ITEM
lastVisibleItem=layoutManager.findLastVisibleItemPosition();
阅读(...) 评论()android recyclerview刷新问题_百度知道主题信息(必填)
主题描述(最多限制在50个字符)
申请人信息(必填)
申请信息已提交审核,请注意查收邮件,我们会尽快给您反馈。
如有疑问,请联系
傻丫头和高科技产物小心翼翼的初恋
如今的编程是一场程序员和上帝的竞赛,程序员要开发出更大更好、傻瓜都会用到软件。而上帝在努力创造出更大更傻的傻瓜。目前为止,上帝是赢的。个人网站:。个人QQ群:、
CSDN &《程序员》编辑/记者,我的邮箱
个人大数据技术博客:
一只文艺范的软件攻城狮,Keep Learn,Always.
一个实现了下拉刷新,滚动到底部加载更多以及添加header功能的的RecyclerView。使用方式和RecyclerView完全一致,不需要额外的layout,不需要写特殊的adater。 加载效果内置了AVLoadingIndicatorView上的所有效果,可以根据需要指定。

我要回帖

更多关于 recyclerview刷新界面 的文章

 

随机推荐