之前寫刷新數據的時候都會使用自定義的控件實現,現在Google給我們提供了更加方便的,剛寫完,感覺還挺好看的,希望讀到這篇文章的你能夠喜歡。
SwipeRefreshLayout
簡介
SwipeRefreshLayout組件只接受一個子組件:即需要刷新的那個組件。它使用一個偵聽機制來通知擁有該組件的監聽器有刷新事件發生,換句話說我們的Activity必須實現通知的接口。該Activity負責處理事件刷新和刷新相應的視圖。一旦監聽者接收到該事件,就決定了刷新過程中應處理的地方。如果要展示一個“刷新動畫”,它必須調用setRefrshing(true),否則取消動畫就調用setRefreshing(false)。
RecyclerView
簡介
RecyclerView與ListView原理是類似的:都是僅僅維護少量的View並且可以展示大量的數據集。
廢話不多說了,直接上代碼:
activity_recycler.xml佈局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#cccccc"
android:orientation="vertical">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/srl_title"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
RecyclerActivity 的主界面
public class RecyclerActivity extends Activity {
private String TAG = "com.lv";
private int firstVisibleItem;
private int totalItemCount;
private boolean flag = false;
//屏幕中最後一個可見子項的position
private int lastVisibleItemPosition;
//聲明 SwipeRefreshLayout
private SwipeRefreshLayout mSwipeRefreshLayout;
//聲明 RecyclerView
private RecyclerView mRecyclerview;
//聲明 LinearLayoutManager
private LinearLayoutManager mLayoutManager;
//聲明 adapter
private SampleAdapter adapter;
//假數據
private List<ItemModel> list;
//Handler 更新ui
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
for (int k = 0; k < 2; k++) {
ItemModel mitem = new ItemModel();
mitem.setTitle("用戶服務型軟件" + k);
mitem.setContent("新聞新聞新聞新聞新聞新聞新聞新聞新聞新聞");
list.add(mitem);
}
adapter.notifyDataSetChanged();
mSwipeRefreshLayout.setRefreshing(false);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler);
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.srl_title);
mRecyclerview = (RecyclerView) findViewById(R.id.rv_list);
list = new ArrayList<>();
mSwipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.color1),
getResources().getColor(R.color.color2), getResources().getColor(R.color.color3)
, getResources().getColor(R.color.color4));
// 這句話是爲了,第一次進入頁面的時候顯示加載進度條
mSwipeRefreshLayout.setRefreshing(true);
mRecyclerview.addOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == RecyclerView.SCROLL_STATE_IDLE
&& lastVisibleItemPosition == (totalItemCount - 1)) {
Log.e(TAG, "1111");
mSwipeRefreshLayout.setRefreshing(true);
handler.sendEmptyMessageDelayed(0, 1000);
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
lastVisibleItemPosition = mLayoutManager.findLastVisibleItemPosition();
firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
totalItemCount = adapter.getItemCount();
if (firstVisibleItem + lastVisibleItemPosition == totalItemCount && !flag) {
flag = true;
} else {
flag = false;
}
}
});
for (int k = 0; k < 3; k++) {
ItemModel mitem = new ItemModel();
mitem.setTitle("新聞新聞" + k);
mitem.setContent("新聞新聞新聞新聞新聞新聞新聞新聞");
list.add(mitem);
}
mRecyclerview.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerview.setLayoutManager(mLayoutManager);
mRecyclerview.setItemAnimator(new DefaultItemAnimator());
adapter = new SampleAdapter(RecyclerActivity.this, list);
mRecyclerview.setAdapter(adapter);
handler.sendEmptyMessageDelayed(0, 1000);
}
}
adapter 的代碼
/**
* Created by mac on 16/11/12.
*/
public class SampleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private List<ItemModel> list;
// enum Type{
// TYPE_ITEM ,TYPE_FOOTER
// }
private static final int TYPE_ITEM = 0;
private static final int TYPE_FOOTER = 1;
public SampleAdapter(Context context, List<ItemModel> list) {
this.context = context;
this.list = list;
}
/**
* 創建
*
* @param parent
* @param viewType
* @return
*/
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_ITEM) {
View view = LayoutInflater.from(parent.getContext()).inflate(
R.layout.list_item_text, null);
view.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT,
RecyclerView.LayoutParams.WRAP_CONTENT));
return new ItemViewHolder(view);
} else if (viewType == TYPE_FOOTER) {
// type == TYPE_FOOTER 返回footerView
View view = LayoutInflater.from(parent.getContext()).inflate(
R.layout.footerview, null);
view.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT,
RecyclerView.LayoutParams.WRAP_CONTENT));
return new FooterViewHolder(view);
}
return null;
}
/**
* 綁定Viewholder 數據
*
* @param holder
* @param position
*/
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof ItemViewHolder) {
((ItemViewHolder) holder).mtitle.setText(String.valueOf(list
.get(position).getTitle()));
((ItemViewHolder) holder).mcontent.setText(String.valueOf(list
.get(position).getContent()));
}
}
/**
* 每個item的類型
*
* @param position
* @return
*/
@Override
public int getItemViewType(int position) {
// 最後一個item設置爲footerView
if (position + 1 == getItemCount()) {
return TYPE_FOOTER;
} else {
return TYPE_ITEM;
}
}
/**
* 顯示的item的數量
*
* @return
*/
@Override
public int getItemCount() {
return list.size() + 1;
}
/**
* 底部佈局
*/
class FooterViewHolder extends RecyclerView.ViewHolder {
public FooterViewHolder(View view) {
super(view);
}
}
/**
* 每個Item 顯示的內容
*/
class ItemViewHolder extends RecyclerView.ViewHolder {
TextView mtitle, mcontent;
public ItemViewHolder(View view) {
super(view);
mtitle = (TextView) view.findViewById(R.id.tv_title);
mcontent = (TextView) view.findViewById(R.id.tv_content);
}
}
}
list_item_text.xml 每個item的佈局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/text_cirl"
android:orientation="horizontal"
android:padding="3dp">
<ImageView
android:id="@+id/iv_img"
android:layout_width="64dp"
android:layout_height="64dp"
android:padding="2dp"
android:src="@mipmap/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:orientation="vertical"
android:padding="2dp">
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:ellipsize="end"
android:lines="1"
android:textSize="18sp"
android:textStyle="bold"
tools:text="新聞的標題部分" />
<TextView
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:ellipsize="end"
android:lines="1"
android:textSize="15sp"
android:textStyle="normal"
tools:text="新聞的主體內容部分新聞的主體內容部分新聞的主體內容部分新聞的主體內容部分" />
</LinearLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#cccc" /> </LinearLayout>
另外我用的是android studio 2.2 所以在使用的時候在gradle中配置下:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.0'
compile 'com.android.support:recyclerview-v7:23.3.0'
testCompile 'junit:junit:4.12'
}
以上就是我弄的刷新數據,有興趣的可以試試,這裏面有隱藏的bug,大家可以自己發現,自己改動下吧!感興趣的同學可以添加下我的微信 13792763023 我的微信號~ 歡迎大家一起討論,共同成長~