你真的很挣这个,所以我在寻找没有运气的解决方案一天后的第一篇文章。
我有一个启动画面,其中有一个带有onClickListener的ImageView,点击它时打开了我的MainActivity就好了:
bookMain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
}
然而,我想要实现的是,当单击按钮时,它会打开我的MainActivity,但是将MainActivity中的fragment_container替换为另一个片段(这是我的底栏导航的工作原理)。
这是MainActivity中的代码,用于在单击导航按钮时处理片段的替换,基本上我希望Splash Screen的点击打开“BookFragment”而不是直接进入“RoomsFragment”:
public class MainActivity extends AppCompatActivity {
//STANDARD ON CREATE OPEN TO LOAD INITIAL LAYOUT
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//THIS CREATES A BLANK CONTAINER FOR EACH FRAGMENT YOU CREATE TO GO IN
if (findViewById(R.id.fragment_container) != null) {
// However, if we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) {
return;
}
// Create a new Fragment to be placed in the activity layout
RoomsFragment firstFragment = new RoomsFragment();
// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, firstFragment).commit();
//THIS CREATES THE NAV BAR AND TELLS WHAT FRAGMENT TO PUT IN THE CONTAINER WHEN A TAB IS SELECTED
BottomBar bottomBar = (BottomBar) findViewById(R.id.bottomBar);
bottomBar.setOnTabSelectListener(new OnTabSelectListener() {
@Override
public void onTabSelected(@IdRes int tabId) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
//TAB1 - THE DEFAULT TAB
if (tabId == R.id.tab_rooms) {
RoomsFragment newFragment = new RoomsFragment();
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
//TAB2
} else if (tabId == R.id.tab_shisha) {
// THIS BIT BELOW IS WHAT CHANGES THE FRAGMENT IN THE CONTAINER
ShishaFragment newFragment = new ShishaFragment();
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
//TAB3
} else if (tabId == R.id.tab_cocktails) {
// THIS BIT BELOW IS WHAT CHANGES THE FRAGMENT IN THE CONTAINER
CocktailsFragment newFragment = new CocktailsFragment();
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
//TAB4
} else if (tabId == R.id.tab_food) {
// THIS BIT BELOW IS WHAT CHANGES THE FRAGMENT IN THE CONTAINER
FoodFragment newFragment = new FoodFragment();
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
//TAB5
} else if (tabId == R.id.tab_book) {
// THIS BIT BELOW IS WHAT CHANGES THE FRAGMENT IN THE CONTAINER
BookFragment newFragment = new BookFragment();
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
});
在onCreate方法中:
//ONCLICK LISTENER FOR THE ENTER BUTTON AND BOOK BUTTON ON SPLASH PAGE
Button enterButton = (Button) findViewById(R.id.enter_button);
enterButton.setOnClickListener(SplashActivity.this);
Button bookButton = (Button) findViewById(R.id.book_button);
bookButton.setOnClickListener(SplashActivity.this);
}
public void onClick(View v) {
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
switch (v.getId()) {
case R.id.enter_button:
startActivity(intent);
break;
case R.id.book_button:
intent.putExtra("source","onClick");
startActivity(intent);
break;
default:
break;
}
}
在onCreate方法中(输入与您相关的bottomBar选项卡的位置):
Intent intent = getIntent();
String source = intent.getStringExtra("source");
if (source != null && source.equals("onClick")) {
bottomBar.selectTabAtPosition(4);
}
祝你好运!
答案 0 :(得分:0)
因为你使用了这个:
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, firstFragment).commit();
它会显示firstFragment
默认
你可以试试这个:
在Spalsh活动中
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
为意图添加一些参数
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
intent.putExtra("source","onClick"); //add like this line
startActivity(intent);
主要活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
```your code
Intent intent = getIntent();
String source = intent.getStringExtra("source");
if (source.equals("onClick")) {
bottomBar.onTabSelected(R.id.tab_book);
}
}
当主要活动接受参数source
并且source
值是您的设置时,它将运行if
中的代码