切换片段 - 从一个片段移动到另一个片段

时间:2013-06-20 11:01:35

标签: android android-layout android-fragments

我正在使用FragmentActivity创建一个演示应用。

我正试图从一个片段移动到下一个片段(按钮点击)。从那时起,我想转到上一个片段(点击按钮)。

我的问题找到了很多答案。这是我的代码:

myfragment_1.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:orientation="vertical" >

<Button
    android:id="@+id/button_1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="click1"
    android:textAppearance="?android:attr/textAppearanceLarge" />

myfragment_2.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/f_l1" >

<Button
    android:id="@+id/button_2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="click"
    android:textAppearance="?android:attr/textAppearanceLarge" />

片段类是:

MyFragment_1.java

public class MyFragment_1 extends Fragment implements OnClickListener {
public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";
Button messageTextView;

public static final MyFragment newInstance() {
    MyFragment_1 f = new MyFragment_1();
    return f;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.myfragment_1, container, false);

    messageTextView = (Button) v.findViewById(R.id.button_1);

    messageTextView.setOnClickListener(this);
    return v;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);

}

@Override
public void onClick(View v) {
    if (v == messageTextView) {
        Log.e("inside method", "fragment load");
        Fragment fragment2 = new MyFragment_2();
        FragmentManager fm = getFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.replace(R.id.f_l1, fragment2);
        transaction.commit();

        Log.e("outside method", "fragment loaded...............");
    }
}

}

MyFragment_2.java

    public class MyFragment_2 extends Fragment {
public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";

public static final MyFragment_2 newInstance()
{
        MyFragment_2 f = new MyFragment_2();

    return f;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.myfragment_2, container, false);

    Button messageTextView = (Button)v.findViewById(R.id.button_2);
    messageTextView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
             getFragmentManager().popBackStack();
        }
    });
    return v;
}

}

* MainActivity Code:*

public class PageViewActivity extends FragmentActivity {
MyPageAdapter pageAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_page_view);

    List<Fragment> fragments = getFragments();

    pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);

    ViewPager pager = (ViewPager)findViewById(R.id.viewpager);
    pager.setAdapter(pageAdapter);

}

private List<Fragment> getFragments(){
    List<Fragment> fList = new ArrayList<Fragment>();

    fList.add(MyFragment_1.newInstance());
    fList.add(MyFragment_2.newInstance());
    return fList;
}

private class MyPageAdapter extends FragmentPagerAdapter {
    private List<Fragment> fragments;

    public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
        super(fm);
        this.fragments = fragments;
    }
    @Override
    public Fragment getItem(int position) {
        return this.fragments.get(position);
    }

    @Override
    public int getCount() {
        return this.fragments.size();
    }
}

}

及其布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

</RelativeLayout>

任何人都可以告诉我这段代码有什么问题吗?任何帮助将不胜感激。

提前致谢。

0 个答案:

没有答案