我只是开始使用对话框片段,还有很多我不知道的地方。我有一个MainActivity,通过单击一个按钮可以打开DialogFragment,在该DialogFragment中,我还有另一个可以打开SecondDialogFragment的按钮。第一个工作正常,但第二个工作不正常,我单击第一个DialogFragment中的按钮,屏幕失去了背景的焦点,但什么也没显示。我真的不知道怎么了如果有人可以帮我,我将不胜感激。

这是第一个DialogFragment,我用onClick称第二个。


  DialogFragment.java


 ImageButton iconButton = v.findViewById(R.id.user_icon);
            iconButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    new DSelectIcon().show(getFragmentManager(), "DSelectIcon");
                }
            });



  SecondDialogFragment.java


public class DSelectIcon extends DialogFragment{

    private View v = null;

    private ImageView Selection;
    private static final Integer[] items = { R.drawable.image1,
            R.drawable.image1, R.drawable.image1,
            R.drawable.image1, R.drawable.image1,
            R.drawable.image1, R.drawable.image1,
            R.drawable.image1 };



    public DSelectIcon() {
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        if (savedInstanceState != null) {
            // Restore last state for checked position.
        }
        LayoutInflater inflater = getActivity().getLayoutInflater();
        v = inflater.inflate(R.layout.grid_icon_event, null);
        return createDSelectIcon(v);
    }

    private AlertDialog createDSelectIcon(View v) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        Selection = v.findViewById(R.id.selection);
        GridView grid = v.findViewById(R.id.grid);
        // grid.setAdapter(new ArrayAdapter<Integer>(this, R.layout.cell,
        // items));
        grid.setAdapter(new CustomGridAdapter((MainActivity)getActivity(), items));
        grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                // TODO Auto-generated method stub
                Toast.makeText(getActivity(), "Clicked postion is" + i,
                        Toast.LENGTH_LONG).show();
                //Selection.setImageResource(items[arg2]);
            }
        });

        return builder.create();
    }

    public class CustomGridAdapter extends BaseAdapter {
        private Activity mContext;

        // Keep all Images in array
        public Integer[] mThumbIds;

        // Constructor
        public CustomGridAdapter(MainActivity mainActivity, Integer[] items) {
            this.mContext = mainActivity;
            this.mThumbIds = items;
        }

        @Override
        public int getCount() {
            return mThumbIds.length;
        }

        @Override
        public Object getItem(int position) {
            return mThumbIds[position];
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView = new ImageView(mContext);
            imageView.setImageResource(mThumbIds[position]);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
            return imageView;
        }

    }
}



  Grid_icon_event.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/selection"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <GridView
        android:id="@+id/grid"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:columnWidth="100dip"
        android:gravity="center"
        android:horizontalSpacing="5dip"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth"
        android:verticalSpacing="40dip" >
    </GridView>

</LinearLayout>

最佳答案

固定!!!

一个愚蠢的错误,但我忘了添加:

builder.setView(v);


在SecondDialogFragment.java方法中,createDSelectIcon

现在看起来像这样:

public class DSelectIcon extends DialogFragment{

    private View v = null;

    private ImageView Selection;
    private static final Integer[] items = { R.drawable.image1,
            R.drawable.image1, R.drawable.image1,
            R.drawable.image1, R.drawable.image1,
            R.drawable.image1, R.drawable.image1,
            R.drawable.image1 };



    public DSelectIcon() {
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        if (savedInstanceState != null) {
            // Restore last state for checked position.
        }
        LayoutInflater inflater = getActivity().getLayoutInflater();
        v = inflater.inflate(R.layout.grid_icon_event, null);
        return createDSelectIcon(v);
    }

    private AlertDialog createDSelectIcon(View v) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        Selection = v.findViewById(R.id.selection);
        GridView grid = v.findViewById(R.id.grid);
        // grid.setAdapter(new ArrayAdapter<Integer>(this, R.layout.cell,
        // items));
        grid.setAdapter(new CustomGridAdapter((MainActivity)getActivity(), items));
        grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                // TODO Auto-generated method stub
                Toast.makeText(getActivity(), "Clicked postion is" + i,
                        Toast.LENGTH_LONG).show();
                //Selection.setImageResource(items[arg2]);
            }
        });

        builder.setView(v);


        return builder.create();
    }

    public class CustomGridAdapter extends BaseAdapter {
        private Activity mContext;

        // Keep all Images in array
        public Integer[] mThumbIds;

        // Constructor
        public CustomGridAdapter(MainActivity mainActivity, Integer[] items) {
            this.mContext = mainActivity;
            this.mThumbIds = items;
        }

        @Override
        public int getCount() {
            return mThumbIds.length;
        }

        @Override
        public Object getItem(int position) {
            return mThumbIds[position];
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView = new ImageView(mContext);
            imageView.setImageResource(mThumbIds[position]);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
            return imageView;
        }

    }
}

09-25 23:21