• 安卓下拉刷新空间SwipeRefreshLayout的基本使用


    1.先写布局文件

     <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/refresh"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <android.support.v7.widget.RecyclerView
                android:id="@+id/my_recycler_view"
                android:scrollbars="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
    
        </android.support.v4.widget.SwipeRefreshLayout>

    2.mainactivity

    public class MainActivity extends AppCompatActivity {
        private OkHttpHelper httpHelper  =OkHttpHelper.getInstance();
        private SwipeRefreshLayout swipeRefreshLayout;
        @ViewInject(R.id.tv)
        private TextView tv;
    
        private RecyclerView mRecyclerView;
        private ArrayList<String> mDatas=new ArrayList<>();
        private MyAdapter myAdapter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mRecyclerView = findViewById(R.id.my_recycler_view);
            initDatas();
            myAdapter =new MyAdapter(mDatas);
            mRecyclerView.setAdapter(myAdapter);
            mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
            //mRecyclerView.setLayoutManager(new GridLayoutManager(this,4));
            // mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(4,StaggeredGridLayoutManager.HORIZONTAL));
            //配置分割线
            mRecyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL_LIST));
            //事件监听
    
    
    
            int size =mDatas.size();
            //添加数据到头部
            myAdapter.addData(size,"adddddddddd");
    
            //删除数据
            myAdapter.removeData(0);
    
    
    
            swipeRefreshLayout = findViewById(R.id.refresh);
            initRefreshLayout();
        }
    
        private void initRefreshLayout(){
            //转的圈圈的颜色
            swipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary);
    
            swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    Log.d(TAG, "onRefresh: "+swipeRefreshLayout.isRefreshing());
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            for(int i=0;i<=10;i++){
                                myAdapter.addData(i,"new"+i);
                            }
                            myAdapter.notifyItemRangeChanged(0,10);
    
                            mRecyclerView.scrollToPosition(0);
                            //刷新完成,必须调用,否则会一直是圈圈转转的情况
                            swipeRefreshLayout.setRefreshing(false);
                        }
                    },2000);
                }
            });
        }
    
        private static final String TAG = "MainActivity";
        private void initDatas(){
            mDatas.add("aa");
            mDatas.add("cv");
            mDatas.add("cv1");
            mDatas.add("cv112");
            mDatas.add("cv112");
            mDatas.add("cv112");
            mDatas.add("cv112");
            mDatas.add("cv112");
            mDatas.add("cv112");
            mDatas.add("cv112");
            mDatas.add("cv112");
            mDatas.add("cv112");
            mDatas.add("cv112");
            mDatas.add("cv112");
        }
    
    
    }
  • 相关阅读:
    CS224n笔记12 语音识别的end-to-end模型
    Vue组件
    关于网站
    Vue问题区
    数组的增、删、改、查
    Tree全部展开/折叠
    python 中UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-7: ordinal not in range(128)的解决方法
    python安装wxPython
    python中的迭代器和生成器
    python中的re正则表达式和模板系统
  • 原文地址:https://www.cnblogs.com/norm/p/8276530.html
Copyright © 2020-2023  润新知