圆形自动滚动horizo​​ntalscrollview android [英] circular auto scrolling horizontalscrollview android

查看:107
本文介绍了圆形自动滚动horizo​​ntalscrollview 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

这是我的代码:

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();

和此处的doLoop方法:

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);
}   

推荐答案

通过将RecyclerView与水平线性布局管理器一起使用,我已经实现了类似的要求 这是一些可以帮助您的代码段

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. 要使回收者视图"显示为水平,请使用LinearLayout管理器并将其方向设置为水平

  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);

  • 使其成为圆形:在Recycler View适配器中,使getItemCount返回一个非常大的值,例如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!

    使您的RecyclerView滚动到一个索引,该索引是适配器getItemCount返回的值的一半,您可以通过以下代码来实现:

    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);
    }
    

    希望这对您有帮助!

    这篇关于圆形自动滚动horizo​​ntalscrollview android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆