Search in sources :

Example 1 with SwipeRefreshView

use of com.ningcui.mylibrary.viewLib.SwipeRefreshView in project JustAndroid by chinaltz.

the class SwipeRefreshLayoutActivity method onCreate.

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_swiperefreshlayout);
    ButterKnife.bind(this);
    final SwipeRefreshView swipeRefreshView = (SwipeRefreshView) findViewById(R.id.srl);
    ListView listView = (ListView) findViewById(R.id.swipe_listView);
    // 设置适配器数据
    mList = new ArrayList<>();
    for (int i = 0; i < 30; i++) {
        mList.add("测试数据" + i + "号");
        mCount++;
    }
    mAdapter = new StringAdapter();
    listView.setAdapter(mAdapter);
    // 不能在onCreate中设置,这个表示当前是刷新状态,如果一进来就是刷新状态,SwipeRefreshLayout会屏蔽掉下拉事件
    //swipeRefreshLayout.setRefreshing(true);
    // 设置颜色属性的时候一定要注意是引用了资源文件还是直接设置16进制的颜色,因为都是int值容易搞混
    // 设置下拉进度的背景颜色,默认就是白色的
    swipeRefreshView.setProgressBackgroundColorSchemeResource(android.R.color.white);
    // 设置下拉进度的主题颜色
    swipeRefreshView.setColorSchemeResources(R.color.colorAccent, R.color.colorPrimary, R.color.colorPrimaryDark);
    // 下拉时触发SwipeRefreshLayout的下拉动画,动画完毕之后就会回调这个方法
    swipeRefreshView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

        @Override
        public void onRefresh() {
            // TODO 获取数据
            final Random random = new Random();
            new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {
                    mList.add(0, "测试数据" + random.nextInt(100) + "号");
                    mAdapter.notifyDataSetChanged();
                    Toast.makeText(SwipeRefreshLayoutActivity.this, "刷新了一条数据", Toast.LENGTH_SHORT).show();
                    // 加载完数据设置为不刷新状态,将下拉进度收起来
                    swipeRefreshView.setRefreshing(false);
                }
            }, 1200);
        }
    });
    // 设置下拉加载更多
    swipeRefreshView.setOnLoadListener(new SwipeRefreshView.OnLoadListener() {

        @Override
        public void onLoad() {
            new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {
                    // 添加数据
                    for (int i = 30; i < 35; i++) {
                        mList.add("测试数据" + i + "号");
                        // 这里要放在里面刷新,放在外面会导致刷新的进度条卡住
                        mAdapter.notifyDataSetChanged();
                    }
                    Toast.makeText(SwipeRefreshLayoutActivity.this, "加载了" + 5 + "条数据", Toast.LENGTH_SHORT).show();
                    // 加载完数据设置为不加载状态,将加载进度收起来
                    swipeRefreshView.setLoading(false);
                }
            }, 1200);
        }
    });
}
Also used : SwipeRefreshView(com.ningcui.mylibrary.viewLib.SwipeRefreshView) ListView(android.widget.ListView) Random(java.util.Random) Handler(android.os.Handler) SwipeRefreshLayout(android.support.v4.widget.SwipeRefreshLayout)

Aggregations

Handler (android.os.Handler)1 SwipeRefreshLayout (android.support.v4.widget.SwipeRefreshLayout)1 ListView (android.widget.ListView)1 SwipeRefreshView (com.ningcui.mylibrary.viewLib.SwipeRefreshView)1 Random (java.util.Random)1