对于刚建程序app依赖你导入的那个PullToresh应该不用我进行讲了吧
先看布局
<com.handmark.pulltorefresh.library.PullToRefreshListView xmlns:ptr="http://schemas.android.com/apk/res-auto" android:id="@+id/lv" android:layout_width="fill_parent" android:layout_height="fill_parent" android:cacheColorHint="#000000" android:divider="#19000000" android:dividerHeight="4dp" android:fadingEdge="none" android:fastScrollEnabled="false" android:footerDividersEnabled="false" android:headerDividersEnabled="false" android:smoothScrollbar="true" ptr:ptrAnimationStyle="rotate" ptr:ptrHeaderSubTextColor="#00ffff" ptr:ptrHeaderBackground="@null" ptr:ptrHeaderTextColor="#ffffff" ptr:ptrDrawable="@mipmap/ic_launcher" />
直接粘到xml里就好
我这是网络获取json放到listview中
public class MainActivity extends AppCompatActivity { private PullToRefreshListView lv; private static final String JSON_URL = "https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num="; private List<News> newslist; private MyAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (PullToRefreshListView) findViewById(R.id.lv); initView(); initData(); } private void initData() { xiala(); } private void initView() { lv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() { @Override public void onRefresh(PullToRefreshBase<ListView> refreshView) { //设置下拉时的时间显示 String label=DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL); //更新label显示 refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label); xiala(); } }); lv.setOnLastItemVisibleListener(new PullToRefreshBase.OnLastItemVisibleListener() { @Override public void onLastItemVisible() { shangla(); } }); lv.setScrollingWhileRefreshingEnabled(true); // lv.setMode(Mode.BOTH); } int ss = 2; public void xiala(){ new AsyncTask<String, Integer, String>() { @Override protected String doInBackground(String... params) { String s = params[0]; String pp = new MyhttpUrl().getPP(s+"2"); return pp; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); Bean bean = new Gson().fromJson(s, Bean.class); newslist = bean.getNewslist(); adapter = new MyAdapter(newslist, MainActivity.this); lv.setAdapter(adapter); lv.onRefreshComplete(); } }.execute(JSON_URL); } int index = 2; public void shangla(){ new AsyncTask<String, Integer, String>() { @Override protected String doInBackground(String... params) { index++; String s = params[0]; String pp = new MyhttpUrl().getPP(s+index); return pp; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); Bean bean = new Gson().fromJson(s, Bean.class); newslist.addAll(bean.getNewslist()); adapter.notifyDataSetChanged(); } }.execute(JSON_URL); } }
进行优化显示图片的loader类
public class Loader extends Application { @Override public void onCreate() { super.onCreate(); File file = StorageUtils.getOwnCacheDirectory(getApplicationContext(), "UniversalImageLoader/Cache"); DisplayImageOptions options = new DisplayImageOptions.Builder().cacheOnDisk(true).cacheInMemory(true).build(); ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(getApplicationContext()) .defaultDisplayImageOptions(options) .memoryCache(new LRULimitedMemoryCache(12 * 1024 * 1024)) .memoryCacheSize(12 * 1024 * 1024) .diskCache(new UnlimitedDiskCache(file)) .threadPriority(Thread.NORM_PRIORITY - 2) .denyCacheImageMultipleSizesInMemory() .diskCacheFileNameGenerator(new Md5FileNameGenerator()) .tasksProcessingOrder(QueueProcessingType.LIFO) .writeDebugLogs() .build(); ImageLoader.getInstance().init(configuration); } }
httpurl方法类
public class MyhttpUrl { public String getPP(String ss){ URL url = null; HttpURLConnection httpURLConnection = null; String pp = ""; try { url = new URL(ss); httpURLConnection = (HttpURLConnection) url.openConnection(); int code = httpURLConnection.getResponseCode(); if (code == 200){ InputStream inputStream = httpURLConnection.getInputStream(); byte [] b = new byte[1024]; int len = 0; while((len = inputStream.read(b)) != -1){ pp+=new String(b,0,len); } inputStream.close(); } } catch (Exception e) { e.printStackTrace(); } return pp; } }
适配器
public class MyAdapter extends BaseAdapter { public List<News> list; public Context context; public MyAdapter(List<News> list, Context context) { this.list = list; this.context = context; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; DisplayImageOptions options = new DisplayImageOptions.Builder() .cacheInMemory(true) .cacheOnDisk(true) .bitmapConfig(Bitmap.Config.RGB_565) .imageScaleType(ImageScaleType.EXACTLY) .build(); if (convertView == null){ holder = new ViewHolder(); convertView = View.inflate(context,R.layout.item,null); holder.iv = (ImageView) convertView.findViewById(R.id.iv); holder.tv = (TextView) convertView.findViewById(R.id.tv); convertView.setTag(holder); }else { holder = (ViewHolder) convertView.getTag(); } holder.tv.setText(list.get(position).getCtime()+list.get(position).getDescription()+list.get(position).getTitle()); ImageLoader.getInstance().displayImage(list.get(position).getPicUrl(),holder.iv,options); return convertView; } class ViewHolder{ ImageView iv; TextView tv; } } 最后那些解析的Bean类就不需要展示了吧!﹃_﹃〣