1.在项目下的build.gradle中
allprojects {
repositories {
google()
jcenter()
maven { url 'https://jitpack.io' }
}
}
2.添加依赖
implementation 'com.android.support:design:27.1.1'
implementation 'com.github.qdxxxx:StickyHeaderDecoration:1.0.1'
3.设置
private void Data() {
mCars = new ArrayList<>();
mCars.add(new Car("奥迪", "A"));
mCars.add(new Car("阿尔法罗密欧", "A"));
mCars.add(new Car("阿斯顿马丁", "A"));
mCars.add(new Car("ALPINA", "A"));
mCars.add(new Car("安凯客车", "A"));
mCars.add(new Car("本田", "B"));
mCars.add(new Car("别克", "B"));
mCars.add(new Car("奔驰", "B"));
mCars.add(new Car("宝马", "B"));
mCars.add(new Car("保时捷", "B"));
mCars.add(new Car("比亚迪", "B"));
mCars.add(new Car("北京", "B"));
mCars.add(new Car("宾利", "B"));
mCars.add(new Car("巴博斯", "B"));
mCars.add(new Car("布加迪威龙", "B"));
mCars.add(new Car("长安", "C"));
mCars.add(new Car("长城", "C"));
mCars.add(new Car("大众", "D"));
mCars.add(new Car("东南", "D"));
mCars.add(new Car("东风", "D"));
mCars.add(new Car("DS", "D"));
mCars.add(new Car("道奇", "D"));
mCars.add(new Car("东风小康", "D"));
}
private void initView() {
// final LayoutInflater inflater = LayoutInflater.from(this);
mRlv = (RecyclerView) findViewById(R.id.rlv);
mRlv.setLayoutManager(new LinearLayoutManager(this));
RlvAdapter rlvAdapter = new RlvAdapter(mCars);
//返回头布局的内容
final NormalDecoration decoration = new NormalDecoration() {
@Override
public String getHeaderName(int i) {
return mCars.get(i).headerName;
}
};
//自定义头布局,可不设置
// decoration.setOnDecorationHeadDraw(new NormalDecoration.OnDecorationHeadDraw() {
// @Override
// public View getHeaderView(final int i) {
// View inflate = inflater.inflate(R.layout.item_header, null);
// TextView tv = inflate.findViewById(R.id.tv);
// tv.setText(mCars.get(i).headerName);
// return inflate;
// }
// });
mRlv.addItemDecoration(decoration);
//头布局的点击事件
decoration.setOnHeaderClickListener(new NormalDecoration.OnHeaderClickListener() {
@Override
public void headerClick(int i) {
Toast.makeText(MainActivity.this, mCars.get(i).headerName, Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this,FlowActivity.class));
}
});
mRlv.setAdapter(rlvAdapter);
}