Created
November 18, 2024 08:56
-
-
Save syimo/5b0e7fc9220ae1eb0f94924166e008a2 to your computer and use it in GitHub Desktop.
recyclerview with bottom seekbar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
| android:layout_width="match_parent" | |
| android:orientation="vertical" | |
| android:layout_height="match_parent"> | |
| <androidx.recyclerview.widget.RecyclerView | |
| android:layout_marginTop="100dp" | |
| android:background="@color/cardview_shadow_start_color" | |
| android:id="@+id/recyclerView" | |
| android:layout_width="match_parent" | |
| android:layout_height="wrap_content" | |
| android:orientation="horizontal" /> | |
| <SeekBar | |
| android:id="@+id/seekBar" | |
| android:layout_width="match_parent" | |
| android:layout_height="wrap_content" | |
| android:padding="16dp" /> | |
| </LinearLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class LoopingAdapter extends RecyclerView.Adapter<LoopingAdapter.ViewHolder> { | |
| private List<String> data; | |
| public LoopingAdapter(List<String> data) { | |
| this.data = data; | |
| } | |
| @NonNull | |
| @Override | |
| public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { | |
| View view = LayoutInflater.from(parent.getContext()) | |
| .inflate(R.layout.simple_text, parent, false); | |
| return new ViewHolder(view); | |
| } | |
| @Override | |
| public void onBindViewHolder(@NonNull ViewHolder holder, int position) { | |
| // 利用 position 进行循环映射 | |
| String text = data.get(position % data.size()); | |
| holder.textView.setText(text); | |
| } | |
| @Override | |
| public int getItemCount() { | |
| // 设置一个很大的值来模拟无限循环 | |
| return Integer.MAX_VALUE; | |
| } | |
| class ViewHolder extends RecyclerView.ViewHolder { | |
| TextView textView; | |
| ViewHolder(View itemView) { | |
| super(itemView); | |
| textView = itemView.findViewById(R.id.tv); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MainActivity : AppCompatActivity() { | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| enableEdgeToEdge() | |
| setContentView(R.layout.activity_main) | |
| // ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets -> | |
| // val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()) | |
| // v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom) | |
| // insets | |
| // } | |
| val recyclerView = findViewById<RecyclerView>(R.id.recyclerView) | |
| val seekBar = findViewById<SeekBar>(R.id.seekBar) | |
| // 准备 12 个数据 | |
| val data: MutableList<String> = ArrayList() | |
| for (i in 1..50) { | |
| data.add("Item $i") | |
| } | |
| // 设置 Adapter | |
| val adapter = LoopingAdapter(data) | |
| recyclerView.adapter = adapter | |
| // 设置 SnapHelper | |
| // val snapHelper = LinearSnapHelper() | |
| // snapHelper.attachToRecyclerView(recyclerView) | |
| // 设置横向 LinearLayoutManager | |
| val layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false) | |
| recyclerView.layoutManager = layoutManager | |
| // 设置起始位置 | |
| val startPosition = Int.MAX_VALUE / 2 - (Int.MAX_VALUE / 2 % data.size) | |
| recyclerView.scrollToPosition(startPosition) | |
| Log.e("asdasdasdas", "stat===" + startPosition) | |
| // 添加滚动监听,对齐到第一个可见项目 | |
| recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() { | |
| override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) { | |
| super.onScrollStateChanged(recyclerView, newState) | |
| if (newState == RecyclerView.SCROLL_STATE_IDLE) { // 滑动停止 | |
| val layoutManager = recyclerView.layoutManager as LinearLayoutManager? | |
| layoutManager?.let { | |
| val firstVisiblePosition = it.findFirstVisibleItemPosition() | |
| val firstVisibleView = it.findViewByPosition(firstVisiblePosition) | |
| firstVisibleView?.let { view -> | |
| val offset = view.left // 计算偏移量 | |
| if (offset != 0) { // 仅当未对齐时才滑动 | |
| recyclerView.smoothScrollBy(offset, 0) | |
| } | |
| } | |
| } | |
| } | |
| } | |
| }) | |
| // 设置 SeekBar 最大值为数据量 | |
| seekBar.max = data.size - 1 | |
| // 监听 RecyclerView 滚动 | |
| recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() { | |
| override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { | |
| super.onScrolled(recyclerView, dx, dy) | |
| val firstVisibleItem = layoutManager.findFirstVisibleItemPosition() % data.size | |
| seekBar.progress = firstVisibleItem | |
| Log.e("asdasdasdas", "stat===" + firstVisibleItem) | |
| } | |
| }) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="utf-8"?> | |
| <FrameLayout | |
| xmlns:android="http://schemas.android.com/apk/res/android" | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content"> | |
| <TextView | |
| android:id="@+id/tv" | |
| android:text="asdsaddasda" | |
| android:layout_width="150dp" | |
| android:background="@android:color/holo_blue_light" | |
| android:layout_marginEnd="10dp" | |
| android:layout_height="50dp"> | |
| </TextView> | |
| </FrameLayout> |
Author
syimo
commented
Nov 18, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment