下拉刷新

来源:互联网 发布:java2和java的区别 编辑:程序博客网 时间:2024/04/29 16:29
package com.example.nan.activity;import android.os.Bundle;import android.support.v4.widget.SwipeRefreshLayout;import android.support.v7.app.AppCompatActivity;import android.support.v7.widget.LinearLayoutManager;import android.support.v7.widget.RecyclerView;import android.widget.Toast;import com.example.nan.R;import com.example.nan.adapter.Good_select_adapter;import com.example.nan.bean.BaseBean;import com.example.nan.bean.Good_selectBean;import com.example.nan.net.Api;import com.example.nan.net.HttpUtil;import com.example.nan.net.OnNetListener;import java.io.IOException;import java.util.ArrayList;import java.util.List;public class GoodsListActivity extends AppCompatActivity {    private Good_select_adapter adapter;    private RecyclerView mRvGoodSelect;    private SwipeRefreshLayout mSrl;    private List<Good_selectBean.DatasBean.GoodsListBean> list = new ArrayList<>();    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_goods_list);        initView();//        Intent intent = getIntent();//        String gcId = intent.getStringExtra("gcId");//        Toast.makeText(GoodsListActivity.this,gcId,Toast.LENGTH_LONG).show();        Info(true);    }    private void initView() {        mRvGoodSelect = (RecyclerView) findViewById(R.id.rv_good_select);        mRvGoodSelect.setLayoutManager(new LinearLayoutManager(GoodsListActivity.this));        adapter = new Good_select_adapter(GoodsListActivity.this, list);        mRvGoodSelect.setAdapter(adapter);        mSrl = (SwipeRefreshLayout) findViewById(R.id.srl);        mSrl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {            @Override            public void onRefresh() {                Info(true);            }        });    }    private void Info(final boolean isRefrsh) {        HttpUtil.getInstance(this).doGet(Api.GOODSELECT, Good_selectBean.class, new OnNetListener() {            @Override            public void onSuccess(BaseBean baseBean) throws IOException {                Good_selectBean good_selectBean = (Good_selectBean) baseBean;                List<Good_selectBean.DatasBean.GoodsListBean> goods_list = good_selectBean.getDatas().getGoods_list();                adapter.setOnItemListener(new Good_select_adapter.OnItemListener() {                    @Override                    public void onItemCilck(Good_selectBean.DatasBean.GoodsListBean goodsListBean) {                        Toast.makeText(GoodsListActivity.this,goodsListBean.getGoods_price(),Toast.LENGTH_LONG).show();                    }                });                if (isRefrsh) {                    adapter.refreshData(goods_list);                    mSrl.setRefreshing(false);                }            }            @Override            public void onError(IOException e) {            }        });    }}
-----------------------------------------------------------------------------------------------------------------------
<?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"tools:context="com.example.nan.activity.GoodsListActivity"><android.support.v4.widget.SwipeRefreshLayout    android:id="@+id/srl"    android:layout_width="match_parent"    android:layout_height="match_parent">    <android.support.v7.widget.RecyclerView        android:id="@+id/rv_good_select"        android:layout_width="match_parent"        android:layout_height="match_parent"></android.support.v7.widget.RecyclerView></android.support.v4.widget.SwipeRefreshLayout></LinearLayout>
原创粉丝点击