XRecyclerView的简单使用与图片拉动字体缩放效果的实现

来源:互联网 发布:淘宝上怎么好评改差评 编辑:程序博客网 时间:2024/05/20 12:21

一、XRecyclerView的简单使用

1,依赖第三方implementation project(':xrecyclerview')与xml布局中的引入;

 2,设置布局管理器

//设置布局管理器LinearLayoutManager layoutManager = new LinearLayoutManager(this);layoutManager.setOrientation(LinearLayoutManager.VERTICAL);mRecyclerView.setLayoutManager(layoutManager);
     3,相关属性设置
mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);    //设置刷新的样式mRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);    //设置加载更多的样式mRecyclerView.setArrowImageView(R.drawable.iconfont_downgrey);      //设置加载动画的箭头//设置底部加载更多显示字符串mRecyclerView.getDefaultFootView().setLoadingHint("自定义加载中提示");mRecyclerView.getDefaultFootView().setNoMoreHint("自定义加载完毕提示");
// When the item number of the screen number is list.size-2,we call the onLoadMoremRecyclerView.setLimitNumberToCallLoadMore(2);

     4,头部可以添加head布局
//加入XRecycleView的头部View header = LayoutInflater.from(this).inflate(R.layout.recyclerview_header, (ViewGroup)findViewById(android.R.id.content),false);mRecyclerView.addHeaderView(header);View header2 = LayoutInflater.from(this).inflate(R.layout.recyclerview_header, (ViewGroup)findViewById(android.R.id.content),false);mRecyclerView.addHeaderView(header2);

     5,设置Adapter
listData = new ArrayList<String>();mAdapter = new MyAdapter(listData);mRecyclerView.setAdapter(mAdapter);//设置adaptermRecyclerView.refresh();

     6,设置相关的几个重要的监听事件
//上拉、下拉监听事件        mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {            @Override            public void onRefresh() {                refreshTime ++;                times = 0;                new Handler().postDelayed(new Runnable(){                    @Override                    public void run() {                        listData.clear();                        for(int i = 0; i < itemLimit ;i++){                            listData.add("item" + i + "after " + refreshTime + " times of refresh");                        }                        mAdapter.notifyDataSetChanged();                        mRecyclerView.refreshComplete();                    }                }, 1000);            //refresh data here            }            @Override            public void onLoadMore() {                Log.e("aaaaa","call onLoadMore");                if(times < 2){                    new Handler().postDelayed(new Runnable(){                        @Override                        public void run() {                            for(int i = 0; i < itemLimit ;i++){                                listData.add("item" + (1 + listData.size() ) );                            }                            mRecyclerView.loadMoreComplete();                            mAdapter.notifyDataSetChanged();                        }                    }, 1000);                } else {                    new Handler().postDelayed(new Runnable() {                        @Override                        public void run() {                            for(int i = 0; i < itemLimit ;i++){                                listData.add("item" + (1 + listData.size() ) );                            }                            mRecyclerView.setNoMore(true);                            mAdapter.notifyDataSetChanged();                        }                    }, 1000);                }                times ++;            }        });        //设置Item点击事件        mAdapter.setClickCallBack(new MyAdapter.ItemClickCallBack() {                    @Override                    public void onItemClick(int pos) {                        // a demo for notifyItemRemoved                        // TODO: 2017/12/21 one:移除//                        listData.remove(pos);//                        mRecyclerView.notifyItemRemoved(listData,pos);                        // TODO: 2017/12/21 two:跳转到第二个Activity                        startActivity(new Intent(MainActivity.this,CollapsingToolbarLayoutActivity.class));                    }                }        );
二、图片拉动字体缩放效果的实现
直接贴代码了
<!--todo XRecycleView、CoordinatorLayout、AppBarLayout、CollapsingToolbarLayout共同实现图片拉动与字体缩放效果--><!--todo 其中AppBarLayout的使用要引入jar包:com.android.support:appcompat-v7:26.0.0--><android.support.design.widget.CoordinatorLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:id="@+id/main_content"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:fitsSystemWindows="true">    <android.support.design.widget.AppBarLayout        android:id="@+id/appbar"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"        android:fitsSystemWindows="true">        <android.support.design.widget.CollapsingToolbarLayout            android:id="@+id/collapsing_toolbar"            android:layout_width="match_parent"            android:layout_height="match_parent"            app:layout_scrollFlags="scroll|exitUntilCollapsed"            android:fitsSystemWindows="true"            app:contentScrim="?attr/colorPrimary"            app:expandedTitleMarginStart="48dp"            app:expandedTitleMarginEnd="64dp">            <ImageView                android:layout_width="match_parent"                android:layout_height="match_parent"                android:scaleType="centerCrop"                android:fitsSystemWindows="true"                android:src="@drawable/headimg"                app:layout_collapseMode="parallax" />            <android.support.v7.widget.Toolbar                android:id="@+id/toolbar"                android:layout_width="match_parent"                android:layout_height="?attr/actionBarSize"                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"                app:layout_collapseMode="pin" />        </android.support.design.widget.CollapsingToolbarLayout>    </android.support.design.widget.AppBarLayout>    <com.jcodecraeer.xrecyclerview.XRecyclerView        android:id="@+id/recyclerview"        app:layout_behavior="@string/appbar_scrolling_view_behavior"        android:layout_width="fill_parent"        android:layout_height="fill_parent" /></android.support.design.widget.CoordinatorLayout>

demo下载地址猛戳这里

原创粉丝点击