欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

PullToRefresh 上下拉刷新 简单实现

程序员文章站 2024-03-17 12:21:16
...

1.添加依赖
compile ‘com.github.userswlwork:pull-to-refresh:1.0.0’
2.布局文件使用控件
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/plv"
android:layout_width=“match_parent”
android:layout_height=“match_parent”>
</com.handmark.pulltorefresh.library.PullToRefreshListView>
3.具体代码
package com.bw.module10;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.w3c.dom.Text;

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

public class Frag extends Fragment {
String urlString = “http://api.expoon.com/AppNews/getNewsList/type/1/p/1”;
List<Bean.DataBean> list = new ArrayList<>();
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what==1){
String obj = (String) msg.obj;
Gson g = new Gson();
Bean bean = g.fromJson(obj, Bean.class);
List<Bean.DataBean> data = bean.getData();
list.addAll(data);
adapter.notifyDataSetChanged();
plv.onRefreshComplete();
}
}
};
private ImageLoader instance;
private MyAdapter adapter;
private PullToRefreshListView plv;
private int page;
private String zon;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.frag,null);
plv = v.findViewById(R.id.plv);
plv.setMode(PullToRefreshListView.Mode.BOTH);
instance = ImageLoader.getInstance();

    adapter = new MyAdapter();
    plv.setAdapter(adapter);
    getNet(0);
    return v;
}

private void getNet(int page) {
    zon=urlString+page;
    init();
}

private void init() {
    new Thread(){
        @Override
        public void run() {
            super.run();
            String json = NetUrl.getJson(zon);
            Message message = handler.obtainMessage();
            message.what=1;
            message.obj=json;
            handler.sendMessage(message);
        }
    }.start();
    plv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
        @Override
        public void onPullDownToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
            list.clear();
            getNet(0);
            page=0;
        }

        @Override
        public void onPullUpToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
            page++;
            getNet(page);
        }
    });
}

class MyAdapter extends BaseAdapter{

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder=new ViewHolder();
            convertView = View.inflate(getActivity(),R.layout.list_item,null);
            holder.img = convertView.findViewById(R.id.img);
            holder.tv = convertView.findViewById(R.id.tv);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        instance.displayImage(list.get(position).getPic_url(),holder.img);
        holder.tv.setText(list.get(position).getNews_title());
        return convertView;
    }

}
class ViewHolder{
    ImageView img;
    TextView tv;
}

}

相关标签: Banner