Android - Simple RecyclerView, SwipeRefreshLayout - Tạo RecyclerView đơn giản hiển thị một danh sách có thể refresh trong Android Studio
1. Tạo View
Thêm thẻ sau trong layout file:
2. Tạo adapter cho RecyclerView
3. Xử lý RecyclerView trong Activity
Thêm thẻ sau trong layout file:
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/refresh_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/_8sdp"> <android.support.v7.widget.RecyclerView
android:id="@+id/rcv_list"
android:layout_width="match_parent"
android:layout_height="match_parent" /> </android.support.v4.widget.SwipeRefreshLayout>
Tạo view cho item trong list: tạo một file xml: item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/_8sdp"
android:background="@color/background_color"
android:padding="@dimen/_8sdp"> <TextView
android:id="@+id/item_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/_8sdp"
android:paddingLeft="@dimen/_16sdp"
android:paddingRight="@dimen/_8sdp"
android:paddingTop="@dimen/_8sdp"
android:text=".............."
android:textSize="@dimen/text_size_small"
android:textStyle="bold" />
</RelativeLayout>
Tạo ViewHolder cho item trong RecyclerView:
public class CustomViewHolder extends RecyclerView.ViewHolder { private TextView title; public CustomViewHolder(View itemView) { super(itemView);
title = (TextView) itemView.findViewById(R.id.item_title); } public void bindData() { //// TODO: 3/22/2017 bind data here } public void unbindData() { // TODO: 3/22/2017 release data here } }
Tạo Adapter:
public class RecyclerViewAdapter extends RecyclerView.Adapter<CustomViewHolder> { private View.OnClickListener onItemClickListener; public void addData() { notifyDataSetChanged(); } public void clearData() { notifyDataSetChanged(); } public void setOnItemClickListener(View.OnClickListener onItemClickListener) { this.onItemClickListener = onItemClickListener; } @Override
public GroupViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { CustomViewHolder viewHolder = new CustomViewHolder
(LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_layout, parent, false));
viewHolder.itemView.setOnClickListener(onItemClickListener);
return viewHolder; } @Override
public void onBindViewHolder(CustomViewHolder holder, int position) { holder.bindData(); } @Override
public int getItemCount() { return 0; } }
private SwipeRefreshLayout swipeRefreshLayout;private RecyclerView recyclerView;
private RecyclerViewAdapter adapter;
swipeRefreshLayout = (SwipeRefreshLayout)
findViewById(R.id.refresh_list);swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { refreshData(); } });
recyclerView = (RecyclerView) findViewById(R.id.rcv_list);recyclerView.setLayoutManager(newLinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));adapter = new RecyclerViewAdapter();adapter.setOnItemClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } });recyclerView.setAdapter(adapter);4. Xử lý các sự kiện thêm, xóa, click, animation
.....
Nhận xét
Đăng nhận xét