在app
的build.gradle
在添加以下代码
1、implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:3.0.6'
,这个里面带的适配器,直接调用就即可
BaseRecyclerViewAdapterHelper
简称BRVAH
Android SDK |
是否支持BaseRecyclerViewAdapterHelper:3.0.6 |
---|---|
android compileSdkVersion 29 |
是 |
android compileSdkVersion 30 |
是 |
android compileSdkVersion 31 |
是 |
android compileSdkVersion 32 |
是 |
android compileSdkVersion 33 |
是 |
这依赖包还需要得到要添加,在Project
的build.gradle
在添加以下代码,不添加就不行
allprojects {
repositories {
...
maven { url "https://jitpack.io" }//加上
}
}
1、主活动布局中标题栏
<RelativeLayout
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="@dimen/title_height"
android:background="#4CAF50">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="标题栏 "
android:textColor="#F44336"
android:textSize="22sp" />
RelativeLayout>
2、RecyclerView
的标题栏高度
recyclerView.translationY, resources.getDimension(R.dimen.title_height)
3、注意:主活动布局中和ListView的标题栏高度必须要一样
<dimen name="title_height">50dpdimen>
RvAdapter.kt
package com.example.myapplication3.adapter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.example.myapplication3.R
import kotlinx.android.synthetic.main.item.view.*
class RvAdapter(layoutResId: Int = R.layout.item) :
BaseQuickAdapter<String, BaseViewHolder>(layoutResId) {
override fun convert(holder: BaseViewHolder, item: String) {
holder.itemView.run {
tv_content.text = item
}
}
}
item
布局item.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_menu_layout"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#e9e9e9">
<RelativeLayout
android:id="@+id/ll_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
RelativeLayout>
RelativeLayout>
MainActivity.kt
package com.example.myapplication3
import android.animation.Animator
import android.animation.ObjectAnimator
import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import android.view.ViewConfiguration
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.myapplication3.adapter.RvAdapter
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
var list = ArrayList<String>()
private var mTouchSlop = 0
private var mFirstY = 0f
private var mCurrentY = 0f
private val mAdapter by lazy {
RvAdapter()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mTouchSlop = ViewConfiguration.get(this@MainActivity).scaledTouchSlop
init()
showHideTitleBar(true);
}
private fun init() {
val layoutManager = LinearLayoutManager(this@MainActivity)
layoutManager.orientation = LinearLayoutManager.VERTICAL
recyclerView.layoutManager = layoutManager
recyclerView.adapter = mAdapter
for (i in 0..50) {
list.add("第 ${i + 1} 行")
}
mAdapter.setList(list)
recyclerView.setOnTouchListener(View.OnTouchListener{ v, event ->
when(event.action){
MotionEvent.ACTION_DOWN -> {
mFirstY = event.y
}
MotionEvent.ACTION_MOVE -> {
mCurrentY = event.y
if (mCurrentY - mFirstY > mTouchSlop) {
// 下滑 显示titleBar
showHideTitleBar(true)
}else if (mFirstY - mCurrentY > mTouchSlop){
// 上滑 隐藏titleBar
showHideTitleBar(false)
}
}
MotionEvent.ACTION_UP ->{
}
}
return@OnTouchListener false
})
}
private var mAnimatorTitle: Animator? = null
private var mAnimatorContent: Animator? = null
private fun showHideTitleBar(tag: Boolean) {
if (mAnimatorTitle != null && mAnimatorTitle!!.isRunning) {
mAnimatorTitle!!.cancel()
}
if (mAnimatorContent != null && mAnimatorContent!!.isRunning) {
mAnimatorContent!!.cancel()
}
if (tag) {
// mAnimatorTitle = ObjectAnimator.ofFloat(rl_title, "translationY", rl_title.getTranslationY(), 0)
mAnimatorTitle = ObjectAnimator.ofFloat(rl_title, "translationY", rl_title.translationX,0f)
// mAnimatorContent = ObjectAnimator.ofFloat(recyclerView, "translationY", recyclerView.getTranslationY(), getResources().getDimension(R.dimen.title_height))
mAnimatorContent = ObjectAnimator.ofFloat(recyclerView, "translationY", recyclerView.translationY, resources.getDimension(R.dimen.title_height))
} else {
// mAnimatorTitle = ObjectAnimator.ofFloat(rl_title, "translationY", rl_title.getTranslationY(), -rl_title.getHeight())
mAnimatorTitle = ObjectAnimator.ofFloat(rl_title, "translationY", rl_title.translationY, -resources.getDimension(R.dimen.title_height))
// mAnimatorContent = ObjectAnimator.ofFloat(recyclerView, "translationY", recyclerView.getTranslationY(), 0)
mAnimatorContent = ObjectAnimator.ofFloat(recyclerView, "translationY", recyclerView.translationY, 0f)
}
mAnimatorTitle!!.start();
mAnimatorContent!!.start();
}
// public class MainActivity extends AppCompatActivity {
//
// private RelativeLayout mTitle;
// ArrayList list = new ArrayList();
// ArrayAdapter adapter;
// private ListView mListView;
// private int mTouchSlop;
// private float mFirstY;
// private float mCurrentY;
//
// @Override
// protected void onCreate(Bundle savedInstanceState) {
// super.onCreate(savedInstanceState);
// requestWindowFeature(Window.FEATURE_NO_TITLE);
// setContentView(R.layout.activity_main);
// mTouchSlop = ViewConfiguration.get(this).getScaledTouchSlop();
//
// initViews();
// showHideTitleBar(true);
// }
//
// private void initViews() {
// mListView = (ListView) findViewById(R.id.listview);
// mTitle = (RelativeLayout) findViewById(R.id.title);
//
// for (int i = 0; i < 100; i++) { //设置数据
// list.add("第 " + (i + 1) + " 行");
// }
//
// adapter = new ArrayAdapter(this, android.R.layout.activity_list_item, android.R.id.text1, list);
// mListView.setAdapter(adapter);
// mListView.setOnTouchListener(new View.OnTouchListener() {
//
// @Override
// public boolean onTouch(View v, MotionEvent event) {
// switch (event.getAction()) {
// case MotionEvent.ACTION_DOWN:
// mFirstY = event.getY();
// break;
// case MotionEvent.ACTION_MOVE:
// mCurrentY = event.getY();
// if (mCurrentY - mFirstY > mTouchSlop) {
// System.out.println("mtouchislop:" + mTouchSlop);
// // 下滑 显示titleBar
// showHideTitleBar(true);
// } else if (mFirstY - mCurrentY > mTouchSlop) {
// // 上滑 隐藏titleBar
// showHideTitleBar(false);
// }
// break;
// case MotionEvent.ACTION_UP:
// break;
// }
// return false;
// }
// });
// }
//
// private Animator mAnimatorTitle;
// private Animator mAnimatorContent;
//
// private void showHideTitleBar(boolean tag) {
// if (mAnimatorTitle != null && mAnimatorTitle.isRunning()) {
// mAnimatorTitle.cancel();
// }
// if (mAnimatorContent != null && mAnimatorContent.isRunning()) {
// mAnimatorContent.cancel();
// }
// if (tag) {
// mAnimatorTitle = ObjectAnimator.ofFloat(mTitle, "translationY", mTitle.getTranslationY(), 0);
// mAnimatorContent = ObjectAnimator.ofFloat(mListView, "translationY", mListView.getTranslationY(), getResources().getDimension(R.dimen.title_height));
//
// } else {
// mAnimatorTitle = ObjectAnimator.ofFloat(mTitle, "translationY", mTitle.getTranslationY(), -mTitle.getHeight());
// mAnimatorContent = ObjectAnimator.ofFloat(mListView, "translationY", mListView.getTranslationY(), 0);
// }
// mAnimatorTitle.start();
// mAnimatorContent.start();
// }
// }
}
代码中:return@OnTouchListener false
设置成true
,列表第一行就一直滑到顶部下面,不是超顶部屏障,如下图:
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/rl_title"
android:layout_width="match_parent"
android:layout_height="@dimen/title_height"
android:background="#4CAF50">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="标题栏 "
android:textColor="#F44336"
android:textSize="22sp" />
RelativeLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
RelativeLayout>