English 中文(简体)
观点分类系统(第1页,真实)确实是NOT的顺利发展。
原标题:ViewPager setCurrentItem(pageId, true) does NOT smoothscroll

我正在汇编SDK 4.03、三星英凡二字、二等支持书目4的图书馆,并用我手提的“观点”进行实际血清工作罚款。

viewPager.setCurrentItem(id); // , or
viewPager.setCurrentItem(id, true);  

它并非顺利发展,而是立即交换看法。 虽然文件明确指出,这是确定第二个论点的目的。 如何做到这一点?

问题回答

我之所以确定这一点,是因为我创造了一个超越“观点”的神秘感。 页: 1 采用思考方法。

public class MyViewPager extends ViewPager {

    public MyViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        setMyScroller();
    }

    private void setMyScroller() {
        try {
            Class<?> viewpager = ViewPager.class;
            Field scroller = viewpager.getDeclaredField("mScroller");
            scroller.setAccessible(true);
            scroller.set(this, new MyScroller(getContext()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public class MyScroller extends Scroller {
        public MyScroller(Context context) {
            super(context, new DecelerateInterpolator());
        }

        @Override
        public void startScroll(int startX, int startY, int dx, int dy, int duration) {
            super.startScroll(startX, startY, dx, dy, 1000 /*1 secs*/);
        }
    }
}

我叫setCurrentItem,在手里工作,对我课以罚款。

new Handler().post(new Runnable() {
        @Override
        public void run() {
            myViewPager.setCurrentItem(1, true);
        }
    });

希望这一帮助。

这就是我的做法。 我在<编码>ViewPager上通过将我自己的习俗分级列入同一套中,否决了包装-私人方法。 其价值为零,造成打断行为,而不是平滑。

package android.support.v4.view;

import android.content.Context;
import android.util.AttributeSet;

public class MyViewPager extends ViewPager {

    public MyViewPager(Context context) {
        super(context);
    }

    public MyViewPager(Context context, AttributeSet attr) {
        super(context, attr);
    }

    void smoothScrollTo(int x, int y, int velocity) {
        super.smoothScrollTo(x, y, 1);
    }
}

如果你能够计算和提供仅1个组织的实际速度,它就发挥了巨大作用。

我知道这一read已久,但这是谷歌最高成果之一。 我已回过头来,谈到现在如何解决这一问题。 上述解决办法根本无助于我。 然而,我确实找到了一个对我有利的解决办法。

我目前的想法是,在一位浏览器内有一个清单。 当你点击其中一种观点时,就会给它带来新的一页和滚动。 在此之前,情况非常令人不安,但似乎正好如此,因为我呼吁我这样做。

mViewPager.setCurrentItem(intIndex, true);

摘自我的OnClickEvent。 出于某种原因,浏览器不喜欢这样做,因此,我行使了这一职能。 它创造了一条新的天线,可以 run在天线上。 这一可操作性是指将视力帕格推向某个项目。

public static void scrollToIndex(int index) {

    final int intIndex = index;

    //We re going to make another thread to separate ourselves from whatever
    //thread we are in 
    Thread sepThread = new Thread(new Runnable(){

        public void run()
        {
            //Then, inside that thread, run a runnable on the ui thread.
            //MyActivity.getContext() is a static function that returns the 
            //context of the activity. It s useful in a pinch.
            ((Activity)MyActivity.getContext()).runOnUiThread(new Runnable(){

                @Override
                public void run() {

                    if (mSlidingTabLayout != null)
                    {
                        //I m using a tabstrip with this as well, make sure
                        //the tabstrip is updated, or else it won t scroll
                        //correctly
                        if(mSlidingTabLayout.getTabStripChildCount() <= intIndex)
                        mSlidingTabLayout.updateTabStrip();

                        //Inside this thread is where you call setCurrentItem
                        mViewPager.setCurrentItem(intIndex, true);

                    }

                }

            });

         }
    });


    sepThread.start();


}

我希望我至少帮助过这个问题的人。 亲爱!

Edit: 看看我的答案,我很相信,你刚刚能够站在ui子上,应该工作。 尽管如此,用食盐进行检测,但我已经进行过检测。

我之所以这样说,是因为我对上述解决办法完全满意。

The class overrides setCurrentItem(int item, boolean smoothScroll) and uses reflection to keep this method as original as possible. The key is that velocity is not 0. You only have to replace your current ViewPager with this MyViewPager class and use it like normally:

import android.content.Context;
import android.util.AttributeSet;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import androidx.viewpager.widget.ViewPager;

public class MyViewPager extends ViewPager {

    public MyViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * Set the currently selected page. If the ViewPager has already been through its first
     * layout with its current adapter there will be a smooth animated transition between
     * the current item and the specified item.
     *
     * @param item         Item index to select
     * @param smoothScroll True to smoothly scroll to the new item, false to transition immediately
     */
    @Override
    public void setCurrentItem(int item, boolean smoothScroll) {
        final Class<?> viewpager = ViewPager.class;
        int velocity = 1;

        try {
            Field mPopulatePending = viewpager.getDeclaredField("mPopulatePending");
            mPopulatePending.setAccessible(true);
            mPopulatePending.set(this, false);

            Method setCurrentItemInternal = viewpager.getDeclaredMethod("setCurrentItemInternal", int.class, boolean.class, boolean.class, int.class);
            setCurrentItemInternal.setAccessible(true);
            setCurrentItemInternal.invoke(this, item, smoothScroll, false, velocity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

正如其他人后来发现的那样,这是一件大事,我们必须将其延迟,但可以非常简单:

pager.post {
    pager.setCurrentItem(1, true)
}

令人痛心的是,你必须首先尝试错误的答案,才能做到这一点。

The ViewPager was changed a lot from revision 5 to 9. Some of those changes that might be related to your problem:

  • Bug fixes for user interface behavior in ViewPager (revision 5 and 6)
  • Fixed numerous bugs for ViewPager (revision 9)

我最精gues的猜测是,你的支助图书馆不是最新的。 修缮图书馆

I had the same problem, but today I ve found a simple solution. Maybe it will help you. First, lets suppose we have a ViewPager that filled the whole screen. To switch between pages I ve created my own custom View with tabs and put it over the ViewPager. Clicking a tab should scroll smoothly to the appropriate page in ViewPager with setCurrentItem(item, true) - but it scrolls instantly, with no smooth! Then I tried to add a simple button (not custom) over the ViewPager + callback:

@Override
public void onClick(View v) {
   viewPager.setCurrentItem(item, true);
}

之后,平稳的roll升工作。 因此,解决办法非常简单:在Button集团内部,Touch(......)的内装饰物的收复是真实的,因此它总是会触动事件! 接着,当我把“回落的谎言”改为“回到真实的”时,我开始为我的习惯提出看法。

我希望我的成功故事能够帮助你。

对于使用Xamarin的人(尽管这种做法也适用于 Java)我可以建议采用基于上述答案的下一个办法(从支持图书馆收到的Pager诉7 AppCompat 19.1.0号):

public void ScrollSmooth(int i)
{
    var jClass = JNIEnv.GetObjectClass(_pager.Handle);
    var jMethod = JNIEnv.GetMethodID(jClass, "setCurrentItemInternal", "(IZZI)V");
    JNIEnv.CallVoidMethod (_pager.Handle, jMethod, new JValue (i), new JValue (true), new JValue (false), new JValue (1));
}

它依靠从

Note this variable mFirstLayout - it will be set true while viewpager callback onAttachedToWindow(such as on recyclerview),so will be not smoothScroll. You should override onAttachedToWindow to control the mFirstLayout variable. Something like this:

        @Override
        protected void onAttachedToWindow() {
            super.onAttachedToWindow();
            try {
                Field mFirstLayout = ViewPager.class.getDeclaredField("mFirstLayout");
                mFirstLayout.setAccessible(true);
                mFirstLayout.set(this, false);
                getAdapter().notifyDataSetChanged();
                setCurrentItem(getCurrentItem());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

下面的代码将暂停该页,然后向下lide。

import androidx.viewpager.widget.ViewPager;


new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            viewPager.setCurrentItem(index, true);
        }
    }, 500);

这一解决办法对:

参看Page Limit(3); 根据你页数调整这一价值。 例如 你有4页,你可以使用4页而不是3页。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...