gpt4 book ai didi

java - 如何禁用此特定 View 页面中的滑动功能?

转载 作者:行者123 更新时间:2023-12-02 01:26:37 25 4
gpt4 key购买 nike

我对 Java 没有那么丰富的经验,所以我自己无法弄清楚。我买了一个带有标签布局的模板,如下所示(图 1):

This is how my Template looks like

我想禁用这两个选项卡之间的滑动功能,因此只能通过单击选项卡“TAB 2”来访问“TAB 2”。

互联网上有很多教程,但没有一个适合我的代码,所以我有点困惑。

这是我的 TabFragment:

public class TabFragment extends Fragment {
public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 2 ;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


View x = inflater.inflate(R.layout.tab_layout,null);
tabLayout = (TabLayout) x.findViewById(R.id.tabs);
viewPager = (ViewPager) x.findViewById(R.id.viewpager);

viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));

viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
public void onPageScrollStateChanged(int state) {}
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}



tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});

return x;

}

class MyAdapter extends FragmentPagerAdapter {

public MyAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position)
{

if(position == 0){
return new Tab1();
}
if(position == 1){
return new Tab2();
}



return null;
}

@Override
public int getCount() {

return int_items;

}


@Override
public CharSequence getPageTitle(int position) {

switch (position){
case 0 :
return "Tab 1";
case 1 :
return "Tab 2";

}
return null;
}
}}

和我的 tab_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">


<android.support.design.widget.TabLayout
android:id="@+id/tabs"
app:tabGravity="fill"
app:tabMode="fixed"
android:background="@color/material_blue_grey_800"
app:tabIndicatorColor="@color/orange"
app:tabSelectedTextColor="@color/orange"
app:tabTextColor="@color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.design.widget.TabLayout>

<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="50dp">

</android.support.v4.view.ViewPager>



</LinearLayout>

我尽力想办法解决这个问题,但没有成功,所以我希望你们中的一个人可以帮助我解决这个小问题:)

非常感谢。

最佳答案

您需要创建一个自定义ViewPager来拦截onTouch事件并避免默认行为。这样用户将无法在页面之间滑动,但可以触摸选项卡。

您可以查看已回答的链接:How do disable paging by swiping with finger in ViewPager but still be able to swipe programmatically?

编辑

我将通过步骤进行解释:

  1. 创建一个名为 NonSwipeableViewPager 的新类
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.animation.DecelerateInterpolator;
import android.widget.Scroller;
import java.lang.reflect.Field;

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*/);
}
}
}
  • 修改您的布局以使用新的 ViewPager
  • <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="wrap_content">


    <android.support.design.widget.TabLayout
    android:id="@+id/tabs"
    app:tabGravity="fill"
    app:tabMode="fixed"
    android:background="@color/material_blue_grey_800"
    app:tabIndicatorColor="@color/orange"
    app:tabSelectedTextColor="@color/orange"
    app:tabTextColor="@color/white"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    </android.support.design.widget.TabLayout>

    <NonSwipeableViewPager <!-- You will need to import this correctly with your class package -->
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="50dp">

    </NonSwipeableViewPager>



    </LinearLayout>
  • 使用新的 ViewPager 更新您的 Activity
  • public class TabFragment extends Fragment {
    public static TabLayout tabLayout;
    public static NonSwipeableViewPager viewPager; // This line changed
    public static int int_items = 2 ;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


    View x = inflater.inflate(R.layout.tab_layout,null);
    tabLayout = (TabLayout) x.findViewById(R.id.tabs);
    viewPager = (NonSwipeableViewPager) x.findViewById(R.id.viewpager); // This line changed

    viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));

    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    public void onPageScrollStateChanged(int state) {}
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}



    tabLayout.post(new Runnable() {
    @Override
    public void run() {
    tabLayout.setupWithViewPager(viewPager);
    }
    });

    return x;

    }

    您可以保留相同的适配器MyAdapter

    这将避免在选项卡之间滑动,但会保留每个选项卡上的点击。

    关于java - 如何禁用此特定 View 页面中的滑动功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56809474/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com