1. 程式人生 > >Refresh+LoadMore下拉重新整理 上拉載入更多

Refresh+LoadMore下拉重新整理 上拉載入更多

匯入有關的library庫 XListViewFlush

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.text.format.DateFormat;
import com.example.day9_xlistview.adapter.FlushAdapter;
import com.example.xlistviewflush.view.XListView;
import java.util.ArrayList;
import java.util.Date;
/**
 * 介面卡有三種
 * 1:ArrayAdapter  引數1上下文物件,引數2:載入的佈局模板 引數3:資料來源
 * 2:SimpleAdapter
 * 3:BaseAdapter
 * 怎麼建Model跟app進行關聯
 */
public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener {
    private ArrayList<String> mList = new ArrayList<>();
    private int start = 0;
    private XListView XListView;
    private FlushAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        adapter = new FlushAdapter(this, mList);
        XListView.setAdapter(adapter);
    }
private void initData() {
        for (int i = 0; i != 20; i++) {
            mList.add("這是第" + (++start) + "資料");
        }
    }
  private void initView() {
        XListView = (XListView) findViewById(R.id.XListView);
        //設定能上拉下拉的方法
        XListView.setPullLoadEnable(true);
        //千萬不要忘記設定監聽
        XListView.setXListViewListener(this);
   }
    private void stopFlush(){
        XListView.stopLoadMore();
        XListView.stopRefresh();
        Date date = new Date();
        String dateStr = (String)DateFormat.format("EEEE, MMMM dd日, yyyy kk:mm", date);
        XListView.setRefreshTime(dateStr);
 }
    重新整理
    @Override
    public void onRefresh() {
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            start=0;
            mList.clear();
            initData();
            stopFlush();
        }
    },2000);
    }
//載入更多
    @Override
    public void onLoadMore() {
      new Handler().postDelayed(new Runnable() {
          @Override
          public void run() {
              initData();
              adapter.notifyDataSetChanged();
              stopFlush();
          }
      },2000);
    }
}

介面卡配置

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.example.day9_xlistview.R;
import java.util.ArrayList;
public class FlushAdapter extends BaseAdapter {
    private Context mContext;
    private ArrayList<String> mList;
public FlushAdapter(Context mContext, ArrayList<String> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }
 @Override
    public int getCount() {
        return mList.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) {
        convertView = View.inflate(mContext, R.layout.xlistview_item, null);
        TextView tv = convertView.findViewById(R.id.xName);
        tv.setText(mList.get(position));
        return convertView;
    }
}

佈局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <!-- <android.support.v4.widget.SwipeRefreshLayout
         android:layout_width="wrap_content"
         android:layout_height="wrap_content">
         <ListView
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"></ListView>
     </android.support.v4.widget.SwipeRefreshLayout>-->
 <com.example.xlistviewflush.view.XListView
        android:id="@+id/XListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
 </com.example.xlistviewflush.view.XListView>

</android.support.constraint.ConstraintLayout>

item

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/xName"
        android:layout_width="wrap_content"
        android:textSize="30sp"
        android:layout_height="wrap_content"
        android:text="heheda"/>
</LinearLayout>