我使用TabLayout和viewpager在每个标签之间滑动,我在定位标签时遇到问题,例如:我在标签3处点击了某些内容&转到新活动,这是一个细节的东西,我的问题是当我想回到具体的tab3,但没有回到tabs3,它直接回到tabs1,我已经搜索但仍然没有得到解决。
如何使用片段从活动返回到选项卡中的特定选项卡..?
我在第一次调用TabFragment时使用它。
public class MainActivity extends AppCompatActivity{
DrawerLayout mDrawerLayout;
NavigationView mNavigationView;
FragmentManager mFragmentManager;
FragmentTransaction mFragmentTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// session manager
session = new SessionManager(getApplicationContext());
if (!session.isLoggedIn()) {
logoutUser();
}
mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.containerView, new TabFragment()).commit();
}}
然后,这就是我设置标签的方式。
public class TabFragment扩展了Fragment {
public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 4 ;
public static Toolbar toolbar;
private int[] icons_off = {
R.mipmap.ic_panorama_white_36dp,
R.mipmap.ic_notifications_active_white_36dp,
R.mipmap.ic_inbox_white_36dp,
R.mipmap.ic_tag_faces_white_36dp
};
private int[] icons_on = {
R.mipmap.ic_panorama_black_36dp,
R.mipmap.ic_notifications_active_black_36dp,
R.mipmap.ic_inbox_black_36dp,
R.mipmap.ic_tag_faces_black_36dp
};
private String[] title_tab = {
"",
"Tour",
"Draft",
"My Profile"
};
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/**
*Inflate tab_layout and setup Views.
*/
View x = inflater.inflate(R.layout.tab_layout,null);
tabLayout = (TabLayout) x.findViewById(R.id.tabs);
viewPager = (ViewPager) x.findViewById(R.id.viewpager);
/**
*Set an Apater for the View Pager
*/
viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
toolbar = (android.support.v7.widget.Toolbar) x.findViewById(R.id.tool_bar);
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayShowTitleEnabled(false);
toolbar.setLogo(R.mipmap.ic_landscape_white_36dp);
setHasOptionsMenu(true);
/**
* Now , this is a workaround ,
* The setupWithViewPager dose't works without the runnable .
* Maybe a Support Library Bug .
*/
tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
tabLayout.getTabAt(0).setIcon(icons_on[0]).select();
tabLayout.getTabAt(1).setIcon(icons_off[1]);
tabLayout.getTabAt(2).setIcon(icons_off[2]);
tabLayout.getTabAt(3).setIcon(icons_off[3]);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
switch (position) {
case 0:
toolbar.setLogo(R.mipmap.ic_landscape_white_36dp);
toolbar.setTitle(Html.fromHtml("<font color='#ffffff'>"+title_tab[position]+"</font>"));
tabLayout.getTabAt(0).setIcon(icons_on[0]);
tabLayout.getTabAt(1).setIcon(icons_off[1]);
tabLayout.getTabAt(2).setIcon(icons_off[2]);
tabLayout.getTabAt(3).setIcon(icons_off[3]);
break;
case 1:
toolbar.setLogo(null);
toolbar.setTitle(Html.fromHtml("<font color='#ffffff'>"+title_tab[position]+"</font>"));
tabLayout.getTabAt(0).setIcon(icons_off[0]);
tabLayout.getTabAt(1).setIcon(icons_on[1]);
tabLayout.getTabAt(2).setIcon(icons_off[2]);
tabLayout.getTabAt(3).setIcon(icons_off[3]);
break;
case 2:
toolbar.setLogo(null);
toolbar.setTitle(Html.fromHtml("<font color='#ffffff'>"+title_tab[position]+"</font>"));
tabLayout.getTabAt(0).setIcon(icons_off[0]);
tabLayout.getTabAt(1).setIcon(icons_off[1]);
tabLayout.getTabAt(2).setIcon(icons_on[2]);
tabLayout.getTabAt(3).setIcon(icons_off[3]);
break;
case 3:
toolbar.setLogo(null);
toolbar.setTitle(Html.fromHtml("<font color='#ffffff'>"+title_tab[position]+"</font>"));
tabLayout.getTabAt(0).setIcon(icons_off[0]);
tabLayout.getTabAt(1).setIcon(icons_off[1]);
tabLayout.getTabAt(2).setIcon(icons_off[2]);
tabLayout.getTabAt(3).setIcon(icons_on[3]);
break;
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
});
return x;
}
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_main, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_search) {
//Toast.makeText(getContext(), "Chat Clicked !", Toast.LENGTH_SHORT).show();
Intent in = new Intent(getActivity(), SearchActivity.class);
in.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(in);
} else if(id == R.id.action_setting){
//Toast.makeText(getContext(), "Chat Clicked !", Toast.LENGTH_SHORT).show();
Intent in = new Intent(getActivity(), SettingsActivity.class);
in.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(in);
}
return super.onOptionsItemSelected(item);
}
class MyAdapter extends FragmentPagerAdapter{
public MyAdapter(FragmentManager fm) {
super(fm);
}
/**
* Return fragment with respect to Position .
*/
@Override
public Fragment getItem(int position)
{
switch (position){
case 0 : return new Feed();
case 1 : return new Tour();
case 2 : return new Project();
case 3 : return new MyProfile();
}
return null;
}
@Override
public int getCount() {
return int_items;
}
@Override
public CharSequence getPageTitle(int position) {
/*
switch (position){
case 0 :
return "Primary";
case 1 :
return "Social";
case 2 :
return "Updates";
case 3 :
return "User";
}
*/
return null;
}
public boolean onTouch(View v, MotionEvent event) {
return true;
}
}}
和我在回到我想要的之前的最后一个位置。
public class TourActivity extends AppCompatActivity {
@Override
public void onBackPressed() {
super.onBackPressed();
// here's where i want to back to specific tab
}
答案 0 :(得分:2)
在创建新活动之前,请保存viewpager位置,之后,当您向后导航时,使用setCurrentItem(position);
首先,在开始其他活动之前保存您的位置,它可以是sharedPreferences或其他。
其次,在tab片段'onResume从sharedPreferences读取位置,并使用viewpager.setCurrentItem(position)将当前项设置为viewpager。
答案 1 :(得分:0)
我会在导航到其他活动时保存当前片段位置,并在onResume()上返回第一个活动时设置 viewpager.setCurrentItem(int index)其中index是您保存的值。
答案 2 :(得分:0)
尝试在被调用活动中按下后退按钮时添加finish()。当我们从其他活动中调用一个活动时,会保存调用者活动的实例状态,因此您将片段保存在堆栈中,然后调用被调用的活动,因此如果您希望返回,则只需完成第一个活动,然后将调用调用者活动的实例状态,您将获得调用被调用活动的相同片段。因此,在被调用的活动中添加此代码:
@Override onBackPressed{finish();}