1

因此,我一直在阅读有关 Android 中的对话框的内容,并且很好奇如何在我当前的代码中实现这一点。

我按照 android文档创建了自定义布局。我将它创建为一个新类。

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;

我将如何在那里实现这一点。

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

谢谢!

4

1 回答 1

0

首先,尝试继承 FragmentPagerAdapter 或 FragmentStatePagerAdapter 而不是 PagerAdapter。

在您的 Fragment(State)PagerAdapter 的“getItem(...)”中,您返回一个包含您提到的“addSiteButton”按钮的片段。

您的 Fragment 声明了一个回调接口:

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 实现回调:

public class FieldsActivity extends FragmentActivity implements MyFragment.Callback {

  ....
  ....
  @Override
  public void showDialog() {
    // Put the code you already have to create the dialogFragment.
  }
}
于 2013-02-04T21:04:36.937 回答