0

我在将 OnItemClick 实施到 ViewPager 时遇到了一些麻烦。

我想要它,所以当我点击 ViewPager 显示的我的一张图片时,它会显示一条 toast 消息,告诉我这是有效的。

到目前为止,当我在执行此操作时单击我的一个项目时没有任何反应。

任何帮助,将不胜感激!

ViewPager XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="@+id/rellayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
</android.support.v4.view.ViewPager>

<com.google.ads.AdView
    android:id="@+id/ad"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    ads:adSize="BANNER"
    ads:adUnitId="AdIdHere"
    ads:loadAdOnCreate="true" >
</com.google.ads.AdView>


</RelativeLayout>

ImageDetail片段类:

public class ImageDetailFragment extends Fragment {
private static final String IMAGE_DATA_EXTRA = "extra_image_data";
private String mImageUrl;
private ImageView mImageView;
private ImageFetcher mImageFetcher;

public static ImageDetailFragment newInstance(String imageUrl) {
    final ImageDetailFragment f = new ImageDetailFragment();

    final Bundle args = new Bundle();
    args.putString(IMAGE_DATA_EXTRA, imageUrl);
    f.setArguments(args);

    return f;
}

public ImageDetailFragment() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mImageUrl = getArguments() != null ? getArguments().getString(
            IMAGE_DATA_EXTRA) : null;
}

   private Context context;

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

     final View v = inflater.inflate(R.layout.image_detail_fragment,
        container, false);

      context = v.getContext();

        v.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Toast.makeText(context, "Cilcked..",Toast.LENGTH_SHORT).show();

        }
    });

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    if (Batmanark.class.isInstance(getActivity())) {
        mImageFetcher = ((Batmanark) getActivity()).getImageFetcher();
        mImageFetcher.loadImage(mImageUrl, mImageView);

}
}

@Override
public void onDestroy() {
    super.onDestroy();
    if (mImageView != null) {

        ImageWorker.cancelWork(mImageView);
        mImageView.setImageDrawable(null);
    }
}
}

片段详细信息 XML:

<?xml version="1.0" encoding="utf-8"?>


<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ProgressBar
    style="?android:attr/progressBarStyleLarge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center" />

<com.question.ui.RecyclingImageView
    android:id="@+id/imageView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:contentDescription="@string/imageview_description" />

</FrameLayout>

ViewPager 类:

public class Batmanark extends FragmentActivity implements OnClickListener {
private static final String IMAGE_CACHE_DIR = "images";
public static final String EXTRA_IMAGE = "extra_image";

private ImagePagerAdapter mAdapter;
private ImageFetcher mImageFetcher;
private ViewPager mPager;

@TargetApi(11)
@Override
public void onCreate(Bundle savedInstanceState) {
    if (BuildConfig.DEBUG) {
        Utils.enableStrictMode();
    }
    super.onCreate(savedInstanceState);
    setContentView(R.layout.image_detail_pager);

    AdView ad = (AdView) findViewById(R.id.ad);
    ad.loadAd(new AdRequest());

    final DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    final int height = displayMetrics.heightPixels;
    final int width = displayMetrics.widthPixels;


    final int longest = (height > width ? height : width) / 2;

    ImageCache.ImageCacheParams cacheParams = new ImageCache.ImageCacheParams(
            this, IMAGE_CACHE_DIR);
    cacheParams.setMemCacheSizePercent(0.25f); 


    mImageFetcher = new ImageFetcher(this, longest);
    mImageFetcher.addImageCache(getSupportFragmentManager(), cacheParams);
    mImageFetcher.setImageFadeIn(false);


    mAdapter = new ImagePagerAdapter(getSupportFragmentManager(),
            Images.imageUrlsBatmanark.length);
    mPager = (ViewPager) findViewById(R.id.pager);
    mPager.setAdapter(mAdapter);
    mPager.setPageMargin((int) getResources().getDimension(
            R.dimen.image_detail_pager_margin));
    mPager.setOffscreenPageLimit(2);


    getWindow().addFlags(LayoutParams.FLAG_FULLSCREEN);


    if (Utils.hasHoneycomb()) {
        final ActionBar actionBar = getActionBar();


        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayHomeAsUpEnabled(true);


        mPager.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
            @Override
            public void onSystemUiVisibilityChange(int vis) {
                if ((vis & View.SYSTEM_UI_FLAG_LOW_PROFILE) != 0) {
                    actionBar.hide();
                } else {
                    actionBar.show();
                }
            }
        });


        mPager.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
        actionBar.hide();
    }


    final int extraCurrentItem = getIntent().getIntExtra(EXTRA_IMAGE, -1);
    if (extraCurrentItem != -1) {
        mPager.setCurrentItem(extraCurrentItem);
    }
}

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

@Override
protected void onPause() {
    super.onPause();
    mImageFetcher.setExitTasksEarly(true);
    mImageFetcher.flushCache();
}

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

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_menu, menu);
    return true;
}

public ImageFetcher getImageFetcher() {
    return mImageFetcher;
}


private class ImagePagerAdapter extends FragmentStatePagerAdapter {
    private final int mSize;

    public ImagePagerAdapter(FragmentManager fm, int size) {
        super(fm);
        mSize = size;
    }

    @Override
    public int getCount() {
        return mSize;
    }

    @Override
    public Fragment getItem(int position) {
        return ImageDetailFragment
                .newInstance(Images.imageUrlsBatmanark[position]);
    }
}


@TargetApi(11)
@Override
public void onClick(View v) {
    final int vis = mPager.getSystemUiVisibility();
    if ((vis & View.SYSTEM_UI_FLAG_LOW_PROFILE) != 0) {
        mPager.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
    } else {
        mPager.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
    }
}
}
4

1 回答 1

2

您需要在 ImageDetailFragment 中的视图上设置点击事件

private Context context;

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

     final View v = inflater.inflate(R.layout.image_detail_fragment,
        container, false);

      context = v.getContext();

        v.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Toast.makeText(context, "Cilcked..",Toast.LENGTH_SHORT).show();

        }
    });
mImageView = (ImageView) v.findViewById(R.id.imageView);
return v;
 }
于 2013-10-02T09:53:55.030 回答