如何将此活动转换为片段?我已经转换了一些部分但我得到了错误,这是我的实际代码,任何人都可以提供帮助来纠正我,
public class GalleryActivity extends Fragment {
public Cursor cursor;
/*
* Column index for the Thumbnails Image IDs.
*/
public int columnIndex;
String imagePath;
MenuItem mSelectButton;
int mSelectSwitch;
private static final int MIN_DISTANCE = 100;
private static final int MAX_OFF_PATH = 200;
private static final int THRESHOLD_VELOCITY = 100;
private GestureDetector mGestureDetector;
private Context context;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_gallery, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] projection = {MediaStore.Images.Thumbnails._ID};
//managedQuery causes an error
cursor = managedQuery( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,projection,null,null,
MediaStore.Images.Thumbnails.IMAGE_ID);
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
//findViewById causes an error
GridView GalleryImages = (GridView) findViewById(R.id.galleryGrid1);
GalleryImages.setAdapter(new GalleryAdapter(this));
// new GalleryAdapter(this) causes an error
GalleryImages.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
mSelectButton.setVisible(true);
String[] imgData = {MediaStore.Images.Media.DATA};
cursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,imgData, null,
null,
null);
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToPosition(position);
imagePath = cursor.getString(columnIndex);
}
});
}
private class GalleryAdapter extends BaseAdapter {
private Context context;
public GalleryAdapter(Context localContext) {
context = localContext;
}
public int getCount() {
return cursor.getCount();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView picturesView;
if (convertView == null) {
picturesView = new ImageView(context);
// Move cursor to current position
cursor.moveToPosition(position);
// Get the current value for the requested column
int imageID = cursor.getInt(columnIndex);
// Set the content of the image based on the provided URI
picturesView.setImageURI(Uri.withAppendedPath(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID));
picturesView.setScaleType(ImageView.ScaleType.CENTER_CROP);
picturesView.setLayoutParams(new GridView.LayoutParams(470,470));
}
else {
picturesView = (ImageView)convertView;
}
return picturesView;
}
}
答案 0 :(得分:1)
答案 1 :(得分:1)
这是完整的代码:
public class GalleryActivity extends Fragment {
public Cursor cursor;
/*
* Column index for the Thumbnails Image IDs.
*/
public int columnIndex;
String imagePath;
MenuItem mSelectButton;
int mSelectSwitch;
private static final int MIN_DISTANCE = 100;
private static final int MAX_OFF_PATH = 200;
private static final int THRESHOLD_VELOCITY = 100;
private GestureDetector mGestureDetector;
private Context context;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_gallery, container, false);
String[] projection = {MediaStore.Images.Thumbnails._ID};
//managedQuery causes an error
cursor = getActivity().managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, null, null,
MediaStore.Images.Thumbnails.IMAGE_ID);
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
//findViewById causes an error
GridView GalleryImages = (GridView) view.findViewById(R.id.galleryGrid1);
GalleryImages.setAdapter(new GalleryAdapter(getActivity()));
// new GalleryAdapter(this) causes an error
GalleryImages.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mSelectButton.setVisible(true);
String[] imgData = {MediaStore.Images.Media.DATA};
cursor = getActivity().managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imgData, null,
null,
null);
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToPosition(position);
imagePath = cursor.getString(columnIndex);
}
});
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
private class GalleryAdapter extends BaseAdapter {
private Context context;
public GalleryAdapter(Context localContext) {
context = localContext;
}
public int getCount() {
return cursor.getCount();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView picturesView;
if (convertView == null) {
picturesView = new ImageView(context);
// Move cursor to current position
cursor.moveToPosition(position);
// Get the current value for the requested column
int imageID = cursor.getInt(columnIndex);
// Set the content of the image based on the provided URI
picturesView.setImageURI(Uri.withAppendedPath(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID));
picturesView.setScaleType(ImageView.ScaleType.CENTER_CROP);
picturesView.setLayoutParams(new GridView.LayoutParams(470, 470));
} else {
picturesView = (ImageView) convertView;
}
return picturesView;
}
}
}