如何从另一个活动转到滑动选项卡布局中的特定选项卡

时间:2015-06-11 22:31:21

标签: java android tabs

在我的主要活动中,我有一个带有三个标签的滑动标签布局。单击第一个选项卡中的按钮可以进行一系列活动。我想要以下内容:单击系列的最后一个活动中的按钮应该将用户带到我的主活动的第二个选项卡(滑动选项卡布局)。

附录:This是一个类似的问题。看了之后,我在哪里放置onResume它的功能setCurrentTab?我是否将它放在MainActivity,适配器或页面片段中? onResume及其函数setCurrentTab的代码是什么样的?

滑动标签布局

滑动标签条

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);
    }
}

片段适配器:

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];
    }
}

页面碎片:

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;
    }

}

应该导致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);
    }
}

2 个答案:

答案 0 :(得分:10)

尝试使用:

int page = 1;
viewPager.setCurrentItem(page);

编辑: 将页面索引作为意图从上一个活动传递到初始活动

public void submitButton(View view){
    Intent intentSurvey = new Intent(this, MainActivity.class);
    intentSurvey.putExtra(ARG_PAGE, page);
    startActivity(intentSurvey);
}

在主Activity中获取参数:

    int defaultValue = 0;
    int page = getIntent().getIntExtra(ARG_PAGE, defaultValue);
    viewpager.setCurrentItem(page);

答案 1 :(得分:0)

将此添加到您的活动中。

这将帮助您获得想法。
首次打开活动时,将显示您想要的标签。

 @Override
        public void onStart() {
            super.onStart();
            mViewPager.setCurrentItem(Your fragment name);

    }