在Android中与ViewPager和Dialogs进行通信

时间:2013-02-04 17:38:12

标签: android android-layout android-viewpager android-button android-dialogfragment

所以我一直在阅读Android中的Dialogs,并对如何将其实现到我当前的代码感到好奇。

我跟着android documentation创建了一个自定义布局。我把它创建为一个新类。

public class AddSiteDialog extends DialogFragment {

public Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    LayoutInflater inflater = getActivity().getLayoutInflater();

    builder.setView(inflater.inflate(R.layout.main, null))

            .setPositiveButton(R.string.signin, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    //To change body of implemented methods use File | Settings | File Templates.
                }
            })

            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int id) {
                    AddSiteDialog.this.getDialog().cancel();
                }
            });

    return builder.create();
}

}

现在,我希望在单击ViewPager布局内的按钮时显示该对话框。在另一个班级。 public class FieldsActivity extends Activity {

我有ViewPager。

private class MyPagerAdapter extends PagerAdapter {

    final ViewPager mPager = (ViewPager) findViewById(R.id.fieldspager);

    public int getCount() {
        return 3;
    }

    public Object instantiateItem(View collection, int position) {

        LayoutInflater inflater = (LayoutInflater) collection.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        ListView lv = (ListView)findViewById(android.R.id.list);

        final ArrayList<String> siteList = new ArrayList<String>();
        final ArrayAdapter<String> aa;
        aa = new ArrayAdapter<String>(getBaseContext(), R.layout.list_row, siteList);

        if(lv == null) Log.v("lv", "null");
        else Log.v("aa", "null");

        if (lv != null) {
            lv.setAdapter(aa);
        }
        int resId = 0;
        View view = null;
        switch (position) {
            case 0:
                resId = R.layout.field01;
                view = inflater.inflate(resId, null);
                ((ViewPager) collection).addView(view, 0);

                return view;

            case 1:
                resId = R.layout.add_site;
                view = inflater.inflate(resId, null);
                Button addSiteButton = (Button)view.findViewById(R.id.addSiteButton);
                ((ViewPager) collection).addView(view, 0);
                addSiteButton.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                     //I want to click this button and the dialog show up
                    }
                });
                return view;

我将如何在那里实现。

我对此很陌生,所以我想弄清楚事情是如何相互交谈的。

谢谢!

1 个答案:

答案 0 :(得分:0)

首先,尝试子类化FragmentPagerAdapter或FragmentStatePagerAdapter而不是PagerAdapter。

在你的片段(状态)PagerAdapter的'getItem(...)'中,你返回一个包含你提到的'addSiteButton'按钮的片段。

你的Fragment声明了一个Callback接口:

public class MyFragment extends Fragment { 
  ...
  ...
  public interface Callback {
      public void showDialog();
  }

  private Callback mCallback;


  @Override
  public void onAttach(Activity activity) {
    callback = (Callback)activity;
    super.onAttach(activity);
  }

  @Override
  public void onDetach() {
    callback = null;
    super.onDetach();
  }

  ....
  ....
      // Onclick handler of 'addSiteButton'.
      onClick(View view) {
        if (callback != null) {
          callback.showDialog();
        }
      }
  ....
}

确保您的Activity实现Callback:

public class FieldsActivity extends FragmentActivity implements MyFragment.Callback {

  ....
  ....
  @Override
  public void showDialog() {
    // Put the code you already have to create the dialogFragment.
  }
}