英文:
Fragments on my app are not appearing after button click
问题
我刚接触 Android 开发,我已经研究并阅读了大量关于片段(fragments)的信息,但似乎遇到了一点问题。在创建了一切并确保没有错误后,我的应用程序并不按照我想要的方式工作。在按钮点击时,我希望一个片段显示在屏幕上。我有两个按钮,因此有两个片段,但是我的按钮似乎不起作用。
这是我的布局:
<!-- 在这里放你的布局代码 -->
这是 Java 代码:
// 在这里放你的 Java 代码
这是片段 XML 代码:
<!-- 在这里放你的片段 XML 代码 -->
最后,这是片段 Java 代码:
// 在这里放你的片段 Java 代码
再次强调,当我点击这些按钮时,它们似乎没有任何反应。此外,如果有人愿意分享,我想知道如何在加载此活动时使我的一个片段成为默认片段,这样一开始不会只是一个带有两个按钮的空白屏幕。非常感谢大家的帮助!
英文:
I'm new to the android development game and I have researched and read tons of info on fragments, but I seem to have run into a bit of a snag. After creating everything and checking to make sure I had zero errors my app isn't working the way I'd like. On button click I would like for a fragment to show up on the screen. I have two buttons, so two fragments, however my buttons don't seem to work.
Heres my layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="35dp">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<Button
android:id="@+id/signin_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"
android:gravity="center"
android:text="Sign In"
android:textColor="#000000"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<Button
android:id="@+id/signup_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"
android:gravity="center"
android:text="Sign Up"
android:textColor="#000000"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:id="@+id/parent_layout"
>
<FrameLayout
android:id="@+id/signin_fragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
Heres The java code:
package com.example.projectconviencesplash;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class SigninMainActivity extends Fragment implements View.OnClickListener {
public SigninMainActivity() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.signinfragment, container, false);
Button signinBtn = (Button) rootView.findViewById(R.id.signin_button);
Button signupBtn = (Button) rootView.findViewById(R.id.signup_button);
signinBtn.setOnClickListener(this);
signupBtn.setOnClickListener(this);
return rootView;
}
@Override
public void onClick(View view) {
Fragment fragment = null;
switch (view.getId()) {
case R.id.signin_button:
fragment = new SigninTwoActivity();
replaceFragment(fragment);
break;
case R.id.signup_button:
fragment = new SignupTwoActivity();
replaceFragment(fragment);
break;
}
}
public void replaceFragment(Fragment someFragment) {
assert getFragmentManager() != null;
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.signin_fragment, someFragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
Here's the fragment xml:
<?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"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp">
<Button
android:id="@+id/dummy_button"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="0dp"
android:layout_weight="0"
android:background="@drawable/fb_button"
android:drawableStart="@drawable/fb"
android:drawablePadding="-70dp"
android:paddingStart="70dp"
android:text="@string/fb_Title"
android:textColor="#FFFFFF"
android:textSize="18sp" />
<Button
android:id="@+id/dummy_button"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="0dp"
android:layout_weight="0"
android:background="@drawable/g_button"
android:drawableStart="@drawable/g"
android:drawablePadding="-80dp"
android:paddingStart="80dp"
android:text="@string/g_Title"
android:textColor="#FFFFFF"
android:textSize="18sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp">
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:layout_marginStart="0dp"
android:layout_marginLeft="0dp"
android:layout_marginEnd="0dp"
android:layout_marginRight="0dp"
android:src="@drawable/strike" />
<TextView
android:layout_width="149dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:layout_marginStart="0dp"
android:layout_marginLeft="0dp"
android:layout_marginEnd="0dp"
android:layout_marginRight="0dp"
android:text="@string/email_Title"
android:textColor="#cccccc"
android:textSize="16sp"
android:textStyle="" />
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:layout_marginStart="0dp"
android:layout_marginLeft="0dp"
android:layout_marginEnd="0dp"
android:layout_marginRight="0dp"
android:src="@drawable/strike" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="85dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:gravity="start"
android:text="@string/email"
android:textColor="#000000"
android:textSize="18sp"
android:textStyle="" />
<EditText
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="10dp"
android:layout_weight="1.5"
android:background="@drawable/edittextbackground"
android:inputType="text" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="85dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:gravity="start"
android:text="@string/password"
android:textColor="#000000"
android:textSize="18sp"
android:textStyle="" />
<EditText
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="10dp"
android:layout_weight="1.5"
android:background="@drawable/edittextbackground"
android:inputType="text"/>
</LinearLayout>
<Button
android:id="@+id/dummy_button"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="0dp"
android:layout_weight="0"
android:background="@drawable/rounded_corners"
android:drawablePadding="-10dp"
android:paddingStart="30dp"
android:text="@string/signin"
android:textColor="#FFFFFF"
android:textSize="18sp" />
</LinearLayout>
Lastly, here is the fragment java:
package com.example.projectconviencesplash;
import androidx.fragment.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.app.FragmentManager;
public class SigninTwoActivity extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.signinfragment, container, false);
}
}
Again, when I click on these buttons they don't seem to do anything. Also, if anyone is feeling particularly generous, I'd love to know how to make one of my fragments be default when loading this activity so that its not just a blank screen with two buttons when first loading. I appreciate all the help on this guys!
答案1
得分: 0
使用 getChildFragmentManager
替代 getFragmentManager
,当你想在一个片段内替换另一个片段时。
英文:
Use getChildFragmentManager
instead of getFragmentManager
when you want replace fragment inside a fragment.
答案2
得分: 0
activity_main.xml
这个 FrameLayout 将容纳片段,注意 ID 为 fragment_container。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
MainActivity
这个活动应该扩展 AppCompatActivity。
public class MainActivity extends AppCompatActivity {
/**
* 当活动创建时调用
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 对片段一对象的引用
FragmentOne fragOne = new FragmentOne();
// 对片段事务的引用。
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
// 片段事务
fragmentTransaction.add(R.id.fragment_container, fragOne)
.addToBackStack(null) // 将片段添加到返回堆栈,用于后退导航。
.commit(); // 完成片段事务。
}
}
fragment_one.xml
这里没有特别的地方。
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat 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"
tools:context=".FragmentOne"
android:orientation="vertical">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="片段一"
android:textSize="32sp"
tools:layout_editor_absoluteX="143dp" />
<Button
android:id="@+id/btn_fragment_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="片段二"
tools:layout_editor_absoluteY="401dp" />
<Button
android:id="@+id/btn_fragment_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="片段三"
tools:layout_editor_absoluteY="401dp" />
</androidx.appcompat.widget.LinearLayoutCompat>
FragmentOne
public class FragmentOne extends Fragment {
/**
* 当片段加载其用户界面时调用
*/
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_one, container, false);
// 对片段的引用
final FragmentTwo fragTwo = new FragmentTwo();
final FragmentThree fragThree = new FragmentThree();
// 创建片段事务,getActivity 返回片段中活动的上下文。
final FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
// 引用按钮视图。
Button loadFragTwoBtn = v.findViewById(R.id.btn_fragment_two);
// 设置点击侦听器
loadFragTwoBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
transaction.replace(R.id.fragment_container, fragTwo)
.addToBackStack(null)
.commit();
}
});
Button loadFragThreeBtn = v.findViewById(R.id.btn_fragment_three);
loadFragThreeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
transaction.replace(R.id.fragment_container, fragThree)
.addToBackStack(null)
.commit();
}
});
return v;
}
}
fragment_two.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FragmentTwo">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="片段二"
android:textSize="32sp" />
</FrameLayout>
FragmentTwo
public class FragmentTwo extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_two, container, false);
return view;
}
}
fragment_three.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FragmentThree">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="片段三"
android:textSize="32sp" />
</FrameLayout>
FragmentThree
public class FragmentThree extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_three, container, false);
return view;
}
}
希望这对您有所帮助,我还强烈建议您完成片段代码实验,以帮助建立坚实的基础。
附加注意:
在为按钮提供 ID 时,ID 必须是唯一的,在您的布局中,所有的按钮都使用了相同的 ID。
英文:
I'm going to use a more basic code as it will be easier to follow along and hopefully help when it comes to implementing it in other projects. The code I provided below will have a MainActivity, FragmentOne, FragmentTwo, FragmentThree. The MainActivity will load FragmentOne on startup, FragmentOne will have two buttons one to load FragmentTwo and one to load FragmentThree.
activity_main.xml
This FrameLayout will hold the fragments, note the ID fragment_container.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
MainActivity
The activity should extend AppCompatActivity
public class MainActivity extends AppCompatActivity {
/**
* Called when the activity is created
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Reference to Fragment one object
FragmentOne fragOne = new FragmentOne();
// Reference to fragment transaction.
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
// Fragment Transaction
fragmentTransaction.add(R.id.fragment_container, fragOne)
.addToBackStack(null) // Adds fragment to back stack, for backward navigation.
.commit(); // Finishes the fragment transaction.
}
}
fragment_one.xml
Nothing special here.
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat 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"
tools:context=".FragmentOne"
android:orientation="vertical">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment One"
android:textSize="32sp"
tools:layout_editor_absoluteX="143dp" />
<Button
android:id="@+id/btn_fragment_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment two"
tools:layout_editor_absoluteY="401dp" />
<Button
android:id="@+id/btn_fragment_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment three"
tools:layout_editor_absoluteY="401dp" />
</androidx.appcompat.widget.LinearLayoutCompat>
FragmentOne
public class FragmentOne extends Fragment {
/**
* Called when the fragment loads its user interface
*/
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_one,container, false);
// Reference the fragments
final FragmentTwo fragTwo = new FragmentTwo();
final FragmentThree fragThree = new FragmentThree();
// Create FragmentTransaction, getActivity returns context of the activity in a fragment.
final FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
// Reference Button view.
Button loadFragTwoBtn = v.findViewById(R.id.btn_fragment_two);
// Set clickListener
loadFragTwoBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
transaction.replace(R.id.fragment_container, fragTwo)
.addToBackStack(null)
.commit();
}
});
Button loadFragThreeBtn = v.findViewById(R.id.btn_fragment_three);
loadFragThreeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
transaction.replace(R.id.fragment_container, fragThree)
.addToBackStack(null)
.commit();
}
});
return v;
}
}
fragment_two.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FragmentTwo">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Fragment Two"
android:textSize="32sp"/>
</FrameLayout>
FragmentTwo
public class FragmentTwo extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_two, container, false);
return view;
}
}
fragment_three
public class FragmentTwo extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_two, container, false);
return view;
}
}
FragmentThree
public class FragmentThree extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_three, container, false);
return view;
}
}
Hope this is helpful, I would also strongly recommend doing the fragment code labs to help build a strong foundation.
Additional note,
When providing an ID for a button the ID has to be unique, in your layout, all of your buttons use the same ID.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论