Recyclerview本身没有自动滚动的方法以及这种效果
那么我们首先想到的就是自定义View
创建一个用来写自定义View的类
public class MYScrView extends RecyclerView {
private Autoaaview autoview;
private boolean running;
private boolean canrun;
private static final int Timea = 16;
public MYScrView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
autoview = new Autoaaview(this);
}
public MYScrView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
private class Autoaaview implements Runnable{
WeakReference<MYScrView> myScrViewWeakReference;
public Autoaaview(MYScrView myScrView) {
myScrViewWeakReference = new WeakReference<>(myScrView);
}
@Override
public void run() {
MYScrView myScrView = myScrViewWeakReference.get();
// myScrView!=null&&
if (myScrView.canrun&&myScrView.running){
myScrView.scrollBy(2,2);
myScrView.postDelayed(myScrView.autoview,Timea);
}
}
}
public void stort(){
if (running)
stop();
running = true;
canrun = true;
postDelayed(autoview,Timea);
}
private void stop() {
running = false;
removeCallbacks(autoview);
}
@Override
public boolean onTouchEvent(MotionEvent e) {
switch (e.getAction()){
case MotionEvent.ACTION_DOWN:
if (running){
stop();
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_OUTSIDE:
if (canrun){
stort();
}
break;
}
return super.onTouchEvent(e);
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<com.example.myappli01.MYScrView
android:layout_width="wrap_content"
android:id="@+id/Myscr"
android:layout_height="300dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:layout_height="56dp">
<EditText
android:layout_width="300dp"
android:layout_height="match_parent"
android:id="@+id/pl"
android:background="@color/colorPrimary"/>
<Button
android:layout_width="wrap_content"
android:id="@+id/send"
android:layout_height="wrap_content"
android:text="发送"/>
</LinearLayout>
</RelativeLayout>
RecyclerView 用来给Adapter传送的item
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="24dp"
android:id="@+id/textaaa"/>
</LinearLayout>
这是在Activity里面写的
//Activity里面的点击事件的方法
private void send() {
//发送的点击事件
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//运用Dialog再次判断 正常情况下是不需要判断的,自己灵活运用
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("发送");
builder.setMessage("是否发送");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String trim = pl.getText().toString().trim();
if (trim.equals("")) {
Toast.makeText(MainActivity.this, "不允许为空",
Toast.LENGTH_SHORT).show();
} else {
strings.add(new My(trim));
addDanmaku(trim,true);
if (myscrviewAdapter == null) {
myscrviewAdapter = new
MyscrviewAdapter(MainActivity.this, strings);
Myscr.setAdapter(myscrviewAdapter);
Myscr.setLayoutManager(new
LinearLayoutManager(MainActivity.this));
if (true)
Myscr.stort();
} else {
myscrviewAdapter.notifyDataSetChanged();
}
}
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
builder.show();
}
});
}
上文中的Adapter 的适配器
public class MyscrviewAdapter extends RecyclerView.Adapter<BaseViewHolder> {
Context context;
ArrayList<My> mies;
public MyscrviewAdapter(Context context, ArrayList<My> mies) {
this.context = context;
this.mies = mies;
}
@NonNull
@Override
public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View inflate = LayoutInflater.from(context).inflate(R.layout.string, parent,false);
BaseViewHolder baseViewHolder = new BaseViewHolder(inflate);
return baseViewHolder;
}
@Override
public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
holder.setText(R.id.textaaa,mies.get(position%mies.size()).getString()+"");
}
@Override
public int getItemCount() {
return Integer.MAX_VALUE;
}
}
My的小写bean类
public class My {
String string;
public My() {
}
public My(String string) {
this.string = string;
}
public String getString() {
return string;
}
public void setString(String string) {
this.string = string;
}
}
maven { url "https://jitpack.io" }
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.22'