可滚动的 TabWidget 或 TabLayout

Scrollable TabWidget or TabLayout(可滚动的 TabWidget 或 TabLayout)
本文介绍了可滚动的 TabWidget 或 TabLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

这是我得到的

我得到的类是找不到的.我实际上是在尝试像 PlayStore 一样使用 TabWidget.

我想要那个 gif 那样的 TabWidget.怎么做?或者,有没有其他方法可以做到?

我试过了

但是,上面的源代码没有给出ScrollableWidget.

我还在 youtube 中找到了一个演示.他给了源码链接http://www.mediafire.com/download/7c1kd878hsvlvzr/tabscroll.rar .但是,我无法访问该链接.

解决方案

创建一个名为 NonSwipeableViewPager 的视图

公共类 NonSwipeableViewPager 扩展 ViewPager {公共 NonSwipeableViewPager(上下文上下文){超级(上下文);setMyScroller();}public NonSwipeableViewPager(上下文上下文,AttributeSet attrs){超级(上下文,属性);setMyScroller();}@覆盖公共布尔 onInterceptTouchEvent(MotionEvent 事件){//不允许滑动切换页面返回假;}@覆盖公共布尔 onTouchEvent(MotionEvent 事件){//不允许滑动切换页面返回假;}//向下添加一个以平滑滚动私人无效 setMyScroller() {尝试 {类<?>viewpager = ViewPager.class;Field scroller = viewpager.getDeclaredField("mScroller");scroller.setAccessible(true);scroller.set(this, new MyScroller(getContext()));} 捕捉(异常 e){e.printStackTrace();}}公共类 MyScroller 扩展 Scroller {公共 MyScroller(上下文上下文){超级(上下文,新的DecelerateInterpolator());}@覆盖公共无效startScroll(int startX,int startY,int dx,int dy,int持续时间){super.startScroll(startX, startY, dx, dy, 350/*1 secs*/);}}}

这里是 git repo

Here is a library I got.

I had added

implementation 'ru.noties:scrollable:1.3.0'

to my build.gradle. But, when I implement TabLayout of ru.noties:scrollable

<ru.noties.scrollable.sample.TabsLayout
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="@dimen/tabs_height"
    android:background="@color/md_teal_500"/>

I am getting the class isn't found. I am actually trying to use TabWidget like PlayStore.

I want TabWidget like that gif. How to do that? Or, is there any alternative way to do it?

I tried

<TabHost
    android:id="@+id/tab_host"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/rl_">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@android:id/tabs">

            <LinearLayout
                android:id="@+id/Filters"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <include
                    layout="@layout/filters_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/Adjustments"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <include
                    layout="@layout/adjustment_layout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
            </LinearLayout>

        </FrameLayout>

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="#40cccccc" />

    </RelativeLayout>
</TabHost>

But, above source code don't gives ScrollableWidget.

I found an demo in youtube also. He gave source code link http://www.mediafire.com/download/7c1kd878hsvlvzr/tabscroll.rar . But, I am unable to visit that link.

解决方案

Create a view called NonSwipeableViewPager

public class NonSwipeableViewPager extends ViewPager {

public NonSwipeableViewPager(Context context) {
    super(context);
    setMyScroller();
}

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

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    // Never allow swiping to switch between pages
    return false;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    // Never allow swiping to switch between pages
    return false;
}

//down one is added for smooth scrolling

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, 350 /*1 secs*/);
    }
}
}

Here's the git repo

这篇关于可滚动的 TabWidget 或 TabLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)