Android pull down to refresh, pull up to load custom controls/view usage and principles

The content of the study notes comes from github

 

android_my_pull_refresh_view

Overview

This is a general pull-down refresh and pull-up auto-loaded component. The component inherits from LinearLayout, the direction is vertical layout, and consists of three parts, namely Header, ContentView, and Footer. The width and height of ContentView are match_parent, footer. The width and height of the header and header are match_parent and wrap_content respectively. The header and footer will be hidden by setting padding at the beginning, and only the ContentView area will be displayed. When the user pulls down to the top and continues to pull down, the pull-down refresh operation is triggered; when the user pulls up to the bottom and continues to pull up, it triggers more loading operations.
For more information, please refer to my blog

The libraries in this project are used to illustrate the basic principles and are not recommended to be used in the project.

## a, a schematic layout of the original layout
Alt text

Headerh and footer are offset from the screen after padding is set
Alt text

2. Examples of the use of existing components

The following examples are all demonstrated in an Activity.

2.1 Use PullRefreshListView

public class MainActivity extends Activity {

    ListView mListView;

    PullRefreshListView mPullRefreshListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 下拉刷新的listview
        mPullRefreshListView = new PullRefreshListView(this);
        // 获取listview 对象, 即PullRefreshListView中的mContentView
        mListView = mPullRefreshListView.getContentView();

        List<String> datas = new ArrayList<String>();
        for (int i = 0; i < 5; i++) {
            datas.add(" Item - " + i);
        }

        // 设置adapter
        mListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
                datas));

        // 下拉刷新
        mPullRefreshListView.setOnRefreshListener(new OnPullRefreshListener() {

            @Override
            public void onRefresh() {

                Toast.makeText(getApplicationContext(), "refresh", Toast.LENGTH_SHORT).show();
                mPullRefreshListView.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        mPullRefreshListView.refreshComplete();
                    }
                }, 2000);
            }
        });
        // 上拉自动加载
        mPullRefreshListView.setOnLoadMoreListener(new OnLoadMoreListener() {

            @Override
            public void onLoadMore() {
                Toast.makeText(getApplicationContext(), "load more", Toast.LENGTH_SHORT).show();
                mPullRefreshListView.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        mPullRefreshListView.loadMoreComplete();
                    }
                }, 1500);
            }
        });


        setContentView(mPullRefreshListView);

    }
}

Pull down to refresh the screenshot

Alt text

Automatic loading from top to bottom
Alt text

2.2 Use TextView that can be pulled down to refresh

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

 // PullRefreshTextView
        final PullRefreshTextView pullRefreshTextView = new PullRefreshTextView(this);
        pullRefreshTextView.getContentView().setText("下拉刷新TextView");
        // 下拉刷新
        pullRefreshTextView.setOnRefreshListener(new OnPullRefreshListener() {

            @Override
            public void onRefresh() {
                pullRefreshTextView.getContentView().setText(new Date().toGMTString());
                pullRefreshTextView.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        pullRefreshTextView.refreshComplete();
                    }
                }, 1000);
            }
        });

        // 上拉自动加载, TextView不能设置scroll listener ,所以无效
        pullRefreshTextView.setOnLoadMoreListener(new OnLoadMoreListener() {

            @Override
            public void onLoadMore() {
                Toast.makeText(getApplicationContext(), "textview load", Toast.LENGTH_SHORT).show();
                pullRefreshTextView.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        pullRefreshTextView.loadMoreComplete();
                    }
                }, 1000);
            }
        });

         setContentView(pullRefreshTextView);


    }
}

Screenshot

Alt text

Three, extend the component

3.1, inherited from PullRefreshBase

T为你要实现下拉刷新的View的类型,如ListView.       

3.2, initialize ContentView

Override the initContentView method and initialize the mContentView object in this function. Let's take ListView as an example, for example:

 /*
     * 初始化mContentView
     * @see com.uit.pullrefresh.base.PullRefreshBase#initContentView()
     */
    @Override
    protected void initContentView() {
        // 初始化mContentView
        mContentView = new ListView(getContext());
        // 设置OnScrollListener, 用以实现滑动到底部时的自动加载功能,如果不需要该功能可以不设置.
        mContentView.setOnScrollListener(this);
    }

3.3. Overwrite the method to determine whether to slide to the top and bottom

Let's take ListView as an example, for example:

    /*
     * 是否滑动到了顶端,如果返回true, 则表示到了顶端,用户继续下拉则触发下拉刷新
     * @see com.uit.pullrefresh.base.PullRefreshBase#isTop()
     */
    @Override
    protected boolean isTop() {
        View firstChild = mContentView.getChildAt(0);
        if (firstChild == null) {
            return true;
        }
        return mContentView.getFirstVisiblePosition() == 0
                && (firstChild.getTop() >= mContentView.getTop());
    }

    /*
     * 下拉到listview 最后一项时则返回true, 将出发自动加载
     * @see com.uit.pullrefresh.base.PullRefreshBase#isShowFooterView()
     */
    @Override
    protected boolean isShowFooterView() {
        if (mContentView == null || mContentView.getAdapter() == null) {
            return false;
        }

        return mContentView.getLastVisiblePosition() == mContentView.getAdapter().getCount() - 1;
    } 

 


https://github.com/hehonghui/android_my_pull_refresh_view

Append: a more powerful framework

https://github.com/scwang90/SmartRefreshLayout

Guess you like

Origin blog.csdn.net/qq_36355271/article/details/96858055