iT邦幫忙

0

android 指定到某個fragment (已解決)

  • 分享至 

  • xImage

問題一:我目前的問題是 當我按下這個按鍵後 我想要讓他跑到我的tab3 而不是預設的tab1請問我要怎麼改
情境:我從我的tab3.Fragment進到shop_thing_cart.class後在shop_thing_cart.class跟改數量

private View.OnClickListener btn_update = new View.OnClickListener() {
@Override
public void onClick(View view) {
String[] et0 = new String[] {show_thing8,show_thing1,show_thing2,show_thing3,show_thing4,show_thing5,show_thing6,number.getText().toString(),show_thing9};
Upphp_shop.updataing(et0,cookieStr,url);
Intent intent = new Intent(shop_thing_cart.this, home.class);
intent.putExtra("user_id", id_user);
intent.putExtra("user_phone", id_phone);
intent.putExtra("cookie", cookieStr);
intent.putExtra("page", 3);
startActivity(intent);
finish();
}
};

我想要我這邊finish完後回來看我的tab3.Fragment上面的資料是我剛剛跟改的

home

public class home extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        initView();
    }
    private BottomNavigationView mBv;
    private ViewPager mVp;
    private String id_user,id_phone,cookieStr,pag;
    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");
        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);
        //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) {
        BottomAdapter adapter = new BottomAdapter(getSupportFragmentManager());
        adapter.addFragment(new tab1(id_user,id_phone,cookieStr));
        adapter.addFragment(new tab2());
        adapter.addFragment(new tab3(id_user,id_phone,cookieStr));
        adapter.addFragment(new tab4(id_user,id_phone,cookieStr));
        viewPager.setAdapter(adapter);
    }
}
解決方式
出發地:
Intent intent = new Intent(getContext(), home.class);
intent.putExtra("id", 3);
startActivity(intent);
目的地:
onCreate底下

int id = getIntent().getIntExtra("id", 0);
if (id == 3) {
mVp.setCurrentItem(2); //你要到的頁面
}
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答