需求:HorizontalScrollView 默認顯示首項,需初始化滾動到末尾展示後續選項。原文提供完整 HorizontalListView 源碼(繼承 AdapterView<ListAdapter>),支持數據適配器、滑動回收、手勢衝突處理。
public synchronized void scrollTo(int x) {
mScroller.startScroll(mNextX, 0, x - mNextX, 0);
requestLayout();
}
// 調用:horizontalListView.scrollTo(maxX); // maxX 為內容總寬 - 視口寬
mRemovedViewQueue 復用離屏 View,避免頻繁 GConInterceptTouchEvent、dispatchTouchEvent 配合 GestureDetector 處理水平滑動與嵌套 ViewPager 衝突onLayout 中根據 mCurrentX 計算子 View 位置,支持邊界彈性若不需複雜適配器,直接用 HorizontalScrollView + LinearLayout 並在 onWindowFocusChanged 中調用:
hsv.post(() -> hsv.fullScroll(View.FOCUS_RIGHT));
RecyclerView rv = findViewById(R.id.rv);
rv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
rv.scrollToPosition(adapter.getItemCount() - 1);