[英]How to go to a specific tab in a Sliding Tab Layout from another activity
In my Main Activity I have a Sliding Tab Layout with three tabs.在我的主要活动中,我有一个包含三个选项卡的滑动选项卡布局。 Clicking on a button in the first tab leads to a series of activities.
单击第一个选项卡中的按钮会导致一系列活动。 I want the following: Clicking on a button in the last activity of the series should take the user to the second tab of my Main Activity (the Sliding Tab Layout).
我想要以下内容:单击该系列最后一个活动中的按钮应将用户带到我的主活动的第二个选项卡(滑动选项卡布局)。
Addendum: This is a similar question.附录: 这是一个类似的问题。 Having looked at it, where do I place
onResume
and it's function setCurrentTab
?看过之后,我应该把
onResume
和它的函数setCurrentTab
放在哪里? Do I place it in MainActivity, the Adapter, or the Page Fragments?我是将它放在 MainActivity、Adapter 还是 Page Fragments 中? What would the code for
onResume
and it's function setCurrentTab
look like? onResume
及其函数setCurrentTab
的代码是什么样的?
Sliding Tab Layout滑动选项卡布局
Sliding Tab Strip滑动标签条
MainActivity:主要活动:
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
// Get the ViewPager and set it's PagerAdapter so that it can display items
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new MainFragmentPageAdapterForTabs(getSupportFragmentManager(),
MainActivity.this));
// Give the SlidingTabLayout the ViewPager
SlidingTabLayout slidingTabLayout = (SlidingTabLayout) findViewById(R.id.sliding_tabs);
// Center the tabs in the layout
slidingTabLayout.setDistributeEvenly(true);
slidingTabLayout.setViewPager(viewPager);
}
public void takeSurveyButtonAction(View view){
Intent intentSurvey = new Intent(MainActivity.this, SurveyActivity1.class);
startActivity(intentSurvey);
}
}
Fragment Adapter:片段适配器:
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class MainFragmentPageAdapterForTabs extends FragmentPagerAdapter {
final int PAGE_COUNT = 3;
private String tabTitles[] = new String[] { "Take Survey", "Results", "Credits"};
private Context context;
public MainFragmentPageAdapterForTabs(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
@Override
public int getCount() {
return PAGE_COUNT;
}
@Override
public Fragment getItem(int position) {
return MainPageFragmentForTabs.newInstance(position + 1);
}
@Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}
}
Page Fragments:页面片段:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
// In this case, the fragment displays simple text based on the page
public class MainPageFragmentForTabs extends Fragment {
public static final String ARG_PAGE = "ARG_PAGE";
private int mPage;
public static MainPageFragmentForTabs newInstance(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
MainPageFragmentForTabs fragment = new MainPageFragmentForTabs();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPage = getArguments().getInt(ARG_PAGE);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = null;
if(mPage==1){
View view1 = inflater.inflate(R.layout.fragment_page_recycler_view, container, false);
FragmentActivity a = getActivity();
//recycler
RecyclerView recyclerView = (RecyclerView) view1.findViewById(R.id.my_recycler_view);
recyclerView.setHasFixedSize(true);
//layout manager
LinearLayoutManager manager = new LinearLayoutManager(a);
manager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(manager);
//recyclerView.setItemAnimator(new DefaultItemAnimator());
MainAdapterCV1 ca = new MainAdapterCV1();
recyclerView.setAdapter(ca);
view=view1;
}
if(mPage==2){
View view2 = inflater.inflate(R.layout.fragment_page_recycler_view, container, false);
FragmentActivity a = getActivity();
//recycler
RecyclerView recyclerView = (RecyclerView) view2.findViewById(R.id.my_recycler_view);
recyclerView.setHasFixedSize(true);
//layout manager
LinearLayoutManager manager = new LinearLayoutManager(a);
manager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(manager);
//recyclerView.setItemAnimator(new DefaultItemAnimator());
ResultsMainAdapterCV2 ca = new ResultsMainAdapterCV2();
recyclerView.setAdapter(ca);
view=view2;
}
if(mPage==3){
View view3 = inflater.inflate(R.layout.fragment_page_recycler_view, container, false);
FragmentActivity a = getActivity();
//recycler
RecyclerView recyclerView = (RecyclerView) view3.findViewById(R.id.my_recycler_view);
recyclerView.setHasFixedSize(true);
//layout manager
LinearLayoutManager manager = new LinearLayoutManager(a);
manager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(manager);
//recyclerView.setItemAnimator(new DefaultItemAnimator());
CreditsMainAdapterCV3 ca = new CreditsMainAdapterCV3();
recyclerView.setAdapter(ca);
view=view3;
}
return view;
}
}
The Activity that should lead to the second tab in MainActivity.class (by clicking on submitButton):应该指向 MainActivity.class 中第二个选项卡的 Activity(通过单击 submitButton):
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.RadioButton;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.Toast;
public class SurveyActivity13 extends AppCompatActivity {
private RadioGroup radioGroup;
private RadioButton radioButton;
private Button submitButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_survey_activity13);
addListenerOnButton();
}
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.opt1:
if (checked)
// Pirates are the best
break;
case R.id.opt2:
if (checked)
// Ninjas rule
break;
}
}
public void addListenerOnButton() {
radioGroup = (RadioGroup) findViewById(R.id.radio_group);
submitButton = (Button) findViewById(R.id.takeSurveyButton);
submitButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(radioGroup.getCheckedRadioButtonId() == -1){
//happens if no radio buttons are checked
Toast.makeText(
SurveyActivity13.this,
"You have not selected an option",
Toast.LENGTH_SHORT).show();
}
else{
//happens if one of the radio buttons is checked
// get selected radio button from radioGroup
int selectedId = radioGroup.getCheckedRadioButtonId();
// find the radio button by returned id
radioButton = (RadioButton) findViewById(selectedId);
//save selection
String response = radioButton.getText().toString();
SharedPreferences sharedPreferences = getSharedPreferences(
"MyData", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("sex", response);
editor.apply();
//I want this to go to the second tab in MainActivity.class
Intent intentSurvey = new Intent(SurveyActivity13.this, MainActivity.class);
startActivity(intentSurvey);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_survey_activity13, menu);
return true;
}
@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_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Try using:尝试使用:
int page = 1;
viewPager.setCurrentItem(page);
EDIT: Passing the page index as an intent from your last Activity to your initial one编辑:将页面索引作为意图从上一个活动传递到初始活动
public void submitButton(View view){
Intent intentSurvey = new Intent(this, MainActivity.class);
intentSurvey.putExtra(ARG_PAGE, page);
startActivity(intentSurvey);
}
Getting the argument in the main Activity:在主活动中获取参数:
int defaultValue = 0;
int page = getIntent().getIntExtra(ARG_PAGE, defaultValue);
viewpager.setCurrentItem(page);
Add this to your activity.将此添加到您的活动中。
this will help you to get an idea.这将帮助您获得一个想法。
This will show the tab you want when first open the activity.这将在首次打开活动时显示您想要的选项卡。
@Override
public void onStart() {
super.onStart();
mViewPager.setCurrentItem(Your fragment name);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.