我可以将活动转换为片段吗?

时间:2015-08-17 19:16:55

标签: android android-activity android-viewpager fragment

如何将此活动转换为片段?我已经转换了一些部分但我得到了错误,这是我的实际代码,任何人都可以提供帮助来纠正我,

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;
            }
        }

2 个答案:

答案 0 :(得分:1)

  1. 不要使用managedQuery。请使用LoaderManager替换数据库查询。我相信managedQuery依赖于活动,所以如果你仍然想要使用它,请尝试使用getActivity()。managedQuery ....
  2. 替换"这个"使用getActivity();除非您需要Fragment上下文。大多数情况需要活动上下文,因此请使用getActivity。
  3. 需要在onCreateView内部充气的视图上执行findViewById。我建议覆盖onViewCreated()方法来执行所有findViewById调用。注意!在传递给这个方法的View上调用findViewById()!!这是你的片段布局!

答案 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;
            }
        }
    }