1. 程式人生 > >仿知乎下拉重新整理上拉自動載入

仿知乎下拉重新整理上拉自動載入

效果圖如下


activity_main.xml

[html] view plain copy print?在CODE上檢視程式碼片派生到我的程式碼片
  1. <?xmlversion=“1.0”encoding=“utf-8”?>
  2. <RelativeLayoutxmlns:android=“http://schemas.android.com/apk/res/android”
  3.     xmlns:tools=“http://schemas.android.com/tools”
  4.     android:layout_width=“match_parent”
  5.     android:layout_height=“match_parent”
  6.     android:paddingBottom
    =“@dimen/activity_vertical_margin”
  7.     android:paddingLeft=“@dimen/activity_horizontal_margin”
  8.     android:paddingRight=“@dimen/activity_horizontal_margin”
  9.     android:paddingTop=“@dimen/activity_vertical_margin”
  10.     tools:context=“net.sytm.swiperefreshlayout.MainActivity”>
  11.     <android.support.v4.widget.SwipeRefreshLayout
  12.         android:layout_width=“match_parent”
  13.         android:layout_height=“match_parent”
  14.         android:id=“@+id/swipe_id”>
  15.         <net.sytm.widget.CustomerListView
  16.             android:layout_width=“match_parent”
  17.             android:layout_height=“match_parent”
  18.             android:id=“@+id/list_view_id”
    />
  19.     </android.support.v4.widget.SwipeRefreshLayout>
  20. </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="net.sytm.swiperefreshlayout.MainActivity">


    <android.support.v4.widget.SwipeRefreshLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/swipe_id">

        <net.sytm.widget.CustomerListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/list_view_id" />

    </android.support.v4.widget.SwipeRefreshLayout>

</RelativeLayout>

MainActivity.java [java] view plain copy print?在CODE上檢視程式碼片派生到我的程式碼片
  1. package net.sytm.swiperefreshlayout;  
  2. import android.os.Bundle;  
  3. import android.os.Handler;  
  4. import android.os.Message;  
  5. import android.support.v4.widget.SwipeRefreshLayout;  
  6. import android.support.v7.app.AppCompatActivity;  
  7. import android.widget.ArrayAdapter;  
  8. import net.sytm.widget.CustomerListView;  
  9. import java.lang.ref.WeakReference;  
  10. import java.util.ArrayList;  
  11. import java.util.List;  
  12. publicclass MainActivity extends AppCompatActivity implements CustomerListView.Callback {  
  13.     private MHandler mHandler;  
  14.     private SwipeRefreshLayout refreshLayout;  
  15.     private CustomerListView listView;  
  16.     private List<String> list;  
  17.     private ArrayAdapter<String> adapter;  
  18.     @Override
  19.     protectedvoid onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.activity_main);  
  22.         mHandler = new MHandler(this);  
  23.         initUI();  
  24.         bindData();  
  25.     }  
  26.     privatevoid initUI() {  
  27.         refreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_id);  
  28.         refreshLayout.setColorSchemeResources(  
  29.                 android.R.color.holo_blue_bright, android.R.color.holo_green_light,  
  30.                 android.R.color.holo_orange_light, android.R.color.holo_red_light);  
  31.         refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {  
  32.             @Override
  33.             publicvoid onRefresh() {  
  34.                 downData();  
  35.             }  
  36.         });  
  37.         listView = (CustomerListView) findViewById(R.id.list_view_id);  
  38.         listView.setCallback(this);  
  39.     }  
  40.     privatevoid bindData() {  
  41.         list = new ArrayList<>();  
  42.         for (int i = 0; i< 3; i++) {  
  43.             list.add(String.valueOf(i));  
  44.         }  
  45.         adapter = new ArrayAdapter<>(this, android.R.layout.simple_expandable_list_item_1, list);  
  46.         listView.setAdapter(adapter);  
  47.     }  
  48.     @Override
  49.     publicvoid downData() {  
  50.         mHandler.sendEmptyMessageDelayed(02000);  
  51.     }  
  52.     @Override
  53.     publicvoid loadData() {  
  54.         mHandler.sendEmptyMessageDelayed(12000);  
  55.     }  
  56.     staticclass MHandler extends Handler {  
  57.        final WeakReference<MainActivity> activityWeakReference;  
  58.        MHandler(MainActivity activity) {  
  59.            this.activityWeakReference = new WeakReference<>(activity);  
  60.        }  
  61.        @Override
  62.        publicvoid handleMessage(Message msg) {  
  63.            MainActivity activity = activityWeakReference.get();  
  64.            if (activity == null) {  
  65.                return;  
  66.            }  
  67.            switch (msg.what) {  
  68.                case0:  
  69.                    activity.list.add(”hutao”);  
  70.                    activity.adapter.notifyDataSetChanged();  
  71.                    activity.refreshLayout.setRefreshing(false);  
  72.                    break;  
  73.                case1:  
  74.                    activity.list.add(”php”);  
  75.                    activity.adapter.notifyDataSetChanged();  
  76.                    activity.listView.hideFootView();  
  77.                    break;  
  78.            }  
  79.        }  
  80.     }  
  81. }  
package net.sytm.swiperefreshlayout;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;

import net.sytm.widget.CustomerListView;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements CustomerListView.Callback {
    private MHandler mHandler;
    private SwipeRefreshLayout refreshLayout;
    private CustomerListView listView;
    private List<String> list;
    private ArrayAdapter<String> adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mHandler = new MHandler(this);
        initUI();
        bindData();
    }

    private void initUI() {
        refreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_id);
        refreshLayout.setColorSchemeResources(
                android.R.color.holo_blue_bright, android.R.color.holo_green_light,
                android.R.color.holo_orange_light, android.R.color.holo_red_light);

        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                downData();
            }
        });
        listView = (CustomerListView) findViewById(R.id.list_view_id);
        listView.setCallback(this);
    }

    private void bindData() {
        list = new ArrayList<>();
        for (int i = 0; i< 3; i++) {
            list.add(String.valueOf(i));
        }
        adapter = new ArrayAdapter<>(this, android.R.layout.simple_expandable_list_item_1, list);
        listView.setAdapter(adapter);
    }

    @Override
    public void downData() {
        mHandler.sendEmptyMessageDelayed(0, 2000);
    }

    @Override
    public void loadData() {
        mHandler.sendEmptyMessageDelayed(1, 2000);
    }

    static class MHandler extends Handler {

       final WeakReference<MainActivity> activityWeakReference;

       MHandler(MainActivity activity) {
           this.activityWeakReference = new WeakReference<>(activity);
       }

       @Override
       public void handleMessage(Message msg) {
           MainActivity activity = activityWeakReference.get();
           if (activity == null) {
               return;
           }
           switch (msg.what) {
               case 0:
                   activity.list.add("hutao");
                   activity.adapter.notifyDataSetChanged();
                   activity.refreshLayout.setRefreshing(false);
                   break;
               case 1:
                   activity.list.add("php");
                   activity.adapter.notifyDataSetChanged();
                   activity.listView.hideFootView();
                   break;
           }

       }
    }
}

foot_view.xml [html] view plain copy print?在CODE上檢視程式碼片派生到我的程式碼片
  1. <?xmlversion=“1.0”encoding=“utf-8”?>
  2. <LinearLayoutxmlns:android=“http://schemas.android.com/apk/res/android”
  3.     android:orientation=“horizontal”
  4.     android:layout_width=“match_parent”
  5.     android:layout_height=“match_parent”
  6.     android:gravity=“center”>
  7.     <ProgressBar
  8.         android:layout_width=“wrap_content”
  9.         android:layout_height=“wrap_content”
  10.         style=“@style/Widget.AppCompat.ProgressBar”/>
  11.     <TextView
  12.         android:layout_width=“wrap_content”
  13.         android:layout_height=“wrap_content”
  14.         android:text=“載入更多”
  15.         android:textColor=“@android:color/darker_gray”/>
  16. </LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/Widget.AppCompat.ProgressBar"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="載入更多"
        android:textColor="@android:color/darker_gray"/>

</LinearLayout>

CustomerListView.java
[java] view plain copy print?在CODE上檢視程式碼片派生到我的程式碼片
  1. package net.sytm.widget;  
  2. import android.content.Context;  
  3. import android.util.AttributeSet;  
  4. import android.view.LayoutInflater;  
  5. import android.view.View;  
  6. import android.widget.AbsListView;  
  7. import android.widget.ListView;  
  8. import net.sytm.swiperefreshlayout.R;  
  9. /* 
  10.   編碼人 胡桃 
  11.   日期 2016/7/25 
  12.  /
  13. publicclass CustomerListView extends ListView implements AbsListView.OnScrollListener {  
  14.     private Context context;  
  15.     private Callback callback;  
  16.     private View footView;  
  17.     public CustomerListView(Context context, AttributeSet attrs) {  
  18.         super(context, attrs);  
  19.         this.context = context;  
  20.         initUI();  
  21.     }  
  22.     privatevoid initUI() {  
  23.         footView = LayoutInflater.from(context).inflate(R.layout.foot_view, null);  
  24.         footView.setVisibility(View.GONE);  
  25.         this.addFooterView(footView);  
  26.         this.setFooterDividersEnabled(false);  
  27.         this.setOnScrollListener(this);  
  28.     }  
  29.     @Override
  30.     publicvoid onScrollStateChanged(AbsListView view, int scrollState) {  
  31.         if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE  
  32.                 &&  this.getLastVisiblePosition() == this.getCount() - 1) {  
  33.             footView.setVisibility(View.VISIBLE);  
  34.             callback.loadData();  
  35.         }  
  36.     }  
  37.     @Override
  38.     publicvoid onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {  
  39.         //int lastIndex = firstVisibleItem + visibleItemCount - 1 - 1;
  40.     }  
  41.     publicvoid hideFootView() {  
  42.         footView.setVisibility(View.GONE);  
  43.     }  
  44.     publicvoid setCallback(Callback callback) {  
  45.         this.callback = callback;  
  46.     }  
  47.     publicinterface Callback {  
  48.         void downData();  
  49.         void loadData();  
  50.     }  
  51. }  
package net.sytm.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;

import net.sytm.swiperefreshlayout.R;

/**
* 編碼人 胡桃
* 日期 2016/7/25
*/
public class CustomerListView extends ListView implements AbsListView.OnScrollListener {

private Context context;
private Callback callback;
private View footView;


public CustomerListView(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.context = context;
    initUI();
}

private void initUI() {
    footView = LayoutInflater.from(context).inflate(R.layout.foot_view, null);
    footView.setVisibility(View.GONE);
    this.addFooterView(footView);
    this.setFooterDividersEnabled(false);
    this.setOnScrollListener(this);
}

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
    if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE
            &amp;&amp;  this.getLastVisiblePosition() == this.getCount() - 1) {
        footView.setVisibility(View.VISIBLE);

        callback.loadData();
    }
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
    //int lastIndex = firstVisibleItem + visibleItemCount - 1 - 1;
}

public void hideFootView() {
    footView.setVisibility(View.GONE);
}

public void setCallback(Callback callback) {
    this.callback = callback;
}

public interface Callback {
    void downData();
    void loadData();
}

}