创建新的活动时,不能添加片段 [英] Can't add a fragment when Creating a new Activity

查看:188
本文介绍了创建新的活动时,不能添加片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要创建一个向导一样的Andr​​oid应用程序,我想创建一个活动,两个动态片段,在创建活动时,第一个将被添加,第二时在一个按钮,用户点击第一个片段,现在我不能连片段添加到活动:

i'm trying to create a wizard Like Android application, i want to Create an activity and Two dynamic Fragments, the first one will be added when the Activity is created, and the second when the user clicks on a button in the First fragment, right now i can't even add the Fragment to the activity :

活动onCreate方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Fragment fr  = new FragmentNumber();

    getSupportFragmentManager().beginTransaction().add(fr, "number_fragment").commit();


}

这是我的活动code,当我运行这一点,屏幕是空白的。

this is my activity code, when i run this, the screen is blank.

在R.layout.activity_main指一个空的线性布局,我不希望添加的片段那里,因为我需要他们是动态的。

the R.layout.activity_main refer to an empty Linear Layout, i don't want to add the fragments there because i need them to be dynamic.

先谢谢了。

编辑:粘贴多个文件

activity_main.xml中

<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" >

<TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="hello from main"
    />

</FrameLayout>

MaicActivity.java

package com.example.fragmenttraining;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

公共类MainActivity扩展ActionBarActivity {

public class MainActivity extends ActionBarActivity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if(findViewById(android.R.id.content) != null)
    {
        Log.d("main activity", "content found");
    }

    FragmentNumber fr  = new FragmentNumber();

    //getSupportFragmentManager().beginTransaction().add(android.R.id.content, fr, "number_fragment").commit();
    getSupportFragmentManager().beginTransaction().replace(android.R.id.content, fr).commit();

}
FragmentNumber numberFragment;
FragmentFacebook facebookFragment;
public void facebookClicked(View view)
{
    numberFragment = new FragmentNumber();
    numberFragment.facebookClicked(view);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

现在它的工作,但片段没有更换,它显示的activity_main +的FragmentNumber片段的内容的内容甚至ID我更换...

Now it's working, but the fragment is not replaced, it displays the content of the activity_main + the content of the FragmentNumber fragment even id i replace it ...

推荐答案

请按照下列步骤操作:


  1. 创建主(活动布局)文件。在这里面,添加一个框架布局,将作为您的碎片的容器。

  2. 现在,创建两个片段。这包括创建,将您的片段的 onCreateView 方法内被夸大两个XML文件。

  3. 您的一个片段(第一个)应该有一个按钮,用户将能够点击。也就是说,你必须通过ID找到它后附加一个onClick监听器,它onCreateView方法中。

  4. 现在,创建您的第一个分片里面一个接口,它添加了一个方法,你的活动应该实现后的界面覆盖。

  5. 当用户点击该按钮,OnClick方法里面,你应该调用接口方法通知click事件的活动。

  6. 活动里面,当方法被调用时,创建第二个片段的新实例,并添加它通过更换第一个视图 - 或者这取决于你是否在你的活动用两个窗格布局 - 在情况下,你只需要添加的片段。

  7. 记住之前简单地增加一个视图来检查,如果你的第一个片段存在。

  1. Create your main (activity layout) file. In it, add a frame layout which will act as a container for your fragments.
  2. Now create your two fragments. This involves creating two xml files that will be inflated inside your fragment's onCreateView method.
  3. One of your fragments (the first one) should have a button that the user will be able to click. That means you must attach an onClick listener to it inside the onCreateView method after finding it by id.
  4. Now create an interface inside your first fragment and add a method in it that your activity should override after implementing the interface.
  5. When the user clicks that button, inside onClick method, you should call the interface method to notify the activity of the click event.
  6. Inside the activity, when the method is called, create a new instance of the second fragment and add it to view by replacing the first one - or it depends on whether you are using two-pane layout in your activity - in that case, you just add the fragment.
  7. Remember to check if your fragment exists first before simply adding one to view.

我希望这些步骤能够帮助你。

I hope these steps help you.

样品code

public class WizardActivity extends Activity implements SecondFragment.OnButtonClickedListener
{
   private FirstFragment firstFragment;

   public void onCreate(Bundle saveInstanceState)
   {
       super.onCreate(saveInstanceState);

       setContentView(R.layout.main);

       firstFragment = new FirstFragment();

       setFragment(firstFragment, "firstFragment");
   }


   @Override
   public void loadSecondFragment()
   {
       SecondFragment secondFragment = new SecondFragment();

       setFragment(secondFragment, "secondFragment");
   }

   public void setFragment(Fragment frag, String tag)
   {
       FragmentManager fm = getFragmentManager();
       FragmentTransaction ft = fm.beginTransaction();

       Fragment fragment = getFragmentManager().findFragmentById(R.id.fragmentContainer);
       if(fragment == null)
       {
           ft.add(R.id.fragmentContainer, frag, tag);
       } else {
          ft.replace(R.id.fragmentContainer, frag, tag);
       }

       ft.addToBackStack(null);
       ft.commit()

   }

}

现在为主要布局的XML文件。

Now the xml file for main layout.

<LinearLayout ........>

    <!--add whatever you need here-->

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

</LinearLayout>

现在让我们来创建你的片段中的一个 - 第一个:

Now let us create one of your fragments - the first one:

FirstFragment.java

public class FirstFragment extends Fragment implements View.OnClickListener
{
    private Activity mActivity;

    @Override
    public void onAttach(Activity act)
    {
        super.onAttach(act);
        this.mActivity = act;

        /*Initialize whatever you need here*/
    }

    @Override
    public View onCreateView(LayoutInflator inflator, ViewGroup container, Bundle saveInstanceState)

    {
        View v = inflator.inflate(R.layout.first_fragment, container, false);

        Button button = (Button)v.findViewById(R.id.button);
        button.setOnClickListener(this);
    }


    @Override
    public void onClick(View v)
    {
        ((OnButtonClickListener), mActivity).loadSecondFragment();
    }

    public interface OnButtonClickListener
    {
        void loadSecondFragment();
    }
}

您应该能够只是创建第二个片段,并将它在活动加载一个按钮被点击时。

You should be able to just create the second fragment and have it loaded in the activity when a button is clicked.

祝你好运。

这篇关于创建新的活动时,不能添加片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆