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

そのため、多くの検索と読み取りを行った後、自分に合った優れたソリューションを見つけました。興味のある方のために、メイン アクティビティに実装されているフラグメント クラスにインターフェイスを作成しました。メソッドは、フラグメント クラスでボタンを押すことによって開始されました。このようにして、オブジェクト全体をフラグメントに渡す必要なく、変数をメイン クラスに渡すことができました。

したがって、私のクラスはほとんど同じで、これらのビットが追加されています。

そして、インターフェースを含むフラグメントクラス。フラグメントがアタッチされるアクティビティへの参照を取得する 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 に答える