如何禁用 ViewPager 滑动并使用按钮单击启用手动滑动

How to disable ViewPager from swiping and enable manual swipe using button click

我创建了一个控制四个片段的视图寻呼机。但我想禁用手指滑动。我想锁定滑动并使用按钮单击导航到下一个片段或上一个片段。但是我从研究中获得的所有试用代码都被证明是错误的。请问如何完全禁用滑动并引入一个按钮使我能够导航到

这是 SimpleFragmentPageAdapter

public class SimpleFragmentPageAdapter extends FragmentPagerAdapter {
    private String[] tabtitle=new String[]{"Page1","Page2","Page3","Page4",};
    Context context;
    private int pagecount=4;
    public SimpleFragmentPageAdapter(FragmentManager fm, Context context) {
        super(fm);
        this.context=context;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0: //Fragment # 0 - This will show DevelopersFragment
                return Page1Fragment.newInstance(0, "page # 0");
            case 1: //Fragment # 0 - This will show Page1Fragment
                return Page2Fragment.newInstance(1, "Page # 1");
            case 2: //Fragment # 0 - This will show Page2Fragment
                return Page3Fragment.newInstance(2, "Page # 2");
            case 3: //Fragment # 0 - This will show Page3Fragment
                return Page4Fragment.newInstance(3, "Page # 3");
            default:
                return null;
        }
    }

    @Override
    public int getCount() {
        return pagecount;
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return "woliwo" + position;
    }
}

这是Main1Activity.java

public class Main1Activity extends AppCompatActivity {

    Toolbar toolbar;
    TabLayout tabLayout;
    ViewPager viewPager;
    Button Tips3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the start page passed to us or default to first page
        Intent mIntent = getIntent();
        int startPage = mIntent.getIntExtra("startPage", 0);
        setContentView(R.layout.activity_main1);

        toolbar = findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);


        ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
        viewPager.setAdapter(new SimpleFragmentPageAdapter(getSupportFragmentManager(), this));
        // set the current page to the start page
        viewPager.setCurrentItem(startPage);


        //Attach the page change listener inside the activity
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            // This method will be invoked when a new page becomes selected
            @Override
            public void onPageSelected(int position) {
                Toast.makeText(Main1Activity.this, "bool:" + position, Toast.LENGTH_SHORT).show();
            }

            // This method will be invoked when the current page is scrolled
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetpixels) {

            }

            // Called when the scroll state changes:
            //SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING, SCROLL_STATE_SETTLING
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


        tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        tabLayout.setupWithViewPager(viewPager);


    }
    
}

这是我试过的,它给了 1% 的运动事件残疾

final View touchView = findViewById(R.id.view_pager);
        touchView.setOnTouchListener(new View.OnTouchListener()
        {
            @Override
            public boolean onTouch(View v, MotionEvent event)
            {
                return true;
            }
        });

这是 ViewPager class Main1Activity.java 下面是我做的,它什么也没显示,请纠正我

public class Main1Activity extends AppCompatActivity {

    Toolbar toolbar;
    TabLayout tabLayout;
    ViewPager viewPager;
    Button Tips3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main1);

        toolbar = findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);


        CustomViewPager viewPager = findViewById(R.id.view_pager);
        viewPager.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
            }
        });


        tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        tabLayout.setupWithViewPager(viewPager);


    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.tool_item_menu1, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){

            case R.id.item0:
                Toast.makeText(this, "Home Selected", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(Main1Activity.this, MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                finish();
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }

    }



}

您可以使用 https://whosebug.com/users/7725103/rajasekhar 上面评论中提到的 ViewPager2,但是如果您确实需要使用 viewpager:那么您将必须像这样创建一个自定义 viewpager:

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

import androidx.viewpager.widget.ViewPager;

public class CustomViewPager extends ViewPager {

    private boolean enabled;

    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.enabled = true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (this.enabled) {
            return super.onTouchEvent(event);
        }

        return false;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        if (this.enabled) {
            return super.onInterceptTouchEvent(event);
        }

        return false;
    }

    public void setPagingEnabled(boolean enabled) {
        this.enabled = enabled;
    }
}

用法

 <com.yourpackagename.CustomViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/tab_layout"/>

最后: 在 class 您正在使用 viewpager:

 CustomViewPager viewPager=findViewById(R.id.view_pager);
     viewPager.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    return false;
                }
            });

此解决方案适用于使用 ViewPager2 的用户

这样设置布局:

<androidx.viewpager2.widget.ViewPager2
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/tab_layout"/>

然后设置 class 这样: 不管你的编码方式如何,只要放置这个 viewPager2.setUserInputEnabled(false); 禁用滑动效果

ViewPager2 viewPager2 = findViewById(R.id.viewPager);
        viewPager2.setAdapter(new SimpleFragmentPagerAdapter(this));
        viewPager2.setUserInputEnabled(false);

祝你好运!