【发布时间】:2016-09-06 17:28:52
【问题描述】:
我尝试将一个片段导航到另一个片段。在这里我使用一个按钮来完成这个过程。当我运行它时,最后会出现一个空白屏幕。
这是我的步骤,
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 {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final int page=2;
final ViewPager 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, Instance 1");
case 1: return SecondFragment.newInstance("SecondFragment, Instance 1");
case 2: return ThirdFragment.newInstance("ThirdFragment, Instance 1");
default: return ThirdFragment.newInstance("ThirdFragment, Default");
}
}
@Override
public int getCount() {
return 4;
}
}
}
我想将第三个片段导航到第四个片段。
activity_main.xml
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/viewPager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
third_frag.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_red_light" >
<TextView
android:id="@+id/tvFragThird"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="26dp"
android:text="TextView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="@+id/printButton"
android:text="Next"/>
</RelativeLayout>
ThirdFragment.java
public class ThirdFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.third_frag, container, false);
TextView tv = (TextView) v.findViewById(R.id.tvFragThird);
tv.setText(getArguments().getString("msg"));
Button button=(Button)v.findViewById(R.id.printButton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FourthFragment fourthFragment=new FourthFragment();
Bundle bundle=new Bundle();
bundle.putInt(String.valueOf(FirstFragment.newInstance("Terance")),1);
fourthFragment.setArguments(bundle);
FragmentManager fragmentManager=getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.viewPager, fourthFragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
return v;
}
public static ThirdFragment newInstance(String text) {
ThirdFragment f = new ThirdFragment();
Bundle b = new Bundle();
b.putString("msg", text);
f.setArguments(b);
return f;
}
}
fourth_frag.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_dark">
<TextView
android:id="@+id/tvFragFourth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="26dp"
android:text="TextView" />
</RelativeLayout>
FourthFragment.java
public class FourthFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fourth_frag, container, false);
TextView tv = (TextView) v.findViewById(R.id.tvFragFourth);
tv.setText("Fourth Fragment");
return v;
}
public static FourthFragment newInstance(String text) {
text="Terance";
FourthFragment f = new FourthFragment();
Bundle b = new Bundle();
b.putString("msg", text);
f.setArguments(b);
return f;
}
}
有四个片段类,我只提到了两个用于此目的。当来到第三个片段时,不能向左或向右滑动。然后我使用一个按钮转到第四个片段。但它不起作用。
对此有什么想法吗?
谢谢。
【问题讨论】:
-
你为什么不告诉viewpager自己滚动到第4个片段,你确实用你的viewpager加载了4个片段。
-
@danypata :它有目的,在这个应用程序中它应该停止检查一些条件。
-
在按钮点击时尝试使用子片段管理器
-
@Abhishek :我试过了,但给我买了一个错误。
-
您可以尝试将 viewpager 的父级设置为 activity_main.xml 中的框架布局,并在单击按钮时替换该框架,它会起作用吗??
标签: java android xml android-fragments