使用PullTorefres进行下拉上提刷新

对于刚建程序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类就不需要展示了吧!﹃_﹃〣 









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值