问题描述
T有两个活动:MainActivity和LoginActivity.我也有一个Fragment,名称是TabFragment(带有TabLayout),其父级是MainActivity.如何从我的登录活动中查看特定的TabFragment.
T have two Activities: MainActivity and LoginActivity. I have one Fragment too, the name is TabFragment (with a TabLayout), its parent is MainActivity. How can I intent into the specific TabFragment from my Login Activity.
这是我的主要活动:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
id = getIntent().getStringExtra("id");
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.confrag, new FragmentUtama()).commit();
getSupportFragmentManager().beginTransaction().replace(R.id.confrag, new TabFragment()).commit();
}
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
sharedPreferences = getSharedPreferences("login.conf", MODE_PRIVATE);
editor = sharedPreferences.edit();
String username = sharedPreferences.getString("username", "");
String password = sharedPreferences.getString("password", "");
Log.d(TAG, "Username:"+username+"\npassword:"+password);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
这是我的Tab片段:
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.viewpager, null);
tabLayout = (TabLayout)view.findViewById(R.id.tabs);
viewPager = (ViewPager)view.findViewById(R.id.viewpager);
viewPager.setAdapter(new Adapterbaru(getChildFragmentManager()));
tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
Long tsLong = System.currentTimeMillis()/1000;
String ts = tsLong.toString();
return view;
}
public interface OnFragmentInteractionListener {
void onFragmentInteraction(Uri uri);
}
public class Adapterbaru extends FragmentPagerAdapter{
public Adapterbaru(FragmentManager fm){
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0 : {
return new Fragmentone();
}
case 1 : {
return new FragmentTwo();
}
case 2 : {
return new Fragmenttree();
}
}
return null;
}
@Override
public int getCount() {
return jumlah_tab;
}
@Override
public CharSequence getPageTitle(int position) {
switch(position){
case 0 : {
return "First";
}
case 1 : {
return "Second";
}
case 2 : {
return "Third";
}
}
return null;
}
推荐答案
-
从您的
LoginActivity
中,通过Intent
将制表符片段(FragmentOne, FragmentTwo, FragmentThree)
id作为0, 1, 2
传递.
From your
LoginActivity
, pass tabs fragment(FragmentOne, FragmentTwo, FragmentThree)
id as0, 1, 2
throughIntent
.
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.putExtra("FRAGMENT_ID", 0);
startActivity(intent);
在您的 MainActivity
中,从Intent
// Inside OnCreate()
int fragmentId = getIntent().getIntExtra("FRAGMENT_ID", 0);
在 MainActivity
中,使用FragmentManager
显示TabFragment
,然后使用Bundle
From MainActivity
, show TabFragment
using FragmentManager
and pass fragmentId
to TabFragment
using Bundle
// Inside OnCreate()
Bundle bundle = new Bundle();
bundle.putString("TARGET_FRAGMENT_ID", fragmentId);
TabFragment tabFragment = new TabFragment();
tabFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.confrag, tabFragment).commit();
在 TabFragment
中,从Bundle
获取目标片段(FragmentOne, FragmentTwo, FragmentThree)
id,最后使用ViewPager.setCurrentItem(POSITION)
方法显示所需的片段.
In TabFragment
, get target fragment (FragmentOne, FragmentTwo, FragmentThree)
id from Bundle
and finally show desired fragment using ViewPager.setCurrentItem(POSITION)
method.
// Inside OnCreateView()
int position = getArguments().getInt("TARGET_FRAGMENT_ID");
viewpager.setCurrentItem(position);
希望这会有所帮助〜
这篇关于如何意图进入特定的选项卡片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!