English 中文(简体)
图像显示在滚动和机器人内
原标题:visual indication of over scroll in android

我试图添加一些视觉提示,即“ViewPager”中不再有希望方向的页面。 然而,我正在努力找到一个地方,把相关的代码放在哪里。

我尝试过以以下代码扩展 ViewPager 类, 但 Toast 并不显示( ev.get Orientation () return always 0) 。 我也尝试过历史点, 但 ev.get HistorySize () return also 0 。

我错过了什么?

类例 :

public class CustomViewPager extends ViewPager {

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

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

    /**
     * @see android.support.v4.view.ViewPager#onTouchEvent(android.view.MotionEvent)
     */
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        boolean result = super.onTouchEvent(ev);

        switch (ev.getAction() & MotionEventCompat.ACTION_MASK) {
            case MotionEvent.ACTION_MOVE:
                if (ev.getOrientation() > 0) {
                    Toast.makeText(getContext(), "left", 0).show();
                }
        }

        return result;
    }
}
问题回答

如果您查看 v4 支持库, 您可以看到 ViaPager 使用的名为 < a href=" http:// developmenter.android.com/ reference/android/ support/ v4/ widget/ EdgeEffectCompat.html " > EdgeEffectCompat < /a > 的类 (这提供当你到达ICS+ 中一个查看传呼器的开始或结束时的光效应) 。 如果您看到在 compat 库中执行, 您可以看到它有一个如果声明来查看构建版本是否为 14 + (ICS) 。 如果是这样的话, 它最终会( 如果你追踪得足够长) 使用普通的 < a href=" http:// developer.and.and. com/ reference/androdget/ EdgeEffect.html > EdgeEfect

如果您想要的话, 您可以自制使用自己的边緣的自定义 ViewPager 。 您可以查看您的自定义的自定义 ViewPager 。 您可以查看您的自制源代码 。 您可以查看该源代码和机器人源代码, 查看它们是如何实施的 : https:// github.com/ android/platfect < a href=" https://github. org/android/platf_fect_ a href=" https:// githhub. com/android/plateff_fect < a hreffs_ base/ blob/master/ court/res/ res/ drowable- xhdpi/ overscroll_ dege. png" > > > > overscroll_ sedge 和 < href=" "https://githrubrub.com/ blob/ glob/ restraws_ defalb_ destalbs_ probalbs/ askall/ astautalbs.

祝你好运

(顺便说一下,他们是如何在 ICS 上的发射器菜单中 创造出 凉爽的外观边缘倾斜效应的... 这样你就可以有创造性了

我试图取得与这个问题同样的效果。 我努力争取它, 然后我读了@wnafee 回答(我无法用它来做) 。

But then I struggle to implement what was sound pretty simple from the answer. I had so much trouble with implementing it, that I might didn t understand the answer correctly, but there were too many issues of inaccessible APIs since I wasn t working in the same package of the Compatibility library.

在我尝试了一些方法(其中没有一个成功,而且相当复杂)之后,我走到了一个略微不同的方向,现在它像一个符咒一样运作。 我用了一些反思,对于从未使用过它的人来说,不要担心它确实是反省的基础。

我不确定这是不是最好的解决方案,但它对我有效,所以如果你愿意使用它,欢迎你。请阅读Wnafee的例子,因为它解释了我所做的一些事情。

为了完成这项任务,你应该遵循我的三部分解决方案。 (3至10分钟之间)

<强 > 第一部分:

正如Wnafee所说,我刚刚通过拷贝将的源代码贴上,

(just make sure to copy the overscroll_edge and overscroll_glow drawables in the AOSP /res/drawable directories to your own project since they are internal to android)

我只做了两个很小的改变:

  1. I declare that the class extends EdgeEffectCompat (I called my class EdgeEffectForEarlyVersions). public class EdgeEffectForEarlyVersions extends EdgeEffectCompat. The reason for doing this change is that the mLeftEdge and mRightEdge are of the type EdgeEffectCompat.
  2. At the first line of the constructor of "my" new class I added a call to the parent constructor super(context);. Since there is no default constructor to EdgeEffectCompat you have to Explicitly call the constructor.

<强 > 第二部分

此外,我还写了另一个函数。该函数的目的是,如果有一个早期版本(在ICS之前),我们希望使用我们刚刚复制的 < code> EdgeEffectForEarlyVersions 。为了达到这个目的,我使用了反省。

这就是这个函数:

private static void changeEdgeEffectCompactOnEarlyVersions(ViewPager viewPager, Context context)
{
    /* In case that the version is earlier than 14 there is only empty implementation for the edge effect, therefore we change it.
     * for more information look on the following links:
     * 1. http://stackoverflow.com/questions/10773565/visual-indication-of-over-scroll-in-android
     * 2. http://grepcode.com/file/repo1.maven.org/maven2/com.google.android/support-v4/r7/android/support/v4/view/ViewPager.java#ViewPager.0mLeftEdge
     * 3. http://grepcode.com/file/repo1.maven.org/maven2/com.google.android/support-v4/r7/android/support/v4/widget/EdgeEffectCompat.java#EdgeEffectCompat
     */

    if (Build.VERSION.SDK_INT < 14)
    {
        try
        {
            Class<ViewPager> viewPagerClass = ViewPager.class;
            //Get the left edge field, since it is private we used getDeclaredField and not getDeclared 
            Field leftEdge = viewPagerClass.getDeclaredField("mLeftEdge");
            leftEdge.setAccessible(true);
            //Get the right edge field, since it is private we used getDeclaredField and not getDeclared
            Field rightEdge = viewPagerClass.getDeclaredField("mRightEdge");
            rightEdge.setAccessible(true);

            EdgeEffectForEarlyVersions leftEdgeEffect = new EdgeEffectForEarlyVersions(context);
            EdgeEffectForEarlyVersions rightEdgeEffect = new EdgeEffectForEarlyVersions(context);

            //Set the mLeftEdge memeber of viewPager not to be the default one, but to be "our" edgeEffect  
            leftEdge.set(viewPager, leftEdgeEffect);
            //Set the mRightEdge memeber of viewPager not to be the default one, but to be "our" edgeEffect
            rightEdge.set(viewPager, rightEdgeEffect);              
        }
        catch (Exception ex)
        {
            Log.e("refelection", ex.getMessage());
        }

    }
}

<强 > 第三部分

现在只有剩下的事可做, 就是在您有了 ViewPager Incent( ViewPager Incentral) 之后调用该函数, 仅此而已 。

我希望它能帮助别人

您有很多选项, 您可以显示 Toast, 显示对话框, 制作文本View 或图像在 UI 上出现, 等等 。 或者因为您知道 ViewPager 中的 View 项数量, 您可以在位置 0 和/ 或 n+ 1 上添加不同的视图, 并将信件添加到包含您数据的最后一个视图中 。

您可以执行:

viewPager.setOnPageChangeListener(new OnPageChangeListener() {
    public void onPageSelected(int position) {
        //TODO If position is the 0 or n item, add a view at 0 or at n+1 to indicate there is no more pages with data.
    }

    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        // TODO Show a Toast, View or do anything you want when position = your first/last  item;
    }

    public void onPageScrollStateChanged(int state) {
    }
});

@goBeepitit dev 回答, 以补充@goBeepit dev 回答, 当您创建了自己的边缘效果类, 从 EdgeEfffectCompat 扩展到 EdgeEffectCompat, 有些方法必须是布尔。 您可以将这些方法改变为布林类型, 然后在任何情况下返回真实, 这样一切就都正常了 。

您可以在碎片中超载设定用户可视 Hint( bolean) 函数。 Pseudo 代码 :

    void setUserVisibleHint(boolean isVisibleToUser) {
        // If this fragment is becoming visible
        if (isVisibleToUser == true) {
             // Check if it is the last fragment in the viewpager
             if (indexOfThis == getActivity().indexOfLast) {
                // Display right limit reached
                Toast(..., "No more Frags to right",...)
                }
             // Check if it is the first fragment in the viewpager
             else if (indexOfThis == getActivity().indexOfFirst) {
                // Display Left Limit reached
                Toast(..., "No more Frags to left",...)
             }
        }
    }

我并没有将这个函数用于此目的,但出于其他原因使用这个函数,而且它会适当点火。希望这样能帮助...

根据Renard s ViewPager3D:,我实施了反弹效果。

通常使用 ViewPager 通常使用 PagerAdapter , 如 FragmentPagerAdapter Fragment StatePagerAdapter 来将 ViewPager 装入内容( 您的内容将显示 )。

现在,当你使用 PagerAdapter 时,你有一种方法叫做 getCount () <() ,,http://developmenter.android.com/reference/andandroid/support/v4/view/PagerAdapter.html#getCount%28%29 ,它能给您内容的大小。

从现在起,知道您可以轻松显示带有控件语句的信件的大小。

尝试此代码 : < a href=" http:// developmenter.android.com/ reference/android/support/v4/view/ViewPager.html" rel=“ no follow” >http:// developer.android.com/reference/android/support/v4/view/ViewPager.html

注意 : 我不认为您需要自定义的 ViewPager 。 您也需要理解 ViewPager 的碎片 。 请查看 ApiDemos 的样本。 它是一个伟大的来源 。





相关问题
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 ...

热门标签