1

フラグメントから別のアクティビティを開始しようとすると問題が発生します。

私が抱えている問題は、設定したボタンをクリックしても何も起こらないことです。

1 つのレイアウト、フラグメント 1 およびフラグメント 2 を使用する 2 つのフラグメント クラスがあります (以下を参照)。フラグメント 2 で定義されたボタンから新しいアクティビティを開始しようとしています。

フラグメント 1 クラスは次のとおりです。

public class TabsStuffFragment extends Fragment {

View view;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    view = inflater.inflate(R.layout.image_grid_fragment, container, false);
    Button test = (Button) view.findViewById(R.id.button1);
    test.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {

            Intent intent = new Intent(getActivity(), Test.class);
            startActivity(intent);

        }
    });

    return view;

}

} 

開こうとしているアクティビティ:

public class Test extends Activity {

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

}

レイアウト:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<GridView
    android:id="@+id/gridView"
    style="@style/PhotoGridLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:columnWidth="@dimen/image_thumbnail_size"
    android:horizontalSpacing="@dimen/image_thumbnail_spacing"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth"
    android:verticalSpacing="@dimen/image_thumbnail_spacing" >
</GridView>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="BUTTON" 
    android:clickable="true"/>

</RelativeLayout>

フラグメント 2:

public class ImageGridFragment extends Fragment implements
    AdapterView.OnItemClickListener {
private static final String TAG = "ImageGridFragment";
private static final String IMAGE_CACHE_DIR = "thumbs";

private int mImageThumbSize;
private int mImageThumbSpacing;
private ImageAdapter mAdapter;
private ImageFetcher mImageFetcher;




@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);

    mImageThumbSize = getResources().getDimensionPixelSize(
            R.dimen.image_thumbnail_size);
    mImageThumbSpacing = getResources().getDimensionPixelSize(
            R.dimen.image_thumbnail_spacing);

    mAdapter = new ImageAdapter(getActivity());

    ImageCacheParams cacheParams = new ImageCacheParams(getActivity(),
            IMAGE_CACHE_DIR);

    cacheParams.setMemCacheSizePercent(0.25f); 


    mImageFetcher = new ImageFetcher(getActivity(), mImageThumbSize);
    mImageFetcher.setLoadingImage(R.drawable.empty_photo);
    mImageFetcher.addImageCache(getActivity().getSupportFragmentManager(),
            cacheParams);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {



    final View v = inflater.inflate(R.layout.image_grid_fragment,
            container, false);
    final GridView mGridView = (GridView) v.findViewById(R.id.gridView);
    mGridView.setAdapter(mAdapter);
    mGridView.setOnItemClickListener(this);
    mGridView.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView absListView,
                int scrollState) {

            if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_FLING) {
                mImageFetcher.setPauseWork(true);
            } else {
                mImageFetcher.setPauseWork(false);
            }
        }



        @Override
        public void onScroll(AbsListView absListView, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {
        }
    });


    mGridView.getViewTreeObserver().addOnGlobalLayoutListener(
            new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    if (mAdapter.getNumColumns() == 0) {
                        final int numColumns = (int) Math.floor(mGridView
                                .getWidth()
                                / (mImageThumbSize + mImageThumbSpacing));
                        if (numColumns > 0) {
                            final int columnWidth = (mGridView.getWidth() / numColumns)
                                    - mImageThumbSpacing;
                            mAdapter.setNumColumns(numColumns);
                            mAdapter.setItemHeight(columnWidth);
                            if (BuildConfig.DEBUG) {
                                Log.d(TAG,
                                        "onCreateView - numColumns set to "
                                                + numColumns);
                            }
                        }
                    }
                }
            });

    return v;
}

@Override
public void onResume() {
    super.onResume();
    mImageFetcher.setExitTasksEarly(false);
    mAdapter.notifyDataSetChanged();
}

@Override
public void onPause() {
    super.onPause();
    mImageFetcher.setPauseWork(false);
    mImageFetcher.setExitTasksEarly(true);
    mImageFetcher.flushCache();
}

@Override
public void onDestroy() {
    super.onDestroy();
    mImageFetcher.closeCache();
}

@TargetApi(16)
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    final Intent i = new Intent(getActivity(), ImageDetailActivity.class);
    i.putExtra(ImageDetailActivity.EXTRA_IMAGE, (int) id);
    if (Utils.hasJellyBean()) {

        ActivityOptions options = ActivityOptions.makeScaleUpAnimation(v,
                0, 0, v.getWidth(), v.getHeight());
        getActivity().startActivity(i, options.toBundle());
    } else {
        startActivity(i);
    }
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.main_menu, menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.clear_cache:
        mImageFetcher.clearCache();
        Toast.makeText(getActivity(), R.string.clear_cache_complete_toast,
                Toast.LENGTH_SHORT).show();
        return true;
    }
    return super.onOptionsItemSelected(item);
}


private class ImageAdapter extends BaseAdapter {

    private final Context mContext;
    private int mItemHeight = 0;
    private int mNumColumns = 0;
    private int mActionBarHeight = 0;
    private GridView.LayoutParams mImageViewLayoutParams;

    public ImageAdapter(Context context) {
        super();
        mContext = context;
        mImageViewLayoutParams = new GridView.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

        TypedValue tv = new TypedValue();
        if (context.getTheme().resolveAttribute(
                android.R.attr.actionBarSize, tv, true)) {
            mActionBarHeight = TypedValue.complexToDimensionPixelSize(
                    tv.data, context.getResources().getDisplayMetrics());
        }
    }

    @Override
    public int getCount() {

        return Images.imageThumbUrls.length + mNumColumns;
    }

    @Override
    public Object getItem(int position) {
        return position < mNumColumns ? null
                : Images.imageThumbUrls[position - mNumColumns];
    }

    @Override
    public long getItemId(int position) {
        return position < mNumColumns ? 0 : position - mNumColumns;
    }

    @Override
    public int getViewTypeCount() {

        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        return (position < mNumColumns) ? 1 : 0;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup container) {

        if (position < mNumColumns) {
            if (convertView == null) {
                convertView = new View(mContext);
            }

            convertView.setLayoutParams(new AbsListView.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, mActionBarHeight));
            return convertView;
        }


        ImageView imageView;
        if (convertView == null) { 
            imageView = new RecyclingImageView(mContext);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setLayoutParams(mImageViewLayoutParams);
        } else { 
            imageView = (ImageView) convertView;
        }


        if (imageView.getLayoutParams().height != mItemHeight) {
            imageView.setLayoutParams(mImageViewLayoutParams);
        }


        mImageFetcher.loadImage(Images.imageThumbUrls[position
                - mNumColumns], imageView);
        return imageView;
    }


    public void setItemHeight(int height) {
        if (height == mItemHeight) {
            return;
        }
        mItemHeight = height;
        mImageViewLayoutParams = new GridView.LayoutParams(
                LayoutParams.MATCH_PARENT, mItemHeight);
        mImageFetcher.setImageSize(height);
        notifyDataSetChanged();
    }

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

    public int getNumColumns() {
        return mNumColumns;
    }
}
}
4

1 に答える 1