gridview 和 Fragment 事务的问题

Trouble with gridview and Fragment transactions

我有一个应用程序,其中一个 Fragment 有一个微调器,其中有数字 2、4、6、8、10。

当我选择一个号码时,我希望发生 Fragment 交易,并用另一个 FragmentGridView 来替换它。

GridView 应该是 2x2、4x4 或我传递的任何格式。当我将它们放在同一个 Fragment 上时,它起作用了,但现在我将它们分开了,当我调用 changeNumber() 时,我的应用程序崩溃了。

我认为这是因为我在网格片段中创建 Gridview 的位置,但我现在有点迷路了。

 @Override
public void onFragmentInteraction(String number) {
    Grid grid = new Grid();
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.SpinnerFragment, grid, "Grid");
    fragmentTransaction.commit();
    grid.changeNumber(number);
}

    public class Grid extends Fragment {

    GridView gridView;
    int x;

    // TODO: Rename and change types and number of parameters
    public static Grid newInstance(String param1, String param2) {
        Grid fragment = new Grid();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    public Grid() {
        // Required empty public constructor
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        gridView = (GridView) getActivity().findViewById(R.id.gridView);

    }

    public void changeNumber(String number)
    {
        switch (number)
        {
            case "Two":
                x = 2;
                break;

            case "Four":
                x = 4;
                break;

            case "Six":
                x = 6;
                break;

            case "Eight":
                x = 8;
                break;

            case "Ten":
                x = 10;
                break;
        }
        gridView.setNumColumns(x);
        gridView.setAdapter(new CustomAdapter(getActivity(), number));
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_grid, container, false);
    }


}
Here is an example of creating a grid view 

This is how you can create gridview programmatically,

    GridView grid = new GridView(this);
    grid.setId(ViewIdentification.getId());
    grid.setLayoutParams(new GridView.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
    grid.setBackgroundColor(Color.WHITE);
    grid.setNumColumns(3);
    grid.setColumnWidth(GridView.AUTO_FIT);
    grid.setVerticalSpacing(5);
    grid.setHorizontalSpacing(5);
    grid.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
Add the above view to your layout. And here you can get the height and width of the display.

DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay()
            .getMetrics(displaymetrics);
int width = displaymetrics.widthPixels;
int height = displaymetrics.heightPixels;
And here is the adapter class :

public class ImageAdapter extends BaseAdapter {

    private Context mContext;
    private Bitmap[]mis_fotos;

    public ImageAdapter(Context c) {
        mContext = c;    }

    public int getCount() {
        return mis_fotos.length;
        }

    public Object getItem(int position) {
        return position;    }

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

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(width/3, height/3));
            imageView.setScaleType(ImageView.setScaleType(ScaleType.FIT_XY));
            imageView.setPadding(0, 0, 0, 0);
        } else {
            imageView = (ImageView) convertView;
        }
        imageView.setImageBitmap(mis_fotos[position]);
        return imageView;
    }
}

我发现我需要在制作片段时通过包传递字符串,而不是从片段调用方法。

 @Override
public void onFragmentInteraction(String number) {
    Grid grid = new Grid();
    Bundle bundle = new Bundle();
    bundle.putString("number", number);
    grid.setArguments(bundle);
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.SpinnerFragment, grid, "Grid");
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();

}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    gridView = (GridView) getActivity().findViewById(R.id.gridView);
    changeNumber(getArguments().getString("number"));
}