我创建了一个视图寻呼机,其中两个片段用于从一个片段移动到第二个片段。在第二个片段页面中,我想要以下内容: 当我点击按钮时,片段从第二个片段改为第一个片段;在视图中,寻呼机意味着第二页到第一页。第一个和第二个片段有自己的XML文件,查看分页器在主片段的XML文件中。
意思是:
class ViewPagerMainActivity extends FragmentActivity
包含包含ViewPager 现在我需要从第二个片段移动到第一个片段。我怎么能这样做?
我的ViewPagerMainActivity
如下
public class CopyOfViewPagerMainActivity extends FragmentActivity implements
OnClickListener, OnPageChangeListener {
Button btnBack, btnNextImage;
LinearLayout llMainPager;
TelephonyManager telemamanger;
TextView txtOpname, txtCharge;
String operatorCodeName;
int level = -1;
double scale;
int rawlevel;
LinearLayout llayoutMainpager;
int image;
ViewPager pager;
Boolean flagzoom, flagscale, flagfade, flagdepth, flagnone, flagpivot,
flag3d;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_pager_main);
// my other code here
}
private class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(
android.support.v4.app.FragmentManager fragmentManager) {
super(fragmentManager);
}
@Override
public Fragment getItem(int pos) {
switch (pos) {
case 0:
return FirstFragment.newInstance("");
case 1:
return SecondFragment.newInstance("");
default:
return SecondFragment.newInstance("Default");
}
}
@Override
public int getCount() {
return 2; // return no of fragment created by us
}
}
@Override
public void onClick(View v) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
}
}
我的第一个片段
public class CopyOfFirstFragment extends Fragment implements OnTouchListener ,OnPageChangeListener{
String permanentval = "";
LinearLayout llayoutslide;
LinearLayout llayoutpassword;
int image;
TextView txtTime, txtDay, txtmissedCall, txtChargePercent,txtSMS;
ImageView imgCharge;
int charge;
Boolean flagAnimzoom;
ViewPager pchange;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater
.inflate(R.layout.sliding_lock_pager, container, false);
// my all code here
return v;
}
public static CopyOfFirstFragment newInstance(String text) {
CopyOfFirstFragment f = new CopyOfFirstFragment();
Bundle b = new Bundle();
b.putString("msg", text);
f.setArguments(b);
return f;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
}
}
我的第二个片段
public class CopyOfSecondFragment extends Fragment implements OnClickListener {
LinearLayout llayoutPasswordEnter,llayoutPasswordConfirm,llayoutPasswordPager;
Button btnNext, btnOne, btnTwo, btnThree, btnFour, btnFive, btnSix,
btnSeven, btnEight, btnNine, btnZero;
TextView txtClear, txtDone;
String store = "";
int one = 0, two = 0;
String permanentval = "";
RelativeLayout rlayoutPasswordEnter;
int image;
private Vibrator vibrator;
Boolean getflagvibrate=false, getflagSounds=false;
MediaPlayer mp;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.password, container,
false);
// all code here
txtDone.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// in this button i want to move from this fragment to first means i want to display firt xml file instead of this second one <----- here want to move
}
});
return v;
}
public void setButtonSize(Button btn) {
int width = Constant.getDeviceWidth(getActivity()) / 5;
LayoutParams params = new LayoutParams(width, width);
params.leftMargin = 8;
params.rightMargin = 8;
btn.setLayoutParams(params);
}
public static CopyOfSecondFragment newInstance(String text) {
CopyOfSecondFragment f = new CopyOfSecondFragment();
Bundle b = new Bundle();
b.putString("msg", text);
f.setArguments(b);
return f;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:2)
我找到了我的解决方案:在我的点击中使用以下
final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.details, new NewFragmentToReplace, "NewFragmentTag");
ft.commit();
答案 1 :(得分:0)
我不确定您要做什么,但如果您尝试以编程方式更改页面:
ViewPager.setCurrentItem(int item)
会做到这一点。
答案 2 :(得分:0)
您可以使用viewPager.setCurrentItem(CURRENT_FRAGMENT_TO_BE_SET)
来执行此操作
答案 3 :(得分:0)
您也可以执行此操作,将此代码放入您的点击方法
((ViewPagerMainActivity)getActivity()).pager.setCurrentItem(0);
setCurrentItem(0)中的 0
是您想要的任何活动编号
答案 4 :(得分:0)
在您的CopyOfViewPagerMainActivity类中创建您的viewpager静态,然后从另一个类调用它。
例如,
CopyOfViewPagerMainActivity.pager.setCurrentItem(1);