美文网首页Design
SwipyRefreshLayout使用方法 刷新以及加载

SwipyRefreshLayout使用方法 刷新以及加载

作者: 简书_小马 | 来源:发表于2016-08-23 16:14 被阅读1337次

    SwipyRefreshLayout使用方法

    xml

    <LinearLayout    
    xmlns:android="http://schemas.android.com/apk/res/android"   
    xmlns:xtao="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"    
    android:layout_width="match_parent"
    android:layout_height="match_parent">   
    
    <com.weibao.finance.ne.widget.SwipyRefreshLayout   
       android:id="@+id/so_swr"   
       android:layout_width="match_parent"   
       android:layout_height="match_parent"   
       xtao:direction="both"       >    
     
    <android.support.v7.widget.RecyclerView   
       android:id="@+id/so_rec"    android:layout_width="match_parent"      
       android:layout_height="match_parent">   
    
    </android.support.v7.widget.RecyclerView>    
    
    </com.weibao.finance.ne.widget.SwipyRefreshLayout>   
    
    </LinearLayout>
    

    xtao:direction="both"设置刷新加载是否支持TOP(0), // 只有下拉刷新BOTTOM(1), // 只有加载更多BOTH(2);// 全都有

    初始化SwipyRefreshLayout使用方法

        /**   
        *初始化
        /
       private void initView() {
        mSwipyRefreshLayout = (SwipyRefreshLayout) findViewById(R.id.main_SwipyRefreshLayout);   
        mListView = (ListView) findViewById(R.id.main_ListView);   
        // 设置分页第一页的索引 默认为0
        mSwipyRefreshLayout.setFirstIndex(startIndex);   
    
        // 监听事件
        mSwipyRefreshLayout.setOnRefreshListener(this);
    
        arrayAdapter = new ArrayAdapter<String>(MainActivity.this,
                android.R.layout.simple_list_item_1, mList);
        mListView.setAdapter(arrayAdapter);
         initData();
    
    }     
    

    加载数据

    private void initData() {   
        startIndex = 0;   
        createListData();   
    }    
    

    构造数据

    private void createListData() {
        mList = new ArrayList<String>();
        for (int i = 'A'; i <= 'K'; i++) {
            mList.add("item" + (char) i);
        }
    }
    private void addListData(List<String> list) {
        for (int i = 'A'; i <= 'K'; i++) {
            list.add("item" + (char) i);
        }
    }    
    

    刷新 监听

    /**
     * 下拉刷新
     * 
     * @param index
     *            当前分页索引
     */
    @Override
    public void onRefresh(int index) {
        Log.d("index", "onRefresh - index :" + index);
        Toast.makeText(MainActivity.this, "onRefresh - index :" + index,
                Toast.LENGTH_SHORT).show();
        dataOption(TOP_REFRESH);
    }
    

    加载监听

    /**
     * 加载更多
     * 
     * @param index
     *            当前分页索引
     */
    @Override
    public void onLoad(int index) {
        Log.d("index", "onLoad - index :" + index);
        Toast.makeText(MainActivity.this, "onLoad - index :" + index,
                Toast.LENGTH_SHORT).show();
        dataOption(BOTTOM_REFRESH);
    }   
    

    总结

    PullToRefreshView与SwipyRefreshLayout两者在动画上面有不同的风格PullTorefreshView的刷新动画一般在listview第一个item上边等于说加了一个hade而SwipyRefreshLayout则是在SwipyRefreshLayout控件内刷新的相对于PullTorefreshView感觉还是SwipyRefreshLayout更为方便兼容性更高

    相关文章

      网友评论

      本文标题:SwipyRefreshLayout使用方法 刷新以及加载

      本文链接:https://www.haomeiwen.com/subject/cmfnsttx.html