0

我有两个片段,我们称它们为片段 A 和片段 B,它们是 NavigationDrawer 的一部分(这是它们绑定到的活动)。在 Fragment AI 中有一个按钮。按下此按钮时,我希望将另一个项目添加到 Fragment B 中的 ListView。

做这个的最好方式是什么?使用 Intents、SavedPreferences、公开某些东西(?)或其他东西?

编辑 5:20/7/13 这是最新的代码

这是NavigationDrawer我用来启动片段的:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Navigation_Drawer extends FragmentActivity {

    public DrawerLayout mDrawerLayout; // Creates a DrawerLayout called_.
    public ListView mDrawerList;
    public ActionBarDrawerToggle mDrawerToggle;

    private CharSequence mDrawerTitle;
    private CharSequence mTitle;
    private String[] mNoterActivities; // This creates a string array called _.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub

// Just setting up the navigation drawer

    } // End of onCreate

// Removed the menu


        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            selectItem(position);
        }
    }

    private void selectItem(int position) { 

        FragmentManager fragmentManager = getSupportFragmentManager();

        if (position == 0) {
            Fragment qnfragment = new QuickNoteFragment(); 
            ((FragmentBase) qnfragment).setContext(this);
            Bundle args = new Bundle(); // Creates a bundle called args
            args.putInt(QuickNoteFragment.ARG_nOTERACTIVITY_NUMBER, position); 

            qnfragment.setArguments(args);

            fragmentManager.beginTransaction()
                    .replace(R.id.content_frame, qnfragment).commit();



        } else if (position == 3) {
            Fragment pendViewPager = new PendViewPager(); // This is a ViewPager that includes HistoryFragment
            ((FragmentBase) pendViewPager).setContext(this);
            Bundle args = new Bundle();

            pendViewPager.setArguments(args);
            fragmentManager.beginTransaction()
                    .replace(R.id.content_frame, pendViewPager).commit();
        }

    // Update title etc
    }


    @Override
    protected void onPostCreate(Bundle savedInstanceState) { // Used for the NavDrawer toggle
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) { // Used for the NavDrawer toggle
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggles
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

}

这是 QuickNoteFragment:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class QuickNoteFragment extends FragmentBase implements OnClickListener {

    public static final String ARG_nOTERACTIVITY_NUMBER = "noter_activity";

    Button b_create;
// removed other defining stuff

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.quicknote, container, false);
        int i = getArguments().getInt(ARG_nOTERACTIVITY_NUMBER);
        String noter_activity = getResources().getStringArray(
                R.array.noter_array)[i];
        FragmentManager fm = getFragmentManager();
        setRetainInstance(true);

        b_create = (Button) rootView.findViewById(R.id.qn_b_create);

        // Removed other stuff

        getActivity().setTitle(noter_activity);
        return rootView;
    }

    @Override
    public void onClick(View v) {

        // TODO Auto-generated method stub

        switch (v.getId()) {

        case R.id.qn_b_create:

                              String data = "String data";
                DataModel.getInstance().addItem(data);

            break;
        }
    }

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // TODO Auto-generated method stub
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection

        }
    }

    public interface OnItemAddedHandler { // Srains code
        public void onItemAdded(Object data);
    }
}

这是 HistoryFragment (记住它是 a 的一部分ViewPager):

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.RiThBo.noter.QuickNoteFragment.OnItemAddedHandler;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class HistoryFragment extends FragmentBase implements OnItemAddedHandler {

    ListView lv;
    List<Map<String, String>> planetsList = new ArrayList<Map<String, String>>();
    SimpleAdapter simpleAdpt;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.history, container, false);
        initList();


        ListView lv = (ListView) view.findViewById(R.id.listView);
        simpleAdpt = new SimpleAdapter(getActivity(), planetsList,
                android.R.layout.simple_list_item_1, new String[] { "planet" },
                new int[] { android.R.id.text1 });

        lv.setAdapter(simpleAdpt);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> parentAdapter, View view,
                    int position, long id) {

                // We know the View is a TextView so we can cast it

                TextView clickedView = (TextView) view;

                Toast.makeText(
                        getActivity(),
                        "Item with id [" + id + "]  - Position [" + position
                                + "] - Planet [" + clickedView.getText() + "]",
                        Toast.LENGTH_SHORT).show();
            }

        });
        registerForContextMenu(lv);

        return view;

    }

    private void initList() {

        // We populate the planets

        planetsList.add(createPlanet("planet", "Mercury"));
        planetsList.add(createPlanet("planet", "Venus"));
        planetsList.add(createPlanet("planet", "Mars"));
        planetsList.add(createPlanet("planet", "Jupiter"));
        planetsList.add(createPlanet("planet", "Saturn"));
        planetsList.add(createPlanet("planet", "Uranus"));
        planetsList.add(createPlanet("planet", "Neptune"));

    }

    private HashMap<String, String> createPlanet(String key, String name) {

        HashMap<String, String> planet = new HashMap<String, String>();

        planet.put(key, name);

        return planet;

    }

    // We want to create a context Menu when the user long click on an item

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,

    ContextMenuInfo menuInfo) {

        super.onCreateContextMenu(menu, v, menuInfo);

        AdapterContextMenuInfo aInfo = (AdapterContextMenuInfo) menuInfo;

        // We know that each row in the adapter is a Map

        HashMap map = (HashMap) simpleAdpt.getItem(aInfo.position);

        menu.setHeaderTitle("Options for " + map.get("planet"));
        menu.add(1, 1, 1, "Details");
        menu.add(1, 2, 2, "Delete");

    }
     @Override
      public void onItemAdded(Object data) {
          // to add item
          String string = String.valueOf(data);
          Toast.makeText(getContext(), "Data: " + string, Toast.LENGTH_SHORT).show();
          planetsList.add(createPlanet("planet", string));
      }
      @Override
    public void onStart() {
          super.onStart();

          DataModel.getInstance().setOnItemAddedHandler(this);
      }
}

这是片段库:

   import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentActivity;

      public class FragmentBase extends Fragment {

            private FragmentActivity mActivity; // I changed it to FragmentActivity because Activity was not working, and my `NavDrawer` is a FragmentActivity.

            public void setContext(FragmentActivity activity) {
                mActivity = mActivity;
            }

            public FragmentActivity getContext() {
                return mActivity;
            }
    }

这是数据模型:

  import android.util.Log;
    import com.xxx.xxx.QuickNoteFragment.OnItemAddedHandler;

public class DataModel {

    private static DataModel instance;
    private static OnItemAddedHandler mOnItemAddHandler;

    public static DataModel getInstance() {
        if (null == instance) {
            instance = new DataModel();
        }
        return instance;
    }

    public void setOnItemAddedHandler(OnItemAddedHandler handler) {
        mOnItemAddHandler = handler;
    }

    public void addItem(Object data) {
        if (null != mOnItemAddHandler)
            mOnItemAddHandler.onItemAdded(data);
        else {
            Log.i("is context null?", "yes!");
        }  
    }
}

谢谢

4

3 回答 3

1

我建议您使用interfaceand MVC,这将使您的代码更易于维护。

首先你需要一个接口:

    public interface OnItemAddedHandler {
        public void onItemAdded(Object data);
    }

然后,您将需要一个数据模型:

    public class DataModel {

        private static DataModel instance;
        private static OnItemAddedHandler mOnItemAddHandler;

        public static DataModel getInstance() {
            if (null == instance) {
                instance = new DataModel();
            }
            return instance;
        }

        public void setOnItemAddedHandler(OnItemAddedHandler handler) {
            mOnItemAddHandler = handler;
        }

        public void addItem(Object data) {
            if (null != mOnItemAddHandler)
                mOnItemAddHandler.onItemAdded(data);
        }
    }

单击按钮时,您可以将数据添加到数据模型中:

    Object data = null;
    DataModel.getInstance().addItem(data);

然后,实现添加项目FragmentB的接口OnItemAddedHandler

    public class FragmentB implements OnItemAddedHandler {

        @Override
        public void onItemAdded(Object data) {
            // to add item
        }
    }

另外,FragmentB开始时,您应该将自己注册到 DataModel:

    public class FragmentB implements OnItemAddedHandler {

        @Override
        public void onItemAdded(Object data) {
            // to add item
        }

        @Override
        protected void onStart() {
            super.onStart();

            DataModel.getInstance().setOnItemAddedHandler(this);
        }
    }

您也可以添加DataModel.getInstance().setOnItemAddedHandler(this);onCreate方法中FragmentB

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    DataModel.getInstance().setOnItemAddedHandler(this);

    // do other things
}

更新

您可以简单地发送字符串:

    String data = "some string";
    DataModel.getInstance().addItem(data);

然后上FragementB

    public class FragmentB implements OnItemAddedHandler {

        @Override
        public void onItemAdded(Object data) {
            // get what you send into method DataModel.getInstance().addItem(data);
            String string = String.valueOf(data);
        }
    }

更新

好的。您已经添加DataModel.getInstance().setOnItemAddedHandler(this)onCreateView方法,因此无需在onStart()方法中添加它。您可以删除整个onStart方法。

onStart将在片段开始时调用,我们不需要在onCreateView. onItemAdded调用方法时会调用on DataModel.getInstance().addItem(data),我们也不需要调用它onCreateView

因此,您可以从方法中删除以下代码onCreateView

   DataModel.getInstance().setOnItemAddedHandler(this);
   // remove the methods below
   // onItemAdded(getView());
   // onStart();

您有另一个片段,其中有一个按钮,您可以在 clickhandler 函数中添加以下代码:

String data = "some string";
DataModel.getInstance().addItem(data);

更新3

我认为HistoryFragment你什么时候已经分离了QuickNoteFragment 你可以添加代码HistoryFragment来检查:

@Override
public void onDetach() {
    super.onDetach();
    Log.i("test", String.format("onDetach! %s", getActivity() == null));
}

更新4

我认为HistoryFragment并且QuickNoteFragment应该有一个父类,名为FragmentBase

    public class FragmentBase extends Fragment {

        private Activity mActivity;

        public void setContext(Activity activity) {
            mActivity = mActivity;
        }

        public Activity getContext() {
            return mActivity;
        }
    }

HistoryFragmentQuickNoteFragment扩展FragmentBase。然后当你在它们之间切换时,你可以调用setContext来设置一个Activity,比如:

private void selectItem(int position) {

    FragmentManager fragmentManager = getSupportFragmentManager();

    if (position == 0) {
        Fragment qnfragment = new QuickNoteFragment();
        qnfragment.setContext(this);

        // ...

    } else if (position == 1) {
        Fragment pagerFragment = new RemViewPager();
        pagerFragment.setContext(this);

        // ...
    }
}

HistoryFragment现在,我们可以通过调用来获得一个非空活动getContext,因此我们可以将onItemAdded方法更改为:

  @Override
  public void onItemAdded(Object data) {
      // to add item
      String string = String.valueOf(data);
      Toast.makeText(getContext(), "Data: " + string, Toast.LENGTH_SHORT).show();
      planetsList.add(createPlanet("planet", string));
  }

我希望这会奏效。

于 2013-07-19T10:54:40.487 回答
1

一些好的设计原则:

  • 一个活动可以知道关于它包含的任何片段的所有公共信息。
  • 片段不应该知道包含它的特定活动的任何信息。
  • 片段永远不应该知道父活动中可能包含或不包含的其他片段。

基于这些原则的建议方法(非正式设计模式)。

每个片段都应该声明一个由其父活动实现的接口:

public class MyFragment extends Fragment
{
   public interface Parent
   {
      void onMyFragmentSomeAction();
   }
   private Parent mParent;
   public onAttach(Activity activity)
   {
      mParent = (Parent) activity;
   }

   // This would actually be in a listener.  Simplifying to save typing.
   void onSomeButtonClick(View button)
   {
      mParent.onMyFragmentSomeAction();
   }
}

并且活动应该为其包含的所有片段实现适当的接口。

public class MyActivity extends Activity 
                        implements MyFragment.Parent,
                                   YourFragment.Parent,
                                   HisFragment.Parent
{
   [usual Activity code]
   void onMyFragmentSomeAction()
   {
     if yourFragment is showing
     {
        yourFragment.reactToSomeAction();
     }
     if hisFragment is showing
     {
        hisFragment.observeThatSomeActionHappened();
     }
     [etc]
}

广播方法也不错,但它相当重量级,它需要目标 Fragment 知道源 Fragment 将发送哪些广播。

于 2013-07-19T15:59:51.737 回答
0
  1. 使用广播。从 A 发送一个广播,B 将接收并处理它。
  2. 给 B 添加一个公共方法,例如 ,public void addListItem()它将向 B 中的列表视图添加数据。片段 A 尝试使用FragmentMananger.findFragmentByTag()并调用该方法找到片段 B 的实例。
于 2013-07-19T10:12:07.917 回答