Android将一个片段导航到另一个片段正在工作但是,看不到其他片段的背景

时间:2016-05-11 10:53:50

标签: java android xml android-fragments

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

这些是我的步骤,

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

}

有四个片段类,我只提到了两个用于此目的的片段。当来到第三个片段时,无法向左或向右滑动。然后我用一个按钮转到第四个片段。但它没有用。

对此有什么想法吗?

谢谢。

4 个答案:

答案 0 :(得分:2)

I think You have four fragments, but what you did in MyPagerAdapter is

 @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");
        }
    }

you only added 3 fragment in switch case that is why you are not able to navigate from third fragment to fourth fragment, I think what you should do is

 @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");
            case 3: return FourthFragment.newInstance("FourthFragment, Instance 1");
            default: return ThirdFragment.newInstance("ThirdFragment, Default");
        }
    }

答案 1 :(得分:2)

您正在使用R.id.viewPager替换Fourth Fragment,这是错误的。

您应该调用ViewPager方法setcurrentItem(id)来更改View寻呼机内的片段。这是理想的导航方式,因为我们使用的是View Page。

在您的活动中,制作一个方法:

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

答案 2 :(得分:0)

Better override the onPageSelected() method of Viewpager to give the correct position of page.

    public void onPageSelected(int position) {
        //set your page here which gives current page position
    }

答案 3 :(得分:0)

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

}

在上面的代码中,在寻呼机适配器中,也定义了第四个适配器的情况,即

case 3: return FourthFragment.newInstance("FourthFragment, Instance 1");

第二个问题是,点击按钮,你无法使用此代码进入FourthFragment ......

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

代替android.support.v4.view.ViewPager上的替换片段,

transaction.replace(R.id.viewPager, fourthFragment);

使用FrameLayout。

transaction.replace(R.id.framelayout, fourthFragment);