android 模仿知乎下拉刷新和上拉加载

效果图如下



activity_main.xml

<?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

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

<?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


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
                &&  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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值