gpt4 book ai didi

android - 使用底部导航 View 的 viewpager 不会滑动 fragment

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:22:27 26 4
gpt4 key购买 nike

我想滑动底部给出的标签。为此,我通过 this 使用了使用 BottomNavigationView 的 ViewPager .我的代码如下。

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/navigation"
android:layout_alignParentTop="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />

<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:menu="@menu/bottom_navigation_items"
android:background="@color/colorPrimary"
app:itemIconTint="@color/colorAccent"
app:itemTextColor="@android:color/white"></android.support.design.widget.BottomNavigationView>
</RelativeLayout>

Java代码是

    public class MainActivity extends AppCompatActivity {
BottomNavigationView bottomNavigationView;
ViewPager viewPager;
MenuItem prevMenuItem;
FragmentWallet fragmentWallet;
FragmentNavigationDrawer fragmentNavigationDrawer;
FragmentHome fragmentHome;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bottomNavigationView = (BottomNavigationView) findViewById(R.id.navigation);
viewPager = (ViewPager) findViewById(R.id.viewpager);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {

switch (item.getItemId()) {
case R.id.action_menu_home:
viewPager.setCurrentItem(0);
break;
case R.id.action_menu_wallet:
viewPager.setCurrentItem(1);
break;
case R.id.action_menu_navigation_d:
viewPager.setCurrentItem(2);
break;
}

return false;
}
});
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

@Override
public void onPageSelected(int position) {
if (prevMenuItem != null) {
prevMenuItem.setChecked(false);
} else {
bottomNavigationView.getMenu().getItem(0).setChecked(false);
}
Log.d("page", "onPageSelected: " + position);
bottomNavigationView.getMenu().getItem(position).setChecked(true);
prevMenuItem = bottomNavigationView.getMenu().getItem(position);

}


@Override
public void onPageScrollStateChanged(int state) {

}
});
viewPager.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
setupViewPager(viewPager);

}

private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
fragmentHome = new FragmentHome(); // this should be in `FragmentPagerAdapter.getItem()`
fragmentWallet = new FragmentWallet(); // this should be in `FragmentPagerAdapter.getItem()`
fragmentNavigationDrawer = new FragmentNavigationDrawer(); // this should be in `FragmentPagerAdapter.getItem()`

viewPagerAdapter.addFragment(fragmentHome); // this can cause crashes
viewPagerAdapter.addFragment(fragmentWallet); // this can cause crashes
viewPagerAdapter.addFragment(fragmentNavigationDrawer); // this can cause crashes
viewPager.setAdapter(viewPagerAdapter);
}
}

View Pager Adapter代码是

 public class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>(); // this line can cause crashes

public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}

@Override
public int getCount() {
return mFragmentList.size();
}

public void addFragment(Fragment fragment) {
mFragmentList.add(fragment); // this line can cause crashes
}
}

它适用于从一个 fragment 导航到另一个 fragment ,但不能滑动 fragment 。请帮助,或者我是否应该切换其他解决方案。

最佳答案

删除这部分:

viewPager.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});

关于android - 使用底部导航 View 的 viewpager 不会滑动 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47714606/

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