如何在通知上打开特定的viewpager选项卡单击android

时间:2016-07-21 04:47:30

标签: android

现在我只是打开一个有关额外数据的通知点击活动。在此活动中,我想在点击通知数据时打开特定的viewpager选项卡。

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


     readMore = (Button) findViewById(R.id.openACtivity);

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
     setSupportActionBar(toolbar);
     getSupportActionBar().setDisplayHomeAsUpEnabled(true);

}
public void OpenActivity() {

}

@Override
public boolean onSupportNavigateUp() {
    finish();
    return false;
}

@Override
public void onBackPressed() {
    super.onBackPressed();
    finish();
}

2 个答案:

答案 0 :(得分:2)

从您发送有意图的额外数据的位置,同时发送您想要点击的标签的位置,并在标签的活动中获取onCreate中的此位置,如下所示:

viewpager.setCurrentItem(getIntent().getIntExtra("position",DEFAULT_INT_VALUE));

答案 1 :(得分:0)

像这样创建你的视图寻呼机片段

public class TabFragment extends BaseFragments {
private static int argument = 0;
private TabLayout tabLayout;
private ViewPager viewPager;
private TextView employeeReferralText;
Context context;
private static Bundle bundle;
private SetName setName;
private SharedPreferences sharedPreferences;

public static TabFragment newInstance(int arg, Bundle bundles) {
    TabFragment fragment = new TabFragment();
    argument = arg;
    bundle = bundles;
    return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_tab, container, false);

    try {
        context = getActivity();


        viewPager = (ViewPager) view.findViewById(R.id.viewpager);
        setupViewPager(viewPager);
        tabLayout = (TabLayout) view.findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
        bundle = getArguments();
        setActionBar();

    } catch (Exception e) {
        e.printStackTrace();
    }


    return view;
}

private void setupViewPager(ViewPager viewPager) {
    try {

        ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
        adapter.addFrag(new FirstFragment(), "firstFragment");
        adapter.addFrag(new SecondFragment(),"secondFragment");

        viewPager.setAdapter(adapter);

        if (argument == 0)
            ;
        else {
            viewPager.setCurrentItem(argument, true);
            argument = 0;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

static class ViewPagerAdapter extends FragmentPagerAdapter {

    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();


    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFrag(Fragment fragment, String title) {
        try {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public CharSequence getPageTitle(int position) {

        return mFragmentTitleList.get(position);
    }
}

}

关于活动的新内容

@覆盖     public void onNewIntent(Intent intent){         试试{

        if (intent != null) {
          replaceFragment(context,TabFragment.newInstance(0,new Bundle()));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}