0

我正在尝试从滑动视图中包含的片段更新对象。我的代码直接取自 Android 文档。我想要做的是将一个对象从主 CollectionDemoActivity 向下传递到 DemoObjectFragment 片段,使用该片段中的按钮对其进行更新,然后将其传递回主活动。实现这一目标的最佳方法是什么?

我尝试通过 DemoCollectionPagerAdapter 将包中的对象作为可序列化的对象传递,然后再向下传递到片段,但这似乎很麻烦。我还尝试在主要活动中声明该对象并仅在片段类中引用它,但我抱怨它在静态上下文中不能有非静态引用。

public class CollectionDemoActivity extends FragmentActivity {

// When requested, this adapter returns a DemoObjectFragment,
// representing an object in the collection.

DemoCollectionPagerAdapter mDemoCollectionPagerAdapter;
ViewPager mViewPager;

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_collection_demo);

    // ViewPager and its adapters use support library
    // fragments, so use getSupportFragmentManager.
    mDemoCollectionPagerAdapter =
            new DemoCollectionPagerAdapter(
                    getSupportFragmentManager());
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mDemoCollectionPagerAdapter);
}
}

// Since this is an object collection, use a FragmentStatePagerAdapter,
// and NOT a FragmentPagerAdapter.
public class DemoCollectionPagerAdapter extends FragmentStatePagerAdapter {
public DemoCollectionPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int i) {
    Fragment fragment = new DemoObjectFragment();
    Bundle args = new Bundle();
    // Our object is just an integer :-P
    args.putInt(DemoObjectFragment.ARG_OBJECT, i + 1);
    fragment.setArguments(args);
    return fragment;
}

@Override
public int getCount() {
    return 100;
}

@Override
public CharSequence getPageTitle(int position) {
    return "OBJECT " + (position + 1);
}
}

// Instances of this class are fragments representing a single
// object in our collection.
public static class DemoObjectFragment extends Fragment {
public static final String ARG_OBJECT = "object";

@Override
public View onCreateView(LayoutInflater inflater,
        ViewGroup container, Bundle savedInstanceState) {
    // The last two arguments ensure LayoutParams are inflated
    // properly.
    View rootView = inflater.inflate(
            R.layout.fragment_collection_object, container, false);
    Bundle args = getArguments();
    ((TextView) rootView.findViewById(android.R.id.text1)).setText(
            Integer.toString(args.getInt(ARG_OBJECT)));
    return rootView;
}
}
4

1 回答 1

0

因此,经过大量搜索和阅读后,我找到了一个适合我的不错的解决方案。对于那些感兴趣的人,我在片段类中创建了一个接口,该接口在 Main 活动中实现。这些方法是通过在片段类中按下按钮启动的。通过这种方式,我能够将变量向上传递给主类,而无需将整个对象向下传递给片段。

所以我的课程与添加的这些位基本相同:

以及包含接口的片段类。需要调用 onAttach() 方法,该方法获取对片段将附加到的活动的引用。此活动引用绑定到片段中的接口实例。

public class DemoObjectFragment extends Fragment {

....

//Creating the interface
public interface ButtonListener {
 //This method will be called in the main activity. Whatever is passed in as the parameter can be used by the main activity
    public void ButtonPressed(int myInt);
}

//Getting an instance of the interface
ButtonListener updateListener;


//Getting a reference to the main activity when the fragment is attached to it.
//The activity reference is bound to the instance of the interface.
@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    // Ensures the activity implements the callback interface
    try {
        updateListener = (DayUpdateButtonListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString());
    }
}

....

//On the button click call the method through the activity reference from the onAttach() method
//Creating an int object to pass into the method.
int myNewInt = 5;

myButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            updateListener.ButtonPressed(myNewInt);
        }

    });

}

最后在主活动中简单地实现接口并从中添加方法。

public class CollectionDemoActivity extends FragmentActivity implements DemoObjectFragment.ButtonListener {

....

@Override
public void ButtonPressed(int myInt) {
//Update the object with myInt
}


}
于 2014-05-28T16:57:24.487 回答