仿知乎下拉刷新上拉自動加載

效果圖如下



activity_main.xml

  1. <?xml version=“1.0” encoding=“utf-8”?>  
  2. <RelativeLayout xmlns: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.   
  12.   
  13.     <android.support.v4.widget.SwipeRefreshLayout  
  14.         android:layout_width=“match_parent”  
  15.         android:layout_height=“match_parent”  
  16.         android:id=“@+id/swipe_id”>  
  17.   
  18.         <net.sytm.widget.CustomerListView  
  19.             android:layout_width=“match_parent”  
  20.             android:layout_height=“match_parent”  
  21.             android:id=“@+id/list_view_id” />  
  22.   
  23.     </android.support.v4.widget.SwipeRefreshLayout>  
  24.   
  25. </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

  1. package net.sytm.swiperefreshlayout;  
  2.   
  3. import android.os.Bundle;  
  4. import android.os.Handler;  
  5. import android.os.Message;  
  6. import android.support.v4.widget.SwipeRefreshLayout;  
  7. import android.support.v7.app.AppCompatActivity;  
  8. import android.widget.ArrayAdapter;  
  9.   
  10. import net.sytm.widget.CustomerListView;  
  11.   
  12. import java.lang.ref.WeakReference;  
  13. import java.util.ArrayList;  
  14. import java.util.List;  
  15.   
  16. public class MainActivity extends AppCompatActivity implements CustomerListView.Callback {  
  17.     private MHandler mHandler;  
  18.     private SwipeRefreshLayout refreshLayout;  
  19.     private CustomerListView listView;  
  20.     private List<String> list;  
  21.     private ArrayAdapter<String> adapter;  
  22.   
  23.   
  24.     @Override  
  25.     protected void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);  
  27.         setContentView(R.layout.activity_main);  
  28.         mHandler = new MHandler(this);  
  29.         initUI();  
  30.         bindData();  
  31.     }  
  32.   
  33.     private void initUI() {  
  34.         refreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_id);  
  35.         refreshLayout.setColorSchemeResources(  
  36.                 android.R.color.holo_blue_bright, android.R.color.holo_green_light,  
  37.                 android.R.color.holo_orange_light, android.R.color.holo_red_light);  
  38.   
  39.         refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {  
  40.             @Override  
  41.             public void onRefresh() {  
  42.                 downData();  
  43.             }  
  44.         });  
  45.         listView = (CustomerListView) findViewById(R.id.list_view_id);  
  46.         listView.setCallback(this);  
  47.     }  
  48.   
  49.     private void bindData() {  
  50.         list = new ArrayList<>();  
  51.         for (int i = 0; i< 3; i++) {  
  52.             list.add(String.valueOf(i));  
  53.         }  
  54.         adapter = new ArrayAdapter<>(this, android.R.layout.simple_expandable_list_item_1, list);  
  55.         listView.setAdapter(adapter);  
  56.     }  
  57.   
  58.     @Override  
  59.     public void downData() {  
  60.         mHandler.sendEmptyMessageDelayed(02000);  
  61.     }  
  62.   
  63.     @Override  
  64.     public void loadData() {  
  65.         mHandler.sendEmptyMessageDelayed(12000);  
  66.     }  
  67.   
  68.     static class MHandler extends Handler {  
  69.   
  70.        final WeakReference<MainActivity> activityWeakReference;  
  71.   
  72.        MHandler(MainActivity activity) {  
  73.            this.activityWeakReference = new WeakReference<>(activity);  
  74.        }  
  75.   
  76.        @Override  
  77.        public void handleMessage(Message msg) {  
  78.            MainActivity activity = activityWeakReference.get();  
  79.            if (activity == null) {  
  80.                return;  
  81.            }  
  82.            switch (msg.what) {  
  83.                case 0:  
  84.                    activity.list.add(”hutao”);  
  85.                    activity.adapter.notifyDataSetChanged();  
  86.                    activity.refreshLayout.setRefreshing(false);  
  87.                    break;  
  88.                case 1:  
  89.                    activity.list.add(”php”);  
  90.                    activity.adapter.notifyDataSetChanged();  
  91.                    activity.listView.hideFootView();  
  92.                    break;  
  93.            }  
  94.   
  95.        }  
  96.     }  
  97. }  
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

  1. <?xml version=“1.0” encoding=“utf-8”?>  
  2. <LinearLayout xmlns: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.   
  8.     <ProgressBar  
  9.         android:layout_width=“wrap_content”  
  10.         android:layout_height=“wrap_content”  
  11.         style=“@style/Widget.AppCompat.ProgressBar”/>  
  12.   
  13.     <TextView  
  14.         android:layout_width=“wrap_content”  
  15.         android:layout_height=“wrap_content”  
  16.         android:text=“加載更多”  
  17.         android:textColor=“@android:color/darker_gray”/>  
  18.   
  19. </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


  1. package net.sytm.widget;  
  2.   
  3. import android.content.Context;  
  4. import android.util.AttributeSet;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.widget.AbsListView;  
  8. import android.widget.ListView;  
  9.   
  10. import net.sytm.swiperefreshlayout.R;  
  11.   
  12. /* 
  13.   編碼人 胡桃 
  14.   日期 2016/7/25 
  15.  /  
  16. public class CustomerListView extends ListView implements AbsListView.OnScrollListener {  
  17.   
  18.     private Context context;  
  19.     private Callback callback;  
  20.     private View footView;  
  21.   
  22.   
  23.     public CustomerListView(Context context, AttributeSet attrs) {  
  24.         super(context, attrs);  
  25.         this.context = context;  
  26.         initUI();  
  27.     }  
  28.   
  29.     private void initUI() {  
  30.         footView = LayoutInflater.from(context).inflate(R.layout.foot_view, null);  
  31.         footView.setVisibility(View.GONE);  
  32.         this.addFooterView(footView);  
  33.         this.setFooterDividersEnabled(false);  
  34.         this.setOnScrollListener(this);  
  35.     }  
  36.   
  37.     @Override  
  38.     public void onScrollStateChanged(AbsListView view, int scrollState) {  
  39.         if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE  
  40.                 &&  this.getLastVisiblePosition() == this.getCount() - 1) {  
  41.             footView.setVisibility(View.VISIBLE);  
  42.   
  43.             callback.loadData();  
  44.         }  
  45.     }  
  46.   
  47.     @Override  
  48.     public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {  
  49.         //int lastIndex = firstVisibleItem + visibleItemCount - 1 - 1;  
  50.     }  
  51.   
  52.     public void hideFootView() {  
  53.         footView.setVisibility(View.GONE);  
  54.     }  
  55.   
  56.     public void setCallback(Callback callback) {  
  57.         this.callback = callback;  
  58.     }  
  59.   
  60.     public interface Callback {  
  61.         void downData();  
  62.         void loadData();  
  63.     }  
  64. }  
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();
}

}

http://download.csdn.NET/detail/hu285279904/9655839

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章