我创建了一个控制四个片段的视图寻呼机。但是我想禁用手指滑动。我想锁定滑动并使用按钮单击导航到下一个片段或上一个片段。但是我从研究中得到的所有试用代码都被证明是错误的。请如何完全禁用滑动并引入一个按钮以使我能够导航到
这是 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类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);
}
}
}
答案 0 :(得分:0)
您可以使用https://stackoverflow.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"/>
最后: 在课堂上,您使用的是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"/>
然后通过以下方式设置类别: 无论您以何种方式编写代码,都只需 viewPager2.setUserInputEnabled(false);
禁用滑动效果 >
ViewPager2 viewPager2 = findViewById(R.id.viewPager);
viewPager2.setAdapter(new SimpleFragmentPagerAdapter(this));
viewPager2.setUserInputEnabled(false);
祝你好运!