如何将此活动转换为片段?

时间:2014-05-28 19:45:45

标签: android gridview android-activity fragment

我发现这段代码完全符合我的应用需求,但它作为活动运行。

我需要更改哪些内容才能将其转换为片段?因为在将它扩展为Fragment而不是Activity时只有3个错误,我觉得转换起来并不太难?

我真的需要使用这段代码,但它必须是片段,因为我的应用程序使用导航抽屉创建新的片段作为视图。

public class MainActivity extends Activity {

private GridView photoGrid;
private int mPhotoSize, mPhotoSpacing;
private ImageAdapter imageAdapter;

// Some items to add to the GRID
private static final String[] CONTENT = new String[] { 
        "Test Item" };

private static final int[] ICONS = new int[] {R.drawable.test };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // get the photo size and spacing
    mPhotoSize = getResources().getDimensionPixelSize(R.dimen.photo_size);
    mPhotoSpacing = getResources().getDimensionPixelSize(R.dimen.photo_spacing);

    // initialize image adapter
    imageAdapter = new ImageAdapter();

    photoGrid = (GridView) findViewById(R.id.albumGrid);

    // set image adapter to the GridView
    photoGrid.setAdapter(imageAdapter);

    // get the view tree observer of the grid and set the height and numcols dynamically
    photoGrid.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (imageAdapter.getNumColumns() == 0) {
                final int numColumns = (int) Math.floor(photoGrid.getWidth() / (mPhotoSize + mPhotoSpacing));
                if (numColumns > 0) {
                    final int columnWidth = (photoGrid.getWidth() / numColumns) - mPhotoSpacing;
                    imageAdapter.setNumColumns(numColumns);
                    imageAdapter.setItemHeight(columnWidth);

                }
            }
        }
    });
}

// ///////// ImageAdapter class /////////////////
public class ImageAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private int mItemHeight = 0;
    private int mNumColumns = 0;
    private RelativeLayout.LayoutParams mImageViewLayoutParams;

    public ImageAdapter() {
        mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mImageViewLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);
    }

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

    // set numcols
    public void setNumColumns(int numColumns) {
        mNumColumns = numColumns;
    }

    public int getNumColumns() {
        return mNumColumns;
    }

    // set photo item height
    public void setItemHeight(int height) {
        if (height == mItemHeight) {
            return;
        }
        mItemHeight = height;
        mImageViewLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, mItemHeight);
        notifyDataSetChanged();
    }

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

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

    public View getView(final int position, View view, ViewGroup parent) {

        if (view == null)
            view = mInflater.inflate(R.layout.photo_item, null);

        ImageView cover = (ImageView) view.findViewById(R.id.cover);
        TextView title = (TextView) view.findViewById(R.id.title);

        cover.setLayoutParams(mImageViewLayoutParams);

        // Check the height matches our calculated column width
        if (cover.getLayoutParams().height != mItemHeight) {
            cover.setLayoutParams(mImageViewLayoutParams);
        }

        cover.setImageResource(ICONS[position % ICONS.length]);
        title.setText(CONTENT[position % CONTENT.length]);

        return view;
    }
}

}

1 个答案:

答案 0 :(得分:0)

您需要扩展Fragment而不是Activity,然后使用onCreateView方法返回视图,而不是onCreate方法。您需要包含以下代码以将片段与其xml组合在一起。

View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);

然后继续您的代码。每当你有一个小部件时,它应该像这个例子一样实现:

TextView info = (TextView) getView().findViewById(R.id.Address);

最后,如果您有任何其他显示错误的代码,请在带下划线的代码前加上getActivity().

以上所有内容只需要对MainActivity类进行,而不是对适配器进行。