在recyclerView制作的图像滑块中添加下一个,上一个按钮

时间:2019-07-15 09:14:42

标签: java android button android-recyclerview

我已经在recyclerViewSnapHelper的帮助下制作了Image滑块。我还在recyclerView下添加了下一个prev imageView。

我想将它们实现为下一个,上一个。他们的ID分别是下一个,上一个。

这是显示imageSlider的活动:

public class RajeshDaiActivity extends AppCompatActivity implements RajeshDaiAdapter.OnNoteListener {
public RecyclerView recyclerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rajesh_dai);

        recyclerView = findViewById(R.id.rv_image);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);

        recyclerView.setLayoutManager(layoutManager);

        SnapHelper snapHelper = new PagerSnapHelper();
        snapHelper.attachToRecyclerView(recyclerView);

        List<Word> wordList = new ArrayList<>();

        wordList.add(new Word(R.drawable.oq, null));
        wordList.add(new Word(R.drawable.oqqqq, null));
        wordList.add(new Word(R.drawable.opt3, null));



        RajeshDaiAdapter adapter = new RajeshDaiAdapter(this);
        recyclerView.setAdapter(adapter);
        adapter.addItems(wordList);

    }
    @Override
    public void onNoteClick(int position) {
    }
}

1 个答案:

答案 0 :(得分:0)

将此添加到您的主班

int CurrenePosition = ((LinearLayoutManager)recyclerView.getLayoutManager()).findFirstVisibleItemPosition();
NextItemImageView.setOnClickListener(new View.OnClickListener() {
    @Override
        public void onClick(View v) {
            if (CurrenePosition < recyclerView.getAdapter().getItemCount()-1)
            recyclerView.scrollToPosition(CurrenePosition + 1);
        }
    });
PreviousItemImageView.setOnClickListener(new View.OnClickListener() {
    @Override
        public void onClick(View v) {
            if (CurrenePosition > 0)
            recyclerView.scrollToPosition(CurrenePosition - 1);
        }
    });