gpt4 book ai didi

java - 水平 RecyclerView 中的上一个和下一个按钮

转载 作者:行者123 更新时间:2023-12-01 19:13:31 25 4
gpt4 key购买 nike

我正在创建电子商务应用程序,我想问你当我单击“下一个”按钮时,RecyclerView 向右移动并显示下一个项目,当我单击“上一个”按钮时,RecyclerView 向左移动并显示上一个项目?

RecyclerView 外部的上一个和下一个按钮:

1

代码:

private RecyclerView rcTopPicks;
private RecyclerView.Adapter adapter;
private LinearLayoutManager layoutManager;
private ImageView previousTopPicks, nextTopPicks;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);

previousTopPicks = view.findViewById(R.id.btn_left_top_picks);
nextTopPicks = view.findViewById(R.id.btn_right_top_picks);

initView(view);
getTopPicksData();

return view;
}

private void initViews(View view) {
rcTopPicks = view.findViewById(R.id.rc_top_picks);
layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
rcTopPicks.setLayoutManager(layoutManager);
}

private void getTopPicksData() {
// Code for get JSON
....................
adapter = new TopPicksAdapter(productList);
rcTopPicks.setAdapter(adapter);

// This button is not move to left and not showing 2 previous items
previousTopPicks.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
rcTopPicks.scrollToPosition(adapter.getItemCount() - 1);
}
});

// This button is not move to right and not showing 2 next items
nextTopPicks.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
rcTopPicks.scrollToPosition(adapter.getItemCount() + 1);
}
});
}

问题是当我点击下一步按钮时如何将recyclerview的位置滚动到右侧?当我单击上一个按钮时,如何将 recyclerview 的位置滚动到左侧?

最佳答案

单击“下一个”和“上一个”按钮,此代码从当前位置滚动到下一个/上一个 View 。

  binding.btPrevImage.setOnClickListener{
val imageCurrentPosition= (binding.rvAllImage.layoutManager as LinearLayoutManager).findFirstVisibleItemPosition()
if(imageCurrentPosition>0){
binding.rvAllImage.smoothScrollToPosition(imageCurrentPosition-1)
}
}
binding.btNextImage.setOnClickListener{
val imageCurrentPosition= (binding.rvAllImage.layoutManager as LinearLayoutManager).findLastVisibleItemPosition()
if(imageCurrentPosition<productImageAdapter.itemCount-1){
binding.rvAllImage.smoothScrollToPosition(imageCurrentPosition+1)
}
}

关于java - 水平 RecyclerView 中的上一个和下一个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59445838/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com