RecyclerView 实现上浮的标题

RecyclerView 相信大家都已经不默认的吧!我就直接上代码吧,简单粗暴一点

1.先引导RecyclerView的包

   在build.gradle 文件dependencies加上

compile 'com.android.support:cardview-v7:24.0.0'
compile 'com.android.support:recyclerview-v7:24.0.0'

2.创建布局文件

xml version="1.0" encoding="utf-8"?>
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

            android:layout_width="match_parent"
        android:layout_height="match_parent">

                    android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        

                    android:id="@+id/tvTop"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="#FFFFFF"
            android:gravity="center_vertical"
            android:paddingLeft="15dp"
            android:text="标题"
            android:textColor="#000000"
            android:textSize="18sp"/>
    

3.在代码里使用RecyclerView,注释都觉的很详细的啦

public class MainActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private TextView tvTopBar;

    /** 浮动标题的高度 */
    private int mTopBarHeight = 0;
    /** 当前可见的第一个item的下标 */
    private int mCurrentPosition = 0;
    /** 线性管理器 */
    private LinearLayoutManager linearLayoutManager;
    /** 数据源 */
    private ArrayList mList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /**查找组件*/
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        tvTopBar = (TextView) findViewById(R.id.tvTop);

        linearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(linearLayoutManager);//设置线性显示
//        mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));//设置线性宫格显示,相似于gridView
//        mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, OrientationHelper.HORIZONTAL));//线性宫格布局,类似于瀑布流
        mList = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            mList.add("选项===>>" + i);
        }
        mRecyclerView.setAdapter(new MyRecyclerViewAdapter(this, mList));
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                //获取上浮title的高度
                mTopBarHeight = tvTopBar.getHeight();
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                //当滑动的时候获取当前滑动的下一个item的视图
                View view = linearLayoutManager.findViewByPosition(mCurrentPosition + 1);
                if (view == null) {
                    return;
                }
                //view距离手机顶端的top值小于等于浮动title的高度的时候
                if (view.getTop() <= mTopBarHeight) {
                    //浮动标题的高度-view距离top的距离,计算出view和浮动层的交集部分,
                    //然后设置浮动viewtop值,保证滑动的view始终是在浮动层的下方
                    tvTopBar.setY(-(mTopBarHeight - view.getTop()));
                } else {
                    tvTopBar.setY(0);
                }

                if (mCurrentPosition != linearLayoutManager.findFirstVisibleItemPosition()) {
                    mCurrentPosition = linearLayoutManager.findFirstVisibleItemPosition();
                    tvTopBar.setY(0);
                    tvTopBar.setText(mList.get(mCurrentPosition));
                }
            }
        });
    }
}

4.创建RecyclerView的Adapter

public class MyRecyclerViewAdapter extends RecyclerView.Adapter {

    private Context context;
    private List mList;

    public MyRecyclerViewAdapter(Context context, List mList) {
        this.context = context;
        this.mList = mList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.view_item, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.tvTitle.setText(mList.get(position));
    }


    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        public TextView tvTitle;

        public ViewHolder(View itemView) {
            super(itemView);

            tvTitle = (TextView) itemView.findViewById(R.id.tvTitle);
        }
    }
}

5.创建一个item的布局

    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

            android:id="@+id/tvTitle"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center_vertical"
        android:paddingLeft="15dp"
        android:text="标题"
        android:textColor="#000000"
        android:textSize="18sp"/>

            android:layout_width="match_parent"
        android:layout_height="400dp"
        android:scaleType="fitXY"
        android:src="@drawable/img"/>


你可能感兴趣的:(Android)