編輯:關於Android編程
先看效果圖

主要處理的地方:
1、RecyclerView中Adapter的item個人可以無限輪詢.
2、RecyclerView自動滑動
3、手指按下時滑動停止,手指抬起後繼續自動滑動
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);
}
}
Adapter處理:主要處理getItemCount()和數據填充的onBindViewHolder()方法
public class AutoPollAdapter extends RecyclerView.Adapter<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.item_auto_poll, 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.setText(R.id.tv_content,data);
}
@Override
public int getItemCount() {
return Integer.MAX_VALUE;
}
}
最後附上Activity調用的代碼
public class AutoPollRecyclerActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_auto_poll);
initToolBar();
initView();
}
private void initView() {
AutoPollRecyclerView mRecyclerView = (AutoPollRecyclerView) findViewById(R.id.rv_recycleView);
List<String> list = new ArrayList<>();
for (int i = 0; i < 5; ) {
list.add(" Item: " + ++i);
}
AutoPollAdapter adapter = new AutoPollAdapter(this, list);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.HORIZONTAL_LIST));
mRecyclerView.setAdapter(adapter);
if (true) //保證itemCount的總個數寬度超過屏幕寬度->自己處理
mRecyclerView.start();
}
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。
clipse項目遷移到android studio的方法(圖文最新版)
前言最近Android studio(下文簡稱AS)官方發布了正式版,目前火得不行。個人認為主要是因為android是google自家的產品,AS也是他自己搞的IDE,以
Android中轉場動畫的實現與兼容性處理
前言在 Android 5.0 之前,我們已經有了 overridePendingTransition() 方法來實現一些轉場效果。然而,在 Android 5.0 以後
Android Studio使用心得 - 簡介與環境配置
關於Android Studio 在2013 Google IO大會上發布的全新IDE Android Studio相信各位猿們並不陌生,截止2014的Goog
Google官方 詳解 Android 性能優化
為什麼關注性能對於一款APP,用戶首先關注的是 app的性能,而不是APP本身的屬性功能,用戶不關心你是否是搞社交,是否搞電商,是否是一款強大的美圖濾鏡app,用戶首先關