iT邦幫忙

0

BottomNavigationView+ViewPager 關閉左右滑動

  • 分享至 

  • xImage

我相信大部分的大大因該都有用過
我目前的問題是我明明關閉了左右滑動 但她會變得很奇怪硬往旁邊拉真的可以把其他畫面給拉出來 我很好奇我哪裡有錯?
最右邊那個LED是我tab2的畫面 其他畫面都有相同狀況 只要硬往旁邊滑都會出現
https://ithelp.ithome.com.tw/upload/images/20200313/20109685qPHFGtU6SK.png
我嘗試從run來看錯誤 可是他並沒有任何變化!

mVp = (ViewPager) findViewById(R.id.vp);

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

這是我的xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:background="#FFF"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".home">


    <androidx.viewpager.widget.ViewPager
        android:id="@+id/vp"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1" />


    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bv"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_gravity="bottom"
        android:background="#e4e4e4"
        app:menu="@menu/navigation" />
    <!--app:itemIconTint="@drawable/bottom_navigation_selector"-->
    <!--app:itemTextColor="@drawable/bottom_navigation_selector"-->

</LinearLayout>

這是我的完整程式碼

public class home extends AppCompatActivity {


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

        int id = getIntent().getIntExtra("id", 0);
        if (id == 4) {
            mVp.setCurrentItem(3);
        }
        if (id == 3) {
            mVp.setCurrentItem(2);
        }
        if (id == 2) {
            mVp.setCurrentItem(1);
        }
    }
    private BottomNavigationView mBv;
    private ViewPager mVp;
    private String id_user,id_phone,cookieStr,Qr_code_use;
    private int pag_id;
    private void initView() {
        Intent intent = this.getIntent();
        Bundle bundle=intent.getExtras();
        id_user = bundle.getString("user_id");
        id_phone = bundle.getString("user_phone");
        cookieStr = bundle.getString("cookie");
        Qr_code_use = bundle.getString("Qr_code");
        mBv = (BottomNavigationView) findViewById(R.id.bv);
        mVp = (ViewPager) findViewById(R.id.vp);
        //mBv.getMenu().setGroupCheckable(0, false, false); //動畫

        mBv.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.navigation_home:
                        mVp.setCurrentItem(0);

                        return true;

                    case R.id.navigation_love:
                        mVp.setCurrentItem(1);
                        return true;

                    case R.id.navigation_shop_cart:
                        mVp.setCurrentItem(2);
                        return true;

                    case R.id.navigation_search:
                        mVp.setCurrentItem(3);
                        return true;
                }
                return false;
            }
        });

        //数据填充
        setupViewPager(mVp, id_user,id_phone,cookieStr,Qr_code_use);
        //ViewPager监听
        mVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            @Override
            public void onPageSelected(int position) {
                mBv.getMenu().getItem(position).setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        //停止滑動

        mVp.setOnTouchListener(new View.OnTouchListener() {
                            @Override
                            public boolean onTouch(View v, MotionEvent event) {
                                return true;
                            }
                        });
    }
    private void setupViewPager(ViewPager viewPager, Object id_user, Object id_phone, Object cookieStr, Object Qr_code_use) {
        BottomAdapter adapter = new BottomAdapter(getSupportFragmentManager());
        adapter.addFragment(new tab1(id_user,id_phone,cookieStr));
        adapter.addFragment(new tab2(id_user,id_phone,cookieStr));
        adapter.addFragment(new tab3(id_user,id_phone,cookieStr));
        adapter.addFragment(new tab4(id_user,id_phone,cookieStr,Qr_code_use));
        viewPager.setAdapter(adapter);
    }


    public boolean onKeyDown(int keyCode, KeyEvent event) {//捕捉返回鍵
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            ConfirmExit();//按返回鍵,則執行退出確認
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    public void ConfirmExit(){//退出確認
        AlertDialog.Builder ad=new AlertDialog.Builder(home.this);
        ad.setTitle("離開");
        ad.setMessage("確定要離開此程式嗎?");
        ad.setPositiveButton("是", new DialogInterface.OnClickListener() {//退出按鈕
            public void onClick(DialogInterface dialog, int i) {
                // TODO Auto-generated method stub
                home.this.finish();//關閉activity
            }
        });
        ad.setNegativeButton("否",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int i) {
                //不退出不用執行任何操作
            }
        });
        ad.show();//顯示對話框
    }

}

改寫viewpage即可

public class MyViewPager extends ViewPager {
private boolean isCanScroll = false;

public MyViewPager(Context context) {
super(context);
// TODO Auto-generated constructor stub
}

public MyViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public void setScanScroll(boolean isCanScroll) {
this.isCanScroll = isCanScroll;
}

@Override
public void scrollTo(int x, int y) {
super.scrollTo(x, y);
}

@Override
public boolean onTouchEvent(MotionEvent arg0) {
// TODO Auto-generated method stub
if (isCanScroll) {
return super.onTouchEvent(arg0);
} else {
return false;
}

}

@Override
public void setCurrentItem(int item, boolean smoothScroll) {
// TODO Auto-generated method stub
super.setCurrentItem(item, smoothScroll);
}

@Override
public void setCurrentItem(int item) {
// TODO Auto-generated method stub
super.setCurrentItem(item);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent arg0) {
// TODO Auto-generated method stub
if (isCanScroll) {
return super.onInterceptTouchEvent(arg0);
} else {
return false;
}

}
}
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答