Android学习之SwipeRefreshLayout+RecyclerView+CardView

首先添加包的依赖

    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);
    }
}
效果








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值