随着android5.0的发布,google还发布了SwipeRefreshLayout,RecyclerView,CardView几个有用的控件,今天我写这篇博客记录下我的使用过程,内容包括正在刷新加载,下拉刷新,上拉加载更多,无数据的提示信息的显示,也希望给这方面有疑惑的一点帮助!!!
首先当然是添加对这些控件的依赖,由于我使用的是android studio,这个很简单,添加如下的代码在build.gradle下:
compile 'com.android.support:appcompat-v7:23.1.1' compile 'com.android.support:support-v4:23.1.1' compile 'com.android.support:recyclerview-v7:23.1.1' compile 'com.android.support:cardview-v7:23.1.1'
mSwipeRefreshLayout.post(new Runnable() { @Override public void run() { mSwipeRefreshLayout.setRefreshing(true); } });
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.recycleviewdemo.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="wrap_content"/> </android.support.v4.widget.SwipeRefreshLayout> </RelativeLayout>
import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener{ private SwipeRefreshLayout mSwipeRefreshLayout; private RecyclerView mRecyclerView; private ItemsAdapter mAdapter; private List<String> mData; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swiperefreshlayout); mSwipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary, R.color.colorPrimaryDark); mSwipeRefreshLayout.setOnRefreshListener(this); mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview); WrapContentLinearLayoutManager layoutManager = new WrapContentLinearLayoutManager(this, LinearLayoutManager.VERTICAL, false){ @Override protected int getExtraLayoutSpace(RecyclerView.State state) { return 6000; } }; mRecyclerView.setLayoutManager(layoutManager); //mRecyclerView.addItemDecoration(new DividerDecoration(this)); mData = new ArrayList<>(); mSwipeRefreshLayout.post(new Runnable() { @Override public void run() { mSwipeRefreshLayout.setRefreshing(true); } }); loadData(); } private void loadData(){ new Thread(){ @Override public void run() { super.run(); try { Thread.sleep(5000); int index = mData.size(); for(int i=index;i<index+20;i++){ mData.add("第"+i+"个数据"); } runOnUiThread(new Runnable() { @Override public void run() { setAdapter(); mSwipeRefreshLayout.setRefreshing(false); } }); } catch (InterruptedException e) { e.printStackTrace(); } } }.start(); } private void setAdapter(){ if(mAdapter==null){ mAdapter = new ItemsAdapter(this,mData); mRecyclerView.setAdapter(mAdapter); }else{ mAdapter.notifyDataSetChanged(); } } @Override public void onRefresh() { mData.clear(); loadData(); } }
import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.ViewGroup; import java.util.List; /** * Created by Administrator on 2016/1/26. */ public class ItemsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private List<String> mData; private final LayoutInflater mLayoutInflater; private Context mContext; public ItemsAdapter(Context context, List<String> data) { this.mContext = context; this.mData = data; this.mLayoutInflater = LayoutInflater.from(mContext); } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { return new ItemHolder(mLayoutInflater.inflate(R.layout.item_list, parent, false)); } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { final String string = mData.get(position); ItemHolder itemHolder = (ItemHolder)holder; itemHolder.mTextView.setText(string); } @Override public int getItemCount() { return mData.size(); } }
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView android:id="@+id/cv_item" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:card_view="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="8dp" android:descendantFocusability="blocksDescendants" card_view:cardCornerRadius="4dp" card_view:cardUseCompatPadding="true"> <TextView android:id="@+id/item" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="20dp" tools:text="item"/> </android.support.v7.widget.CardView>
if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) { final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager(); recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); totalItemCount = linearLayoutManager.getItemCount(); lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition(); if (!loading && totalItemCount <= (lastVisibleItem + visibleThreshold)) { // End has been reached // Do something if (onLoadMoreListener != null) { onLoadMoreListener.onLoadMore(); } } } }); }