gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 09:52:55 26 4
gpt4 key购买 nike

在我的主要 Activity 中,我有一个包含三个选项卡的滑动选项卡布局。单击第一个选项卡中的按钮会导致一系列 Activity 。我想要以下内容:单击该系列最后一个 Activity 中的按钮应将用户带到我的主要 Activity 的第二个选项卡(滑动选项卡布局)。

附录:This是一个类似的问题。看过之后,我应该把 onResume 和它的函数 setCurrentTab 放在哪里?我是将它放在 MainActivity、Adapter 还是 Page Fragments 中? onResume 的代码和它的函数 setCurrentTab 看起来像什么?

滑动标签布局

滑动标签条

主要 Activity :

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 适配器:

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

页面 fragment :

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

最佳答案

尝试使用:

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

编辑:将页面索引作为 Intent 从上一个 Activity 传递到初始 Activity

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

关于java - 如何从另一个 Activity 转到滑动选项卡布局中的特定选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30792549/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com