我有一个应用程序,其中一个Fragment
上有一个微调器,其中包含数字2,4,6,8,10。
当我选择一个号码时,我想要进行Fragment
次交易,另一个Fragment
则需要GridView
来替换它。
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);
}
}
答案 0 :(得分:0)
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;
}
}
答案 1 :(得分:0)
我发现我需要在制作片段时将字符串传入包中,而不是从片段中调用方法。
@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"));
}