小編給大家分享一下Android中如何實(shí)現(xiàn)RecyclerView滾動(dòng)到中間位置,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比邊壩網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式邊壩網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋邊壩地區(qū)。費(fèi)用合理售后完善,十多年實(shí)體公司更值得信賴。目標(biāo)
點(diǎn)擊某個(gè)條目,在經(jīng)過(guò)4s無(wú)任何操作之后,該條目滾動(dòng)到中間位置顯示。點(diǎn)擊后,用戶在滑動(dòng),等用戶不操作后再開始延時(shí)。用戶多次點(diǎn)擊,記最后一次點(diǎn)擊位置。
分析
首先先考慮,滾動(dòng)到指定位置是如何操作的?
// 滾動(dòng)到指定位置 recyclerView.scrollToPosition(position); // 平滑滾動(dòng)到指定位置 recyclerView.smoothScrollToPosition(position);
有沒有滾動(dòng)到制定像素位置呢?
// scrollBy(x, y)這個(gè)方法是自己去控制移動(dòng)的距離,單位是像素,所以在使用scrollBy(x, y)需要自己去計(jì)算移動(dòng)的高度或?qū)挾取?recyclerView.scrollBy(x, y)
可是,問(wèn)題是滾動(dòng)到中間位置啊?這個(gè)怎么辦呢?這樣子行不行呢?
mRecyclerView.scrollToPosition(0); mRecyclerView.scrollBy(0,400);
先滾動(dòng)到制定位置,在滾動(dòng)一段距離不就好了?運(yùn)行發(fā)現(xiàn),這兩行代碼只執(zhí)行第一行,第二行無(wú)效。
debug 調(diào)試看了下,還是沒有弄懂,實(shí)現(xiàn)太復(fù)雜。
那就是說(shuō)這樣是不行的,那有沒有其他辦法呢?
RecyclerView 有一個(gè)滾動(dòng)監(jiān)聽方法:
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); } @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); } });
onScrollStateChanged 方法對(duì)應(yīng)三種狀態(tài):靜止(SCROLL_STATE_IDLE),拖動(dòng)滾動(dòng)(SCROLL_STATE_DRAGGING),滑動(dòng)(SCROLL_STATE_SETTLING)。
當(dāng)手動(dòng)緩慢滑動(dòng)的時(shí)候,會(huì)觸發(fā): onScrollStateChanged (拖動(dòng)滾動(dòng)) --> (n個(gè))onScrolled -->onScrollStateChanged(靜止);
當(dāng)手快速滑動(dòng)的時(shí)候,會(huì)觸發(fā): onScrollStateChanged (拖動(dòng)滾動(dòng)) --> (n個(gè))onScrolled --> onScrollStateChanged (滑動(dòng)) -->
(n個(gè))onScrolled --> onScrollStateChanged (靜止);
有想法了,點(diǎn)擊的時(shí)候,先運(yùn)行 scrollToPosition,在 onScrolled 方法里面 運(yùn)行 scrollBy 方法。寫代碼,運(yùn)行,通過(guò)。
下面就是中間位置的計(jì)算了。
首先計(jì)算出 recylerview 的展現(xiàn)高度。
Rect rect = new Rect(); mRecyclerView.getGlobalVisibleRect(rect); reHeight = rect.bottom - rect.top - vHeight; 當(dāng)運(yùn)行 scrollToPosition 后,點(diǎn)擊條目就會(huì)出現(xiàn)在視野當(dāng)中,這時(shí)候,計(jì)算出相應(yīng)的位移即可。需要注意一點(diǎn)的是,當(dāng)點(diǎn)擊條目在視野內(nèi)的時(shí)候,是不會(huì)運(yùn)行 scrollToPosition 方法的。 int top = mRecyclerView.getChildAt(position - firstPosition).getTop(); int half = reHeight / 2; mRecyclerView.scrollBy(0, top - half);
最后就是延時(shí)的設(shè)定,采用Handler 進(jìn)行延時(shí)。
代碼
核心代碼如下:
public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; private RecyclerView mRecyclerView; private LinearLayoutManager mLayoutManager; private RecyclerView.Adapter mAdapter; private String[] data; private Handler handler; private boolean isClick = false; private static int vHeight = -1; private static int reHeight = -1; private static int position = 0; private static final int target = 10; private static boolean isMove = false; private Runnable runnable; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); handler = new Handler(); mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view); //創(chuàng)建默認(rèn)的線性LayoutManager mLayoutManager = new LinearLayoutManager(this); mLayoutManager.setAutoMeasureEnabled(true); mRecyclerView.setLayoutManager(mLayoutManager); //如果可以確定每個(gè)item的高度是固定的,設(shè)置這個(gè)選項(xiàng)可以提高性能 mRecyclerView.setHasFixedSize(true); mRecyclerView.setNestedScrollingEnabled(false); data = new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21"}; runnable = new Runnable() { @Override public void run() { if (isVisible()) { scrollToMiddle(); } else { mRecyclerView.scrollToPosition(position); isMove = true; isClick = false; } } }; mAdapter = new MyAdapter(data, new MyAdapter.onRecyclerViewItemClick() { @Override public void onItemClick(View v, int pos) { Toast.makeText(MainActivity.this, "第" + pos + "行", Toast.LENGTH_SHORT).show(); position = pos; vHeight = v.getHeight(); Rect rect = new Rect(); mRecyclerView.getGlobalVisibleRect(rect); reHeight = rect.bottom - rect.top - vHeight; // handler.removeCallbacksAndMessages(null); handler.removeCallbacks(runnable); handler.postDelayed(runnable, 4000); isClick = true; } }); mRecyclerView.setAdapter(mAdapter); mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); Log.d(TAG, "" + newState); if (newState == RecyclerView.SCROLL_STATE_DRAGGING && !isMove) { handler.removeCallbacks(runnable); } if (newState == RecyclerView.SCROLL_STATE_IDLE) { if (isClick) { handler.postDelayed(runnable, 4000); } } } @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); if (isMove) { if (vHeight < 0) { isMove = false; return; } scrollToMiddle(); } } }); public void scrollToMiddle() { final int firstPosition = mLayoutManager.findFirstVisibleItemPosition(); int top = mRecyclerView.getChildAt(position - firstPosition).getTop(); Log.d(TAG, " position" + position + " " + top); int half = reHeight / 2; mRecyclerView.scrollBy(0, top - half); isMove = false; } public boolean isVisible() { final int firstPosition = mLayoutManager.findFirstVisibleItemPosition(); final int lastPosition = mLayoutManager.findLastVisibleItemPosition(); return position <= lastPosition && position >= firstPosition; } @Override protected void onDestroy() { super.onDestroy(); handler.removeCallbacksAndMessages(null); handler = null; } }
看完了這篇文章,相信你對(duì)“Android中如何實(shí)現(xiàn)RecyclerView滾動(dòng)到中間位置”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!