在滑动视图中更新对象

时间:2014-05-26 17:24:44

标签: android android-fragments swipeview

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

我尝试通过DemoCollectionPagerAdapter将一个包中的对象作为serialisable传递,然后再次传递给片段,但这看起来真的很麻烦。我还尝试在主要活动中声明对象并在片段类中引用它,但我抱怨它在静态上下文中不能有非静态引用。

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;
}
}

1 个答案:

答案 0 :(得分: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
}


}