1.自定义Recycleview
package com.bawei.myapplication.view;
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import com.gavin.com.library.BaseDecoration;
/**
* Created by gavin
* date 2018/8/26
*/
public class MyRecyclerView extends RecyclerView {
private BaseDecoration mDecoration;
public MyRecyclerView(Context context) {
super(context);
}
public MyRecyclerView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public MyRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void addItemDecoration(ItemDecoration decor) {
if (decor != null && decor instanceof BaseDecoration) {
mDecoration = (BaseDecoration) decor;
}
super.addItemDecoration(decor);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent e) {
if (mDecoration != null) {
switch (e.getAction()) {
case MotionEvent.ACTION_DOWN:
mDecoration.onEventDown(e);
break;
case MotionEvent.ACTION_UP:
if (mDecoration.onEventUp(e)) {
return true;
}
break;
default:
}
}
return super.onInterceptTouchEvent(e);
}
}
2.Activity
package com.bawei.myapplication;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;
import com.bawei.myapplication.model.City;
import com.bawei.myapplication.util.CityUtil;
import com.bawei.myapplication.adapter.StickyAdapter;
import com.bawei.myapplication.view.MyRecyclerView;
import com.gavin.com.library.StickyDecoration;
import com.gavin.com.library.listener.GroupListener;
import com.gavin.com.library.listener.OnGroupClickListener;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
/**
* 文字悬浮
*/
public class StickyRecyclerViewActivity extends AppCompatActivity {
@BindView(R.id.rv)
MyRecyclerView mRecyclerView;
StickyAdapter mStickyAdapter;
List<City> dataList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sticky_recycler_view);
ButterKnife.bind(this);
initView();
}
private void initView() {
//模拟数据
dataList.addAll(CityUtil.getCityList());
dataList.addAll(CityUtil.getCityList());
//------------- StickyDecoration 使用部分 ----------------
StickyDecoration decoration = StickyDecoration.Builder
.init(new GroupListener() {
@Override
public String getGroupName(int position) {
//组名回调
if (position < dataList.size() && position > -1) {
//获取组名,用于判断是否是同一组
return dataList.get(position).getProvince();
}
return null;
}
})
//背景色
.setGroupBackground(ContextCompat.getColor(this, R.color.color_group_background))
//高度
.setGroupHeight(getResources().getDimensionPixelOffset(R.dimen.dp_35))
//分割线颜色
.setDivideColor(ContextCompat.getColor(this, R.color.color_divide))
//分割线高度 (默认没有分割线)
.setDivideHeight(getResources().getDimensionPixelOffset(R.dimen.dp_2))
//字体颜色 (默认黑色)
.setGroupTextColor(Color.RED)
//字体大小
.setGroupTextSize(getResources().getDimensionPixelOffset(R.dimen.sp_15))
// 边距 靠左时为左边距 靠右时为右边距
.setTextSideMargin(getResources().getDimensionPixelOffset(R.dimen.dp_10))
//点击事件,返回当前分组下的第一个item的position
.setOnClickListener(new OnGroupClickListener() {
@Override
public void onClick(int position, int id) {
//Group点击事件
String content = "onGroupClick --> " + dataList.get(position).getProvince();
Toast.makeText(StickyRecyclerViewActivity.this, content, Toast.LENGTH_SHORT).show();
}
})
.build();
//------------- StickyDecoration 使用部分 ----------------
RecyclerView.LayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
mRecyclerView.setLayoutManager(manager);
mRecyclerView.addItemDecoration(decoration);
mStickyAdapter = new StickyAdapter(this, dataList);
mRecyclerView.setAdapter(mStickyAdapter);
}
}
3.布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:elevation="0dp">
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="我是一个Banner"
android:gravity="center"
android:background="#ffffff"
android:textSize="30sp"
app:layout_scrollFlags="scroll|enterAlways"/>
</android.support.design.widget.AppBarLayout>
<com.bawei.myapplication.view.MyRecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>
</android.support.design.widget.CoordinatorLayout>