[英]Recyclerview item onClick to open a fragment from Main Activity Fragment
[英]Android onClick on Main to open fragment
我在使用片段時遇到問題。 當我按下按鈕時,我想在 MainActivity 上以編程方式打開一個片段。 使用下面的代碼運行應用程序,但它不會打開片段:
MyFragment fragment = new MyFragment();
android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.fragment_container,fragment, "Hello");
transaction.commit();
我在這里做錯了什么? 我對堆棧溢出做了很多研究,但找不到適合我需求的問題。
編輯
這是我希望片段出現的布局:
<?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:id="@+id/content1"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:layout_marginTop="96dp"
tools:context="com.example.sdilab.pap.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1"
android:weightSum="2">
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@id/cardView"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_weight="1"
card_view:cardUseCompatPadding="true">
// Other layouts and views here
<Space
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView1"
android:layout_gravity="center"
android:layout_marginBottom="16dp"
android:src="@drawable/exercicio"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/exerciciotext"
android:textAlignment="center"
android:gravity="bottom"
android:layout_marginBottom="36dp"
android:text="Registar\nexercício"
android:textSize="@dimen/activity_horizontal_margin"
/>
<Space
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@id/cardView"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_weight="1"
card_view:cardUseCompatPadding="true">
// Other layouts and views here
<Space
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView2"
android:layout_gravity="center"
android:layout_marginBottom="16dp"
android:src="@drawable/food"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:textAlignment="center"
android:gravity="bottom"
android:id="@+id/foodtext"
android:layout_marginBottom="36dp"
android:text="Registar\nrefeição"
android:textSize="@dimen/activity_horizontal_margin"
/>
<Space
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</android.support.v7.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1"
android:weightSum="2">
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardViewCalendar"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_weight="1"
android:elevation="0dp"
card_view:cardUseCompatPadding="true">
// Other layouts and views here
<Space
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView3"
android:layout_gravity="center"
android:layout_marginBottom="16dp"
android:src="@drawable/calendar"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:textAlignment="center"
android:id="@+id/calendartext"
android:gravity="bottom"
android:layout_marginBottom="36dp"
android:text="Agendar\ntreinos"
android:textSize="@dimen/activity_horizontal_margin"
/>
<Space
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@id/cardView"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_weight="1"
card_view:cardUseCompatPadding="true">
// Other layouts and views here
<Space
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView4"
android:layout_gravity="center"
android:layout_marginBottom="16dp"
android:src="@drawable/settings"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:textAlignment="center"
android:id="@+id/settingstext"
android:gravity="bottom"
android:layout_marginBottom="36dp"
android:text="Alterar\ndefinições"
android:textSize="@dimen/activity_horizontal_margin"
/>
<Space
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</android.support.v7.widget.CardView>
</LinearLayout>
</LinearLayout>
代替
transaction.add(R.id.fragment_container,fragment, "Hello");
和
transaction.add(R.id.fragment_container,fragment);
試試這個
FragmentManager mFragmentManager = getSupportFragmentManager();
FragmentTransaction mFragmentTransaction = mFragmentManager.beginTransaction();
Fragment fragment = new MyFragment();
mFragmentTransaction.add(R.id.fragment_container, fragment);
mFragmentTransaction.commit();
嘗試做的顯式調用show
添加它后您的片段。 像這樣的東西:
FragmentManager mFragmentManager = getSupportFragmentManager();
FragmentTransaction mFragmentTransaction = mFragmentManager.beginTransaction();
Fragment fragment = new MyFragment();
mFragmentTransaction.add(R.id.fragment_container, fragment);
mFragmentTransaction.show(fragment);
mFragmentTransaction.commit();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.