需要权限
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
需要依赖
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5' compile 'com.google.code.gson:gson:2.2.4'需要导包
implementation project(':PullToRefreshLibrary')
PullToRefreshGridView布局
<com.handmark.pulltorefresh.library.PullToRefreshGridView xmlns:ptr="http://schemas.android.com/apk/res-auto" android:id="@+id/pull_refresh_grid" android:layout_height="fill_parent" android:layout_width="fill_parent" android:numColumns="3" />
public class MainActivity extends AppCompatActivity { String uri; int aa=1; List<Bean.DataBean> list = new ArrayList<Bean.DataBean>(); private PullToRefreshListView pull; private MyAdapter adapter; private ImageLoader instance; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pull = (PullToRefreshListView) findViewById(R.id.pull_refresh_list); pull.setMode(PullToRefreshBase.Mode.BOTH); instance = ImageLoader.getInstance(); ListView lv = pull.getRefreshableView(); setUri(aa+""); new GetDataTake().execute(uri); adapter = new MyAdapter(); lv.setAdapter(adapter); pull.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() { @Override public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) { list.clear(); aa=1; setUri(aa+""); new GetDataTake().execute(uri); } @Override public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) { aa++; setUri(aa+""); new GetDataTake().execute(uri); } }); } class GetDataTake extends AsyncTask<String,Void,String> { @Override protected String doInBackground(String... strings) { String aoto = Util.aoto(strings[0]); return aoto; } @Override protected void onPostExecute(String s) { Gson gson=new Gson(); Bean bean = gson.fromJson(s, Bean.class); List<Bean.DataBean> data = bean.getData(); list.addAll(data); adapter.notifyDataSetChanged(); pull.onRefreshComplete(); super.onPostExecute(s); } } class MyAdapter extends BaseAdapter{ @Override public int getCount() { return list.size(); } @Override public Object getItem(int i) { return null; } @Override public long getItemId(int i) { return 0; } @Override public View getView(int i, View view, ViewGroup viewGroup) { if(view==null){ view=View.inflate(MainActivity.this,R.layout.activity_list,null); } ImageView iv_list = view.findViewById(R.id.iv_list); TextView tv_list = view.findViewById(R.id.tv_list); tv_list.setText(list.get(i).getNews_title()); instance.displayImage(list.get(i).getPic_url(),iv_list); return view; } } public void setUri(String aa){ uri="http://api.expoon.com/AppNews/getNewsList/type/1/p/"+aa; } }