自动滚动的RecyclerView 所以需要写一个类继承 RecyclerView 代码如下:
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import java.lang.ref.WeakReference;
public class AutoPollRecyclerView extends RecyclerView {
private static final long TIME_AUTO_POLL = 16;
AutoPollTask autoPollTask;
private boolean running; //标示是否正在自动轮询
private boolean canRun;//标示是否可以自动轮询,可在不需要的是否置false
public AutoPollRecyclerView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
autoPollTask = new AutoPollTask(this);
}
static class AutoPollTask implements Runnable {
private final WeakReference<AutoPollRecyclerView> mReference;
//使用弱引用持有外部类引用->防止内存泄漏
public AutoPollTask(AutoPollRecyclerView reference) {
this.mReference = new WeakReference<AutoPollRecyclerView>(reference);
}
@Override
public void run() {
AutoPollRecyclerView recyclerView = mReference.get();
if (recyclerView != null && recyclerView.running && recyclerView.canRun) {
recyclerView.scrollBy(2, 2);
recyclerView.postDelayed(recyclerView.autoPollTask, recyclerView.TIME_AUTO_POLL);
}
}
}
//开启:如果正在运行,先停止->再开启
public void start() {
if (running)
stop();
canRun = true;
running = true;
postDelayed(autoPollTask, TIME_AUTO_POLL);
}
public void stop() {
running = false;
removeCallbacks(autoPollTask);
}
@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)
start();
break;
}
return super.onTouchEvent(e);//return false,注释掉onTouchEvent()方法里面的stop和start方法,则列表自动滚动且不可触摸
}
}
适配器的写法如下代码:
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
public class AutoPollAdapter extends RecyclerView.Adapter<AutoPollAdapter.BaseViewHolder> {
private final Context mContext;
private final List<String> mData;
public AutoPollAdapter(Context context, List<String> list) {
this.mContext = context;
this.mData = list;
}
@Override
public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false);
BaseViewHolder holder = new BaseViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(BaseViewHolder holder, int position) {
String data = mData.get(position % mData.size());
holder.content.setText(data);
}
@Override
public int getItemCount() {
return Integer.MAX_VALUE;
}
class BaseViewHolder extends RecyclerView.ViewHolder {
TextView content;
public BaseViewHolder(View itemView) {
super(itemView);
content = (TextView) itemView.findViewById(R.id.content);
}
}
}
Activity调用的写法:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<String> mData = new ArrayList<>();
for (int i = 0; i < 30; i++) {
mData.add("加载的第" + i + "条数据");
}
AutoPollRecyclerView recyclerView = (AutoPollRecyclerView) findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));//设置LinearLayoutManager.HORIZONTAL 则水平滚动
AutoPollAdapter mAdapter = new AutoPollAdapter(this, mData);
recyclerView.setAdapter(mAdapter);
recyclerView.start();//不能掉,不然不会自动滚动,根据自己的需求来
}
}