1. 程式人生 > >XlistView上拉載入及新資料,下拉重新整理資料

XlistView上拉載入及新資料,下拉重新整理資料

private XListView xListView;
private int i=1;
private String path=“http://www.xieast.com/api/news/news.php?page=”;
private ArrayList<User.DataBean> list = new ArrayList<>();
private EadapterA eadapterA;
private String sa;
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);

//重新整理介面卡
close();
eadapterA.notifyDataSetChanged();

   }

};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找控制元件
xListView = findViewById(R.id.lv);
//Xlistview設定上拉載入,下拉重新整理
xListView.setPullRefreshEnable(true);
xListView.setPullLoadEnable(true);
xListView.setXListViewListener(this);
//呼叫Td方法
Td(1);

}
//呼叫HttpURLconnection工具類
private void Td(int i) {
    new Helpper().get(path+i).getThis(new Helpper.HelpLinten() {



        @Override
        public void getData(String str) {
            Gson gson = new Gson();
            User user =gson.fromJson(str,User.class);
            list.addAll(user.getData());
            eadapterA = new EadapterA(MainActivity.this,list);
            xListView.setAdapter(eadapterA);
        }
    });
}

//上拉載入
@Override
public void onRefresh() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
// String path=“http://www.xieast.com/api/news/news.php?page=”;
i++;

         Td(i);

          handler.sendEmptyMessage(0);
      }
  },2000);
}

//下拉重新整理
@Override
public void onLoadMore() {
handler.postDelayed(new Runnable() {
@Override
public void run() {

    Td(1);

       }
   },2000);
}
//關閉重新整理並設定重新整理時間
public  void close(){
    xListView.stopRefresh();
    xListView.stopLoadMore();
    xListView.setRefreshTime("2018:10:12");

}

//==========================================================
//HttpURLConnection工具類

public class Helpper {
Helpper(){};
public Helpper get(final String str){
new Thread(){
@Override
public void run() {
super.run();
try {

                 URL url = new URL(str);
                 HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                 urlConnection.setRequestMethod("GET");
                 urlConnection.setConnectTimeout(5000);
                 if (urlConnection.getResponseCode()==HttpURLConnection.HTTP_OK){
                     String s = CharStreams.toString(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));
                     Message message = Message.obtain();
                     message.obj=s;
                     message.what=1;
                     handler.sendMessage(message);
                 }
             } catch (Exception e) {
                 e.printStackTrace();
             }

         }
     }.start();

     return this;
}
Handler handler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
      String str= (String)msg.obj;
      helpLinten.getData(str);
    }
};
 private  HelpLinten helpLinten;
 public  void getThis(HelpLinten helpLinten){
     this.helpLinten=helpLinten;
 }

 public interface HelpLinten{
     void getData(String str);
 }