RecyclerView加下拉刷新

Activity中

public class SanActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener{
    String path = "http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.billboard.billList&";
    private RecyclerView mRecyclerView;
    private List<Super.SongListBean> mDatas;
    private HomeAdapter mAdapter;
    private SwipeRefreshLayout swipeRefreshLayout;//下拉刷新
    private int count=20;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_san);
        boolean netWorkAvailable = NetWorkUtils.isNetWorkAvailable(this);
        if (!netWorkAvailable) {
            Toast.makeText(SanActivity.this, "联网:" + netWorkAvailable, Toast.LENGTH_SHORT).show();
        }
        mRecyclerView = (RecyclerView) findViewById(R.id.id_recyclerview);
        swipeRefreshLayout= (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
        swipeRefreshLayout.setOnRefreshListener(this);
        getData();
    }

    private void getData() {
        OkHttp3Utils.getInstance().doGet(path+"type=1&size="+count+"&offset=0", new GsonObjectCallback<Super>() {
            @Override
            public void onUi(Super aSuper) {
                mDatas = aSuper.getSong_list();
                mAdapter=new HomeAdapter();
                mRecyclerView.setAdapter(mAdapter);
                mRecyclerView.setLayoutManager(new LinearLayoutManager(SanActivity.this));//list
            }

            @Override
            public void onFailed(Call call, IOException e) {

            }
        });
    }



    @Override
    public void onRefresh() {//刷新
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {

                count++;
                mAdapter.notifyDataSetChanged();
                swipeRefreshLayout.setRefreshing(false);
            }
        },3000);
    }

    //RecyclerView
    class HomeAdapter extends RecyclerView.Adapter {


        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
           /*MyViewHolder holder = new MyViewHolder(LayoutInflater.from(
                    SanActivity.this).inflate(R.layout.item, parent,
                    false));*/
            View view = null;
            RecyclerView.ViewHolder holder = null;
            switch (viewType){
                case 0:
                    view = LayoutInflater.from(SanActivity.this).inflate(R.layout.item, parent, false);
                    holder = new MyViewHolder(view);
                    break;
                case 1:
                    view = LayoutInflater.from(SanActivity.this).inflate(R.layout.item1, parent, false);
                    holder = new MyViewHolder1(view);
                    break;

            }
            return holder;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
                switch (getItemViewType(position)){
                    case 0:
                        MyViewHolder holder1= (MyViewHolder) holder;
                        holder1.tv1.setText(mDatas.get(position).getTitle());
                        //ImageLoader imageLoader=ImageLoader.getInstance();
                        ImageLoader.getInstance().displayImage(mDatas.get(position).getPic_small(), holder1.imageView1);
                        break;
                    case 1:
                        MyViewHolder1 holder2= (MyViewHolder1) holder;
                        holder2.tv2.setText(mDatas.get(position).getSi_proxycompany());
                        break;
                }
        }

       /* @Override
        public void onBindViewHolder(final MyViewHolder holder, final int position) {
            holder.tv1.setText(mDatas.get(position).getTitle());
            //ImageLoader imageLoader=ImageLoader.getInstance();
            ImageLoader.getInstance().displayImage(mDatas.get(position).getPic_small(), holder.imageView1);

        }*/

        @Override
        public int getItemCount() {
            return mDatas.size();
        }

        @Override
        public int getItemViewType(int position) {
            if (position%2 == 0){
                return 0;
            } else {
                return 1;
            }
        }
        class MyViewHolder extends RecyclerView.ViewHolder {

            TextView tv1;
            ImageView imageView1;

            public MyViewHolder(View view) {
                super(view);
                tv1 = (TextView) view.findViewById(R.id.item_text);
                imageView1 = (ImageView) view.findViewById(R.id.item_img);
            }
        }
        class MyViewHolder1 extends RecyclerView.ViewHolder {

            TextView tv2;


            public MyViewHolder1(View view) {
                super(view);
                tv2 = (TextView) view.findViewById(R.id.item1_text);

            }
        }
    }
}

xml中

<?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:id="@+id/activity_san"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.bwie.disanzhoumoni.SanActivity">
    <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/id_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值