1

所以我一直在閱讀關於Android中的對話框,並且很好奇如何在當前代碼中實現此功能。在Android中使用ViewPager和對話框進行通信

我跟着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; 

我將如何實現這一點。

我在這方面很新穎,所以我想弄清楚事情是如何相互交流的。

謝謝!

回答

0

首先,嘗試子類化FragmentPagerAdapter或FragmentStatePagerAdapter而不是PagerAdapter。

在'getItem(...)'你的Fragment(狀態)PagerAdapter中,你返回一個包含你提到的'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(); 
     } 
     } 
    .... 
} 

,並確保您的活動實現回調:

public class FieldsActivity extends FragmentActivity implements MyFragment.Callback { 

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