Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
565 views
in Technique[技术] by (71.8m points)

circular auto scrolling horizontalscrollview android

I want to auto scroll an horizontal scrollview which contains a lot of imageview inside a linearlayout when it reached the last imageview the first one appear and so on

I have found this code which have match my needs but when I have added the auto scrolling the circular feature won't work : when the last imageview is reached the first one won't appear

here is my code :

slider = (RelativeLayout)findViewById(R.id.slider);
    RelativeLayout container = (RelativeLayout) findViewById(R.id.slider);
    scrollView = new PSInfiniteScrollView(this,new PSSize(120,120));
    for (int i = 0; i < 10; i++) {
        MyCloneableView img = new MyCloneableView(this);
        img.setId(i + 20);
        img.setImageResource(R.drawable.ic_launcher);
        img.setScaleType(ImageView.ScaleType.FIT_XY);
        img.setBackgroundColor(c[i]);
        img.setTag(c[i]);
        scrollView.addItem(img);
    }

    container.addView(scrollView);

    Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
            doLoop();
        }
    });

    t.start();

and here doLoop method :

private void doLoop(){
    do {
        scrollView.scrollBy(2, 0);
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        scrollView.scrollBy(2, 0);
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }   while (true);
}   
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I have Achieved a similar requirement by using an RecyclerView with horizontal linear layout manager here are the code snippets which may help you

  1. To make the Recycler View appear horizontal use a LinearLayout manager and set its orientation to horizontal

    mLinearLayoutManager = new LinearLayoutManager(context);
    mLinearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
    mRecyclerView.setLayoutManager(mLinearLayoutManager);
    
  2. To make It circular: In your Recycler View adapter make the getItemCount to return a very large value like Integer.MAX_VALUE.

    @Override
    public int getItemCount() {
        return Integer.MAX_VALUE;
    }
    

To get the actual item inside your adapter you can use simple modulus operation like this

@Override
public void onBindViewHolder(ActionItemViewHolder holder, int position) {
    position = position % ACTUAL_SIZE_OF_LIST;
};

Here is the trick to makes it circular!

Make your RecyclerView to scroll to an index which is half of the value returned by your adapters getItemCount,You can achieve this by the below code:

mLinearLayoutManager.scrollToPosition(Integer.MAX_VALUE / 2);

(this is a simple hack)

I didn't understand what exactly you meant by auto Scrolling ,if you want the items to scroll to center of the screen on clicking it here is the code

private void scrollToCenter(View v) {
    int itemToScroll = mRecyclerView.getChildPosition(v);
    int centerOfScreen = mRecyclerView.getWidth() / 2 - v.getWidth() / 2;
    mLayoutManager.scrollToPositionWithOffset(itemToScroll, centerOfScreen);
}

Hope this helps you!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...