ListFragment as DialogFragment

前端 未结 2 556
误落风尘
误落风尘 2021-01-25 05:19

Is it possible to show ListFragment as Dialog? Or there\'s no way and I should implement my own ListView, empty TextView and

相关标签:
2条回答
  • 2021-01-25 06:08

    Another Option:

    Build Dialog Fragment:

    import android.app.AlertDialog;
    import android.app.Dialog;
    import android.content.DialogInterface;
    import android.os.Bundle;
    import android.support.v4.app.DialogFragment;    
    
    public class ListDialogFragment extends DialogFragment {
    
        private OnListDialogItemSelect listener;
        private String title;
        private String[] list;
    
        public ListDialogFragment(OnListDialogItemSelect listener, String[] list, String title) {
            this.listener=listener;
            this.list=list;
            this.title=title;
        }
    
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
    
            return new AlertDialog.Builder(getActivity())
                    .setTitle(title)
                    .setCancelable(false)
                    .setItems(list, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int item) {
    
                            listener.onListItemSelected(list[item]);
                            getDialog().dismiss(); 
                            ListDialogFragment.this.dismiss();
    
                        }
                    }).create();
    
        }
    
        public interface OnListDialogItemSelect{
            public void onListItemSelected(String selection);
        }
    
    }
    

    On Your Fragment Activity like this:

    public class YourActivity extends FragmentActivity implements OnListDialogItemSelect{
    
        private void showCountryFragment(){
             FragmentManager fm = getSupportFragmentManager();
             ListDialogFragment newFragment = new ListDialogFragment(this,getCountries(),"Country:");
             newFragment.show(fm, "country_picker");
        }
    
        @Override
        public void onListItemSelected(String selection) {
            _bt_country.setText(selection);
        }
    
    }
    
    0 讨论(0)
  • 2021-01-25 06:18

    I am not very sure whether it works with ListFragment or not but we can show an activity as Dialog by applying a theme to activity in manifest file as:

    <activity android:theme="@android:style/Theme.Dialog" />
    

    Please try with your ListFragment, and let me know if it works.

    Thank you.

    0 讨论(0)
提交回复
热议问题