0
我有兩個片段活動。現在我想在這之間切換。另外我想在第一個片段活動結束後執行此操作。如何在兩個片斷之間切換活動android
這裏是我的片段活動課,
MainActivity.java
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.view.ViewPager;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
public class MainActivity extends FragmentActivity {
private ViewPager pager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final int page=2;
pager = (ViewPager) findViewById(R.id.viewPager);
pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
pager.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (pager.getCurrentItem()==page) {
return true;
}
return false;
}
});
}
private class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int pos) {
switch(pos) {
case 0: return FirstFragment.newInstance("FirstFragment");
case 1: return SecondFragment.newInstance("SecondFragment");
case 2: return ThirdFragment.newInstance("ThirdFragment");
case 3: return FourthFragment.newInstance("FourthFragment");
case 4: return FifthFragment.newInstance("FifthFragment");
default: return ThirdFragment.newInstance("DefaultFragment");
}
}
@Override
public int getCount() {
return 5;
}
}
public void setCurrentItem(int which) {
if(pager != null && which >= 0 && which <= 4) {
pager.setCurrentItem(which);
}
}
}
SecondActivity.java
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.view.ViewPager;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
public class SecondActivity extends FragmentActivity {
private ViewPager pager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
final int page=2;
pager = (ViewPager) findViewById(R.id.viewPager_1);
pager.setAdapter(new MyPagerAdapter2(getSupportFragmentManager()));
pager.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (pager.getCurrentItem()==page) {
return true;
}
return false;
}
});
}
private class MyPagerAdapter2 extends FragmentPagerAdapter {
public MyPagerAdapter2(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int pos) {
switch(pos) {
case 0: return FirstFragment_1.newInstance("FirstFragment_2");
case 1: return SecondFragment_1.newInstance("SecondFragment_2");
case 2: return ThirdFragment_1.newInstance("ThirdFragment_2");
case 3: return FourthFragment_1.newInstance("FourthFragment_2");
case 4: return FifthFragment_1.newInstance("FifthFragment_2");
default: return ThirdFragment_1.newInstance("DefaultFragment_2");
}
}
@Override
public int getCount() {
return 5;
}
}
public void setCurrentItem(int which) {
if(pager != null && which >= 0 && which <= 4) {
pager.setCurrentItem(which);
}
}
}
問題我怎麼能做到這一點?
更確切地說你想要什麼。 –
你想要什麼,我想讓你有兩個片段你想要做什麼 –