下拉选项

public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {


    private RecyclerView rv;
    private SwipeRefreshLayout srl;
String url="http://192.168.1.26:8080/test1.json";
    String ur="http://192.168.1.26:8080/test2.json";
    private ArrayList<String> list;
    private MyAdapter myAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        list = new ArrayList<>();


        OkHttpClient client = new OkHttpClient.Builder().build();
        final Request request = new Request.Builder().url(url).get().build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {


            }


            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String s = response.body().string();
                Gson gson = new Gson();
                Student student = gson.fromJson(s, Student.class);
                List<Student.ResultBean> result = student.getResult();
                for (int i=0;i<result.size();i++){
                    list.add(result.get(i).getImg());
                }
            }
        });
        initView();
    }


    private void initView() {
        rv = (RecyclerView) findViewById(R.id.rv);
        srl = (SwipeRefreshLayout) findViewById(R.id.srl);
        rv.setLayoutManager(new GridLayoutManager(this,2));
        myAdapter = new MyAdapter(this, list);
        rv.setAdapter(myAdapter);
        srl.setColorSchemeResources(R.color.colorAccent, R.color.colorPrimary);//设置刷新时进度条




       srl.setOnRefreshListener(this);
    rv.setOnScrollListener(new RecyclerView.OnScrollListener() {
        boolean isList=false;
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            if(list.size()<30){
            if(newState==2){


                    list.add("http://t1.mmonly.cc/uploads/tu/201804/9999/72c75e92da.jpg");
                list.add("http://t1.mmonly.cc/uploads/tu/201804/9999/612c551675.jpg");
                list.add("http://t1.mmonly.cc/uploads/tu/201804/9999/cb729aa278.jpg");
                list.add("http://t1.mmonly.cc/uploads/tu/201804/9999/f5ca994202.jpg");
                list.add("http://t1.mmonly.cc/uploads/tu/201804/9999/4800cbfa19.jpg");
                list.add("http://t1.mmonly.cc/uploads/tu/201804/9999/608228e852.jpg");




                myAdapter.notifyDataSetChanged();
            }}
        }


        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
            int itemCount = layoutManager.getItemCount();
            if (itemCount==list.size()) {


                isList = true;
            }
        }
    });
    }


    @Override
    public void onRefresh() {
        OkHttpClient client = new OkHttpClient.Builder().build();
        final Request request = new Request.Builder().url(ur).get().build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {


            }


            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String s = response.body().string();
                Gson gson = new Gson();
                SP sp = gson.fromJson(s, SP.class);
                List<SP.ResultBean.ProductBean> product = sp.getResult().getProduct();
                list.clear();
                for (int i=0;i<product.size();i++){


                    list.add(product.get(i).getImg());
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        myAdapter.notifyDataSetChanged();
                        srl.setRefreshing(false);
                    }
                });
            }
        });


    }
}

猜你喜欢

转载自blog.csdn.net/weixin_42376563/article/details/80657155