嘿嘿!我正在尝试在编码时学习片段,并且我试图将一个片段与另一个片段切换。每当我点击下一个按钮时,当前片段将被替换为空白片段,我不知道为什么!
MainActivity.Java:
package com.alexoladele.testingshit;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import layout.WelcomeScreenFragment;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Makes sure that the root_layout view is not null before doing anything
if (findViewById(R.id.root_layout) != null) {
// Makes sure that there's no saved instance before proceeding
if (savedInstanceState == null) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction manager = fm.beginTransaction();
manager
.add(R.id.root_layout, WelcomeScreenFragment.newInstance(), "welcomeScreen")
.commit();
}
}
}
}
activity_main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.alexoladele.testingshit.MainActivity">
</FrameLayout>
WelcomeScreenFragment.java:
package layout;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import com.alexoladele.testingshit.MainScreenFragment;
import com.alexoladele.testingshit.R;
// Declares WelcomeScreenFragment as subclass of fragment
public class WelcomeScreenFragment extends Fragment {
private static final String TAG = "WelcomeScreenFragment";
private Button startBtn;
// +++++++++++++++++++++ OVERRIDE - METHODS ++++++++++++++++++++++++++
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
// Attaches the Fragment
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
// Creates Fragment view for use
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_welcome_screen, container, false);
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
startBtn = (Button) view.findViewById(R.id.start_screen_btn);
startBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fm = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction
.replace(R.id.root_layout, MainScreenFragment.newInstance())
.addToBackStack(null)
.commit();
}
});
}
// +++++++++++++++++++++ User METHODS ++++++++++++++++++++++++++
// Method to create new instances of Fragment
public static WelcomeScreenFragment newInstance() {
return new WelcomeScreenFragment();
}
}
fragment_welcome_screen.xml:
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/welcome_screen_root"
tools:context="layout.WelcomeScreenFragment">
<TextView
android:id="@+id/start_screen_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="176dp"
android:fontFamily="monospace"
android:text="@string/welcome_message"
android:textAlignment="center"
android:textSize="13sp"
android:textStyle="bold"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1"
app:layout_constraintHorizontal_bias="0.6" />
<android.support.v7.widget.AppCompatButton
android:id="@+id/start_screen_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/welcome_button"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="59dp"
app:layout_constraintTop_toBottomOf="@+id/start_screen_msg"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="0.5"
android:layout_below="@+id/start_screen_msg"
android:layout_centerHorizontal="true" />
</RelativeLayout>
MainScreenFragment.java:
package com.alexoladele.testingshit;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MainScreenFragment extends Fragment {
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return super.onCreateView(inflater, container, savedInstanceState);
}
public static MainScreenFragment newInstance() {
return new MainScreenFragment();
}
}
fragment_main_screen.xml:
<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/main_screen_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.alexoladele.testingshit.MainScreenFragment">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="163dp"
android:text="@string/main_screen_tempmsg"
android:textAlignment="center"
android:visibility="visible"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_constraintTop_creator="1" />
</RelativeLayout>
答案 0 :(得分:3)
你没有夸大你的fragment_main_screen
xml!
在MainScreenFragment.java
做那个
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main_screen, container, false);
return view;
}
答案 1 :(得分:1)
在创建片段时,我们需要附加片段xml,就像我们在activity中附加一样,但方法不同。
在MainScreenFragment.java中添加以下代码:
makeUp()
答案 2 :(得分:0)
为MainScreenFragment扩充布局,就像您为WelcomeScreenFragment所做的那样。