Java 无法替换viewpager中的片段

Java 无法替换viewpager中的片段,java,android,android-fragments,Java,Android,Android Fragments,当列表视图中的一个项目被按下时,我一直试图用另一个片段替换当前片段。但是,当按下按钮时,onClick方法被激发,但它不会替换片段 JournalFragment.java public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { rootView = inflater.infl

当列表视图中的一个项目被按下时,我一直试图用另一个片段替换当前片段。但是,当按下按钮时,onClick方法被激发,但它不会替换片段

JournalFragment.java

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

        rootView = inflater.inflate(R.layout.fragment_journal_view, container, false);
        context = getContext();
        ListView myView = (ListView) rootView.findViewById(R.id.listView);

        TableControllerUser TCU = new TableControllerUser(context);
        final TableControllerJournal TCJ = new TableControllerJournal(context);


        int accID = TCU.getLoggedInId();
        Cursor cursor = TCJ.getAllJournals(accID);
        Cursor allFood = TCJ.getAllFoodJournals(accID);
        Cursor allActivity = TCJ.getAllActivityJournals(accID);
        Cursor[] cursors = {cursor, allFood, allActivity};

        MergeCursor m = new MergeCursor(cursors);

        final JournalAdapter adapter = new JournalAdapter(context, m, 0);

        myView.setAdapter(adapter);

        myView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Cursor c = (Cursor) adapter.getItem(position);
                Toast.makeText(getActivity(), "onClick Pressed!", Toast.LENGTH_SHORT).show();

                String title = c.getString(c.getColumnIndexOrThrow("title"));

                if (title.contains("Glucose")) {
                    String glucose = c.getString(c.getColumnIndexOrThrow("glucose"));
                    String dateTime = c.getString(c.getColumnIndexOrThrow("glucose_time"));
                    String journalId = c.getString(c.getColumnIndexOrThrow("_id"));
                    String split[] = dateTime.split(" ");
                    String date = split[0];
                    String time = split[1];

                    Fragment gluFrag = new GlucoseFragment();

                    Bundle bundle = new Bundle();
                    bundle.putString("time", time);
                    bundle.putString("date", date);
                    bundle.putString("glucose", glucose);
                    bundle.putString("journalId", journalId);

                    gluFrag.setArguments(bundle);
                    ((GraphActivity) getActivity()).replaceFragments(gluFrag, bundle);
                }
           }
        });
        return rootView;
        // Inflate the layout for this fragment
    }

}
public class GraphActivity extends AppCompatActivity {

    Context context;
    TabLayout mTabLayout;
    ViewPager mViewPager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getApplicationContext();
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_graph);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setElevation(0);


        mTabLayout = (TabLayout) findViewById(R.id.tabLayout);
        mViewPager = (ViewPager) findViewById(R.id.viewPager);
        ViewPagerAdapter pgAdapter = new ViewPagerAdapter(getSupportFragmentManager());
        pgAdapter.addFragments(new GraphFragment(), "Graph");
        pgAdapter.addFragments(new JournalViewFragment(), "Journal");
        mViewPager.setAdapter(pgAdapter);
        mTabLayout.setupWithViewPager(mViewPager);


    }

    public void replaceFragments(Fragment newFragment, Bundle bundle) {
        Fragment fragment = newFragment;
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        fragment.setArguments(bundle);
        ft.replace(R.id.jv, fragment);
        ft.commit();

    }
}
编辑:

ActivityGraph.xml

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="horizontal"
    tools:context=".GraphActivity"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/graph">


    <android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/primary"
        app:tabGravity="fill"
        app:tabMode="scrollable"
        app:tabSelectedTextColor="@color/white"
        app:tabTextColor="@color/black">
        <!--To be fixed in future to occupy whole bar when mode = fixed-->


    </android.support.design.widget.TabLayout>

    <edu.tp.sghealthapp.library.graphViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/tabLayout"></edu.tp.sghealthapp.library.graphViewPager>





</RelativeLayout>

将android:layout_置于linearLayout中的=“@+id/viewPager”下方会导致片段不再显示

编辑3

解决了此


将此代码剪下并放入活动中,然后检查



将此代码剪切并放入“活动”中,然后选中“活动”\u graph.xml:

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="horizontal"
    tools:context=".GraphActivity"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/graph">


    <android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/primary"
        app:tabGravity="fill"
        app:tabMode="scrollable"
        app:tabSelectedTextColor="@color/white"
        app:tabTextColor="@color/black">
        <!--To be fixed in future to occupy whole bar when mode = fixed-->


    </android.support.design.widget.TabLayout>

    <edu.tp.sghealthapp.library.graphViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/tabLayout"></edu.tp.sghealthapp.library.graphViewPager>

 <LinearLayout
    android:id="@+id/jv"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />



</RelativeLayout>

活动\u graph.xml:

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="horizontal"
    tools:context=".GraphActivity"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/graph">


    <android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/primary"
        app:tabGravity="fill"
        app:tabMode="scrollable"
        app:tabSelectedTextColor="@color/white"
        app:tabTextColor="@color/black">
        <!--To be fixed in future to occupy whole bar when mode = fixed-->


    </android.support.design.widget.TabLayout>

    <edu.tp.sghealthapp.library.graphViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/tabLayout"></edu.tp.sghealthapp.library.graphViewPager>

 <LinearLayout
    android:id="@+id/jv"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />



</RelativeLayout>

尝试
wrap_content
View Pager的高度

请参考此项。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/graph"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">


    <android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/ColorPrimary"
        app:tabGravity="fill"
        app:tabMode="scrollable"
        app:tabSelectedTextColor="#FFF"
        app:tabTextColor="#000">
        <!--To be fixed in future to occupy whole bar when mode = fixed-->


    </android.support.design.widget.TabLayout>

    <edu.tp.sghealthapp.library.graphViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tabLayout" />

    <RelativeLayout
        android:id="@+id/jv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/viewPager" />


</RelativeLayout>
尝试
wrap_content
View Pager的高度

请参考此项。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/graph"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">


    <android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/ColorPrimary"
        app:tabGravity="fill"
        app:tabMode="scrollable"
        app:tabSelectedTextColor="#FFF"
        app:tabTextColor="#000">
        <!--To be fixed in future to occupy whole bar when mode = fixed-->


    </android.support.design.widget.TabLayout>

    <edu.tp.sghealthapp.library.graphViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tabLayout" />

    <RelativeLayout
        android:id="@+id/jv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/viewPager" />


</RelativeLayout>


是否执行了if条件?此id是否存在于R.id.jv?@PradeepGupta if条件已执行,但片段事务未执行work@HaroonR.id.jv存在于JournalFragment.xml中。您是否也可以发布您的
活动图.xml
您的if条件是否已执行?此id是否存在于R.id.jv?@PradeepGupta if条件是否已执行已执行,但片段事务未执行work@HaroonR.id.jv存在于JournalFragment.xml中。您是否也可以发布您的
活动图.xml
片段可以替换,但现在我遇到了一些格式问题:@Isaac我无法检查您的链接,因为它在我的公司被阻止。抱歉:)哦,我想日记片段的布局还在后面。?你可以尝试将片段容器放在日志片段的布局中,然后将其替换。片段本身可以被替换,但现在我遇到了一些格式问题:@Isaac我无法检查你的链接,因为它在我的公司被阻止了。抱歉:)哦,我想日记片段的布局还在后面。?您可以尝试将片段容器放在journal fragment的布局中,然后将其替换。您的方法和Vivek_Neel的方法都会产生所需的效果,但格式有一些问题,请参阅编辑。谢谢您的方法和Vivek_Neel的方法都能产生所需的效果,但格式有一些问题,请参阅编辑。谢谢这是行不通的。即使触发了onClick方法,新片段也不会出现在屏幕上:(它将替换并在“查看寻呼机”下方显示您的片段内容。请检查它。如果没有包装内容,请提供一些固定高度以查看寻呼机,如100dp。@jayDorider现在显示片段,但它显示在当前片段的旁边。是否有方法完全替换片段?我已经在使用fragmentTransaction。replace@Isaac检查我的answer EDIT 1 part试用。@Isaac还注意到id jd在
列表视图中定义了两次,并用不同的名称修改了两次。这不起作用。即使触发了onClick方法,新片段也不会出现在屏幕上:(它将替换并在“查看寻呼机”下方显示您的片段内容。请检查它。如果没有包装内容,请提供一些固定高度以查看寻呼机,如100dp。@jayDorider现在显示片段,但它显示在当前片段的旁边。是否有方法完全替换片段?我已经在使用fragmentTransaction。replace@Isaac检查我的answer EDIT 1 part试用。@Isaac还注意到id jd在
列表视图中定义了两次,并使用不同的名称进行了修改。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/graph"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">


    <android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/ColorPrimary"
        app:tabGravity="fill"
        app:tabMode="scrollable"
        app:tabSelectedTextColor="#FFF"
        app:tabTextColor="#000">
        <!--To be fixed in future to occupy whole bar when mode = fixed-->


    </android.support.design.widget.TabLayout>

    <edu.tp.sghealthapp.library.graphViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tabLayout" />

    <RelativeLayout
        android:id="@+id/jv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/viewPager" />


</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

  <FrameLayout
       android:id="@+id/jd"
       android:layout_width="match_parent"
       android:layout_height="match_parent">
  </FrameLayout>

</LinearLayout>