Android中列表数据的上拉加载和下拉刷新功能的实现

这里用到的是一个第三方插件:SwipeToLoadLayout,可以自定义HeaderView和FooterView,非常好用!

GitHub地址:https://github.com/Aspsine/SwipeToLoadLayout

效果图如下:

Android中列表数据的上拉加载和下拉刷新功能的实现_第1张图片

1、自定义RefreshHeaderView的代码如下:

public class RefreshHeaderView extends android.support.v7.widget.AppCompatTextView implements SwipeRefreshTrigger, SwipeTrigger {

    public RefreshHeaderView(Context context) {
        super(context);
    }

    public RefreshHeaderView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void onRefresh() {
        setText("下拉刷新");
    }

    @Override
    public void onPrepare() {
        setText("");
    }

    @Override
    public void onMove(int yScrolled, boolean isComplete, boolean automatic) {
        if (!isComplete) {
            /*if (yScrolled >= getHeight()) {
                setText("RELEASE TO REFRESH");
            } else {
                setText("SWIPE TO REFRESH");
            }*/
            setText("下拉刷新");
        } else {
            //setText("REFRESH RETURNING");
        }
    }

    @Override
    public void onRelease() {
    }

    @Override
    public void onComplete() {
        setText("刷新完成");
    }

    @Override
    public void onReset() {
        setText("");
    }
}

2、自定义LoadMoreFooterView的代码如下:

public class LoadMoreFooterView extends android.support.v7.widget.AppCompatTextView implements SwipeTrigger, SwipeLoadMoreTrigger {
    public LoadMoreFooterView(Context context) {
        super(context);
    }

    public LoadMoreFooterView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void onLoadMore() {
        //setText("LOADING MORE");
    }

    @Override
    public void onPrepare() {
        //setText("");
    }

    @Override
    public void onMove(int yScrolled, boolean isComplete, boolean automatic) {
        if (!isComplete) {
            /*if (yScrolled <= -getHeight()) {
                setText("RELEASE TO LOAD MORE");
            } else {
                setText("SWIPE TO LOAD MORE");
            }*/
            setText("加载更多数据");
        } else {
            //setText("LOAD MORE RETURNING");
        }
    }

    @Override
    public void onRelease() {
        //setText("LOADING MORE");
    }

    @Override
    public void onComplete() {
        setText("加载完成");
    }

    @Override
    public void onReset() {
        setText("");
    }
}

3、activity_main.xml的代码如下:




    

        

        

        

    

注意:(这些id是固定的,和选项卡TabHost类似)

  • refresh header view android:id="@id/swipe_refresh_header"
  • target view android:id="@id/swipe_target"
  • load more footer view android:id="@id/swipe_load_more_footer"

4、MainActivity.java的代码如下:

public class MainActivity extends AppCompatActivity implements OnRefreshListener, OnLoadMoreListener {

    private Context mContext;

    private SwipeToLoadLayout swipeToLoadLayout;
    private ListView lvNewsList;

    private List tbMyJoinedActivitiesList;
    private Tb_MyJoinedActivities tb_myJoinedActivities;
    private MyJoinedNewsListAdapter myJoinedNewsListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        baseDataInit();
        bindViews();
        viewsAddListener();
        viewsDataInit();
    }

    private void baseDataInit() {
        mContext = this;
        tbMyJoinedActivitiesList = new ArrayList<>();
    }

    private void bindViews() {
        swipeToLoadLayout = findViewById(R.id.Main_STLL);
        lvNewsList = findViewById(R.id.swipe_target);
    }

    private void viewsAddListener() {
        swipeToLoadLayout.setOnRefreshListener(this);
        swipeToLoadLayout.setOnLoadMoreListener(this);
    }

    private void viewsDataInit() {
        newsListAddAdapter();
    }

    private void newsListAddAdapter() {
        for (int i=0;i<10;i++) {
            tb_myJoinedActivities = new Tb_MyJoinedActivities();
            tbMyJoinedActivitiesList.add(tb_myJoinedActivities);
        }
        myJoinedNewsListAdapter = new MyJoinedNewsListAdapter(mContext, tbMyJoinedActivitiesList);
        lvNewsList.setAdapter(myJoinedNewsListAdapter);
    }

    @Override
    public void onLoadMore() {
        //Toast.makeText(mContext, "加载更多", Toast.LENGTH_SHORT).show();
        for (int i=0;i<10;i++) {
            tb_myJoinedActivities = new Tb_MyJoinedActivities();
            tbMyJoinedActivitiesList.add(tb_myJoinedActivities);
        }
        myJoinedNewsListAdapter.notifyDataSetChanged();
        swipeToLoadLayout.setLoadingMore(false);
    }

    @Override
    public void onRefresh() {
        //Toast.makeText(mContext, "刷新", Toast.LENGTH_SHORT).show();
        tbMyJoinedActivitiesList.clear();
        for (int i=0;i<5;i++) {
            tb_myJoinedActivities = new Tb_MyJoinedActivities();
            tbMyJoinedActivitiesList.add(tb_myJoinedActivities);
        }
        myJoinedNewsListAdapter.notifyDataSetChanged();
        swipeToLoadLayout.setRefreshing(false);
    }
}

其中,头布局和底部布局是可以自定义的,这里用到的是一个TextView。

你可能感兴趣的:(Android开发)