GridView(刷新加载更多)

 
  
bulid里面加入
compile 'com.github.userswlwork:pull-to-refresh:1.0.0'
 
   
  
public class OtherActivity extends AppCompatActivity {

    private PullToRefreshGridView gridView;
    private boolean b;//判断上拉还是下拉
    int index = 1;
    private List newslists=new ArrayList<>();//定义集合。存放数据
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_other);

        gridView = findViewById(R.id.Grid);

        task();
        listen();

    }
//支持刷新
    private void listen() {
        gridView.setMode(PullToRefreshBase.Mode.BOTH);
        //设置拉动监听器
        gridView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase pullToRefreshBase) {
                b=true;
                index=1;//上拉刷新初始化第一页
                task();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase pullToRefreshBase) {
                b=false;
                index++;
                task();
            }
        });
    }


    private void task() {

        Task Mytask= new Task();
        Mytask.execute("http://api.tianapi.com/meinv/?key=11ea949d63cdcfc91838cb62177c0f00&num=10&pager="+index);
    }

    class Task extends AsyncTask{
        @Override
        protected String doInBackground(String... strings) {

            OkHttpClient okHttpClient =  new OkHttpClient();
            Request request = new Request.Builder().url(strings[0]).build();
            try {
                Response response = okHttpClient.newCall(request).execute();
                if (response.code()==200){
                    String s = response.body().string();
                    return s;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Gson gson = new Gson();
            Userbean userbean = gson.fromJson(s, Userbean.class);
            List newslist = userbean.getNewslist();
            //如果下拉刷新就清空大集合
            if (b){
                newslists.clear();
            }
            //停止刷新
            gridView.onRefreshComplete();
            //存放数据
            newslists.addAll(newslist);
            gridView.setAdapter(new Base(OtherActivity.this,newslists));
        }
    }
}



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:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.zhonghe.OtherActivity">

    <com.handmark.pulltorefresh.library.PullToRefreshGridView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="2"
        android:id="@+id/Grid"
        >

    com.handmark.pulltorefresh.library.PullToRefreshGridView>

LinearLayout>



bulid里面加入
compile 'com.github.userswlwork:pull-to-refresh:1.0.0'

你可能感兴趣的:(GridView(刷新加载更多))