SwiPereFreshLayout下拉和上拉

- 编辑:小昭 -

SwiPereFreshLayout下拉和上拉

 
mRefreshLayout.setColorSchemeResources(R.color.colorAccent,R.color.colorPrimary,R.color.colorPrimaryDark);
设置颜色
mRefreshLayout.setOnRefreshListener
下拉事件

图标显示
srf.setRefreshing(true);
代码



import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private SwipeRefreshLayout srl;
    private ListView ls;
    private ArrayList<Bean.ResultBean.ListBean> list =new ArrayList<>();
    private List<Bean.ResultBean.ListBean> hlist;
    private int x=1;
    private Handler han = new Handler();
    private Adapter adapter;
    private int i3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        http://v.juhe.cn/weixin/query?key=a332c6b34264527ac142764eaed9364d&pno=2
        srl = (SwipeRefreshLayout) findViewById(R.id.srl);
        ls = (ListView) findViewById(R.id.ls);


        ls.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView absListView, int i) {
                if(i== AbsListView.OnScrollListener.SCROLL_STATE_FLING&&i3==adapter.getCount()-1) {
                    han.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            wlqq();
                            x++;
                            list.addAll(hlist);
                            list.addAll(1,hlist);

                            adapter = new Adapter(list, MainActivity.this);

                            ls.setAdapter(adapter);


                            srl.setRefreshing(false);

                            Toast.makeText(MainActivity.this, x+"", Toast.LENGTH_SHORT).show();

                        }
                    }, 2000);



                }
            }

            @Override
            public void onScroll(AbsListView absListView, int i, int i1, int i2) {
                i3 = i + i1 - 1;
            }
        });
        wlqq();

        srl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                wlqq();
                x=1;
                list.addAll(hlist);
                list.addAll(1,hlist);

                adapter = new Adapter(list, MainActivity.this);

                ls.setAdapter(adapter);


                srl.setRefreshing(false);

                Toast.makeText(MainActivity.this, x+"", Toast.LENGTH_SHORT).show();

                    }

                });
            }







    private void wlqq() {
        Utlis.getUs(this).sendGET("http://v.juhe.cn/weixin/query?key=a332c6b34264527ac142764eaed9364d&pno="+x, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                Bean bean = gson.fromJson(response, Bean.class);
                hlist = bean.getResult().getList();
                Log.e("TAG",response);
                list = new ArrayList<>();
                list.addAll(hlist);
                adapter = new Adapter(list, MainActivity.this);
                ls.setAdapter(adapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
    }


}

你会喜欢下面的文章? You'll like the following article.