P
P
prosto_anton2016-03-23 20:36:42
Android
prosto_anton, 2016-03-23 20:36:42

How to do horizontal scrolling in android?

I'm looking for a way to make cyclic horizontal scrolling in android, preferably using standard platform tools for API version no higher than 15.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2016-03-23
@prosto_anton

As a fairly simple option:
1) Take RecyclerView, make it horizontal:
2) Loop it by making a custom adapter:

class CustomAdapter extends RecyclerView.Adapter<Holder>{
        private List<Item> items;
...
        @Override
        public void onBindViewHolder(Holder holder, int position) {
            int actualPosition = position % items.size(); //Используем вместо position
        }

        @Override
        public int getItemCount() {
            return Integer.MAX_VALUE; //важно, обеспечивает "бесконечность" скролла
        }
...
}

And, additionally, when initializing
recyclerView.setAdapter(new CustomAdapter());
recyclerView.scrollToPosition(Integer.MAX_VALUE / 2); // тут подбираем число, чтобы нужный контент был на экране, я взял просто посередине.
You need to scroll so that the scroll is "infinite" in both directions.
In fact, the scroll here is not honestly endless, but quite finite. But it is unlikely that a user will scroll through a billion with a fig of items in one direction or another. Such cases, I personally came up with a hack, can be used.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question