首先添加包的依赖
compile 'com.android.support:recyclerview-v7:24.2.1'
compile 'com.android.support:cardview-v7:22.2.1'
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.konghao.recyclerview.MainActivity">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/SwipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="@+id/RecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/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">
<TextView
android:layout_margin="10dp"
android:textSize="18sp"
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.v7.widget.CardView>
</LinearLayout>
User类
public class User {
private String name;
private int id;
public User(String name,int id){
this.id = id;
this.name = name;
}
public int getId(){
return id;
}
public String getName(){
return name;
}
}
适配器UserAdapter.java
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.ViewHolder>{
private List<User> users;
static class ViewHolder extends RecyclerView.ViewHolder{
TextView name;
public ViewHolder(View view){
super(view);
name = (TextView) view.findViewById(R.id.name);
}
}
public UserAdapter(List<User> users){
this.users = users;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item,parent,false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
User user = users.get(position);
holder.name.setText(user.getName());
}
@Override
public int getItemCount() {
return users.size();
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener{
private List<User> users = new ArrayList<>();
private RecyclerView recyclerView;
//private LinearLayout linearLayout;
private SwipeRefreshLayout swipeRefreshLayout;
private UserAdapter adapter;
private static final int UPDATE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.RecyclerView);
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.SwipeRefreshLayout);
swipeRefreshLayout.setOnRefreshListener(this);
//linearLayout = (LinearLayout) findViewById(R.id.visible_layout);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
initData();
}
Handler handler = new Handler(){
public void handleMessage(Message msg){
super.handleMessage(msg);
switch(msg.what){
case UPDATE:{
//linearLayout.setVisibility(View.GONE);
swipeRefreshLayout.setRefreshing(false);
}
break;
}
}
};
public void initData(){
for(int i=0;i<30;i++){
users.add(new User("孔昊" + i,i));
}
adapter = new UserAdapter(users);
recyclerView.setAdapter(adapter);
}
@Override
public void onRefresh() {
//linearLayout.setVisibility(View.VISIBLE);
swipeRefreshLayout.setRefreshing(true);
//swipeRefreshLayout.setProgressViewOffset(false,0,100);
handler.sendEmptyMessageDelayed(UPDATE,2000);
}
}
效果