Android 如何从我的主要活动中调用 Fragment [英] Android How to call Fragment from my Main Activity
问题描述
我对安卓开发很陌生.
就我而言,我创建了一个主要活动和一个片段.主要活动作为两个按钮.
in my case i have one main activity and one fragment created.Main activity as two buttons.
当我点击按钮时,我的片段应该加载.我该如何实现?
when i click on button, my fragment should load .how can i achieve that?
主要活动.XML
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment 2"
android:id="@+id/fragment_button_2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment 1"
android:id="@+id/fragment_button_1"
android:layout_above="@+id/fragment_button_2"
android:layout_alignLeft="@+id/fragment_button_2"
android:layout_alignStart="@+id/fragment_button_2"
android:layout_marginBottom="33dp" />
</RelativeLayout>
主要活动.java
package com.bentgeorge.fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button fragment_btn_1;
private Button fragment_btn_2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragment_btn_1 = (Button) findViewById(R.id.fragment_button_1);
fragment_btn_2 = (Button) findViewById(R.id.fragment_button_2);
}
@Override
public void onClick(View v) {
Fragment1 frag = new Fragment1();
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.mainlayout,frag,"Test Fragment");
transaction.commit();
}
}
fragment_fragment1.xml
fragment_fragment1.xml
<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="com.bentgeorge.fragment.Fragment1">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
</FrameLayout>
Fragment1.java
Fragment1.java
package com.bentgeorge.fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class Fragment1 extends Fragment {
public Fragment1() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment1, container, false);
}
}
没有做太多代码,因为我对互联网上可用的教程感到困惑.
Not done much codes as i am confused with internet available tutorials.
请帮我解决这个问题
谢谢&问候,
更新:
我做了一些更改,我的片段现在正在加载.ut 按钮仍然可见.如何隐藏按钮
i have done some changes and my fragment is getting loaded now. ut the buttons are still visible . How can i hide the buttons
推荐答案
更新:请像这样更改您的 MainActivity:
Updated: Please change your MainActivity as like this:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fragment fragment = new MainFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_frame, fragment, fragment.getClass().getSimpleName()).addToBackStack(null).commit();
}
}
像这样改变你的activity_main.xml:
Change your activity_main.xml as like this:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
请创建一个xml fragment_main.xml:
Please create an xml fragment_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment 2"
android:id="@+id/fragment_button_2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment 1"
android:id="@+id/fragment_button_1"
android:layout_above="@+id/fragment_button_2"
android:layout_alignLeft="@+id/fragment_button_2"
android:layout_alignStart="@+id/fragment_button_2"
android:layout_marginBottom="33dp" />
</RelativeLayout>
同时创建一个片段作为 MainFragment:
Also create a fragment as MainFragment:
public class MainFragment extends Fragment implements View.OnClickListener{
private Button fragment_btn_1;
private Button fragment_btn_2;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
fragment_btn_1 = (Button) view.findViewById(R.id.fragment_button_1);
fragment_btn_2 = (Button) view.findViewById(R.id.fragment_button_2);
fragment_btn_1.setOnClickListener(this);
fragment_btn_2.setOnClickListener(this);
return view;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.fragment_button_1:
Fragment fragment1 = new Fragment1();
moveToFragment(fragment1);
break;
case R.id.fragment_button_2:
Fragment fragment2 = new Fragment2();
moveToFragment(fragment2);
break;
}
}
private void moveToFragment(Fragment fragment) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_frame, fragment, fragment.getClass().getSimpleName()).addToBackStack(null).commit();
}
}
希望对你有帮助.如果您有任何疑问,请告诉我.
Hope this will helps you. If you had any queries, let me know.
这篇关于Android 如何从我的主要活动中调用 Fragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!