这里我有一个代码,您可以在其中滑动选项卡菜单,但我想在刷卡后打开另一个活动,例如Simmering.class。 我认为这应该有意图,但我应该如何或在哪里提出它。
以下是代码:
import java.util.Locale;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
@SuppressWarnings("unused")
public class MainActivity extends FragmentActivity {
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
* will keep every loaded fragment in memory. If this becomes too memory
* intensive, it may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
@Override
public int getCount() {
// Show 3 total pages.
return 4;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
case 3:
return getString(R.string.title_section4).toUpperCase(l);
}
return null;
}
}
/**
* A dummy fragment representing a section of the app, but that simply
* displays dummy text.
*/
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_dummy, container, false);
TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
dummyTextView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
}
答案 0 :(得分:0)
尝试在片段类的onCreateView方法中添加以下代码。检查您想要开始新活动的位置。并在其中调用startActivity方法。
Intent intent = new Intent(getApplicationContext(), Simmering.class);
getApplicationContext().startActivity(intent);
答案 1 :(得分:0)
您知道吗,刷卡后开始新的活动隐藏页面,您要刷到?如果您的回答是肯定的,则可以使用ViewPager
' s onPageChangeListener。当页面被更改/更新等时调用它的方法。用法如下:
mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int index) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
})
答案 2 :(得分:0)
我已经找到了实现我想要的选项,这是代码:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends FragmentActivity {
ViewPager viewPager=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager= (ViewPager) findViewById(R.id.pager);
FragmentManager fragmentManager=getSupportFragmentManager();
viewPager.setAdapter(new MyAdapter(fragmentManager));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// action with ID action_refresh was selected
case R.id.action_refresh:
Toast.makeText(this, R.string.refresh_clicked, Toast.LENGTH_SHORT).show();
break;
// action with ID action_settings was selected
case R.id.action_settings:
Toast.makeText(this, R.string.settings_clicked, Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
class MyAdapter extends FragmentStatePagerAdapter
{
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
Fragment fragment=null;
switch(i){
case 0: fragment = new Home(); break;
case 1: fragment = new Simmering(); break;
case 2: fragment = new LugnerCity(); break;
case 3: fragment = new Gmunden(); break;
case 4: fragment = new Salzburg(); break;
case 5: fragment = new Linz(); break;
case 6: fragment = new Saalbach(); break;
case 7: fragment = new Innsbruck(); break;
case 8: fragment = new Reutte(); break;
case 9: fragment = new Bregenz(); break;
case 10: fragment = new Kufstein(); break;
case 11: fragment = new Bratislava(); break;
}
return fragment;
}
@Override
public int getCount() {
return 12;
}
@Override
public CharSequence getPageTitle(int position) {
switch(position) {
case 0: return "Home";
case 1: return "Simmering";
case 2: return "Lugner City";
case 3: return "Gmunden";
case 4: return "Salzburg";
case 5: return "Linz";
case 6: return "Saalbach";
case 7: return "Innsbruck";
case 8: return "Reutte";
case 9: return "Bregenz";
case 10: return "Kufstein";
case 11: return "Bratislava";
}
return null;
}
}