java - Android 将一个 fragment 导航到另一个 fragment 可以正常工作,但是看不到其他 fragment 的背景

标签 java android xml android-fragments

我尝试将一个 fragment 导航到另一个 fragment 。在这里我使用一个按钮来完成这个过程。当我运行这个时,我最后得到一个空白屏幕。

这是我的步骤,

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;
        }

    }
}

我想将第三个 fragment 导航到第四个 fragment 。

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;
    }

}

有四个 fragment 类,我只提到了两个用于此目的的 fragment 类。当到达第三个 fragment 时,无法向左或向右滑动。然后我使用按钮转到第四个 fragment 。但它不起作用。

对此有什么想法吗?

谢谢。

最佳答案

您正在用 R.id.viewPager 替换第四个 fragment ,这是错误的。

您应该调用 ViewPager 方法 setcurrentItem(id) 来更改 View pager 内的 fragment 。这是理想的导航方式,因为我们使用的是 View Pager。

在您的 Activity 中,创建一个方法:

public void setCurrentItem(int which) {
    if(viewpager != null && which >= 0 && which <= 4) {
        viewpager.setCurrentItem(which);
    }
}

ThirdFragment.java 调用此方法:

button.setOnClickListener(new View.OnClickListener() {
    if(getActivity() != null) {
        getActivity().setCurrentItem(3); // fourth fragment index is 3
    }
}

关于java - Android 将一个 fragment 导航到另一个 fragment 可以正常工作,但是看不到其他 fragment 的背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37160495/

相关文章:

java - 命令行错误: Could not find or load main class Part1

java - 在java中将 float 舍入到下一个整数值

安卓渐变背景

android 480x800 hdpi 和 480x800 mdpi 之间的区别

android - 运行时 Android 中的 Kiosk 模式

javascript - Ajax 调用 CORS 禁用服务

java - 使用 MongoDB 作为 Spring 批处理作业存储库

尽管在后台线程中加载许多图像,但 JavaFX 应用程序会卡住几秒钟

java - Eclipse Java(非 Android)中的 Strings.xml 或 String 资源

xml - 丑陋的 xml 输出