android viewpager就像图库一样

时间:2015-05-11 11:30:50

标签: android gridview android-fragments android-viewpager

我试图创建一个图库。

我有一个ViewPager,每个Fragment包含一个图像(可转换的图片库是我想要完成的)。 我还有一个GridView onitemclick触发显示所点击图片的所选位置的ViewPager。 我的问题是向右滑动一次,向左滑动一次显示与点击的图像相同的图像。 香港专业教育学院尝试setoffscreenpagelimit没有成功。 这是我的viewpager活动:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image_gallery);
    helper = new WeddHelper(this,"wedd.db",null,1);
    pager = (ViewPager) findViewById(R.id.pager);
    pager.setOnPageChangeListener(this);
    Intent ing = getIntent();
    curr = ing.getIntExtra("curr", 0);
    byto = helper.getPhotos();
    count = byto.size();
    FragmentManager mg = getSupportFragmentManager();
   fragments = new Vector<Fragment>();
    for(int i = 0; i<count;i++){
fragments.add(Fragment.instantiate(this,GalleryFrag.class.getName()));            
    }
    mAdapter = new FragmentAdapter(getSupportFragmentManager(), fragments, this);
    pager.setAdapter(mAdapter);
    pager.setOffscreenPageLimit(1);
    pager.setCurrentItem(curr);
}

@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

@Override
public void onPageSelected(int position) {
    mCurrentPosition = position;
    curr = position;
}

@Override
public void onPageScrollStateChanged(int state) {

}
public int getClicked(){
    return curr;
}
public byte[] getPhotoToBeDisplayed(){

    return byto.get(curr);
}
}

这是我的自定义适配器:

public class FragmentAdapter extends FragmentStatePagerAdapter {

private List<Fragment> fragments=null;
private FragmentManager fragmentManager=null;
private Context context;

public FragmentAdapter(FragmentManager fragmentManager,List<Fragment> fragments, Context context) {
    super(fragmentManager);
    this.fragments=fragments;
    this.fragmentManager=fragmentManager;
    this.context = context;
}

@Override
public Fragment getItem(int position) {
    return fragments.get(position);

}
@Override
public int getCount() {
    return fragments.size();

}
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object)
{
    super.setPrimaryItem(container,0,object);
}
@Override
public void notifyDataSetChanged()
{
    super.notifyDataSetChanged();
}
@Override
public void destroyItem(ViewGroup collection, int position, Object view) {
    fragmentManager.executePendingTransactions();
    fragmentManager.saveFragmentInstanceState(fragments.get(position));
}
public void replaceItem(int position,Fragment fragment)
{
    fragments.set(position, fragment);
    this.notifyDataSetChanged();
}
}

这是片段:

public class GalleryFrag extends Fragment {
WeddHelper helper;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.gallery_fragment, container, false);
    ImageGallery myActivity = (ImageGallery) getActivity();
    int curr = myActivity.getClicked();
    byte[] photoToBeDisplayed = myActivity.getPhotoToBeDisplayed();
    helper = new WeddHelper(getActivity(),"wedd.db", null, 1);
    ArrayList<byte[]> imgs = helper.getPhotos();
    Bitmap br = BitmapFactory.decodeByteArray(photoToBeDisplayed,0,photoToBeDisplayed.length);
    Bitmap bi = BitmapFactory.decodeByteArray(imgs.get(curr),0,imgs.get(curr).length);
    Toast.makeText(getActivity(), curr+"this is from frag", Toast.LENGTH_SHORT).show();
    ImageView iv = (ImageView) v.findViewById(R.id.fragImage);
    iv.setImageBitmap(br);
    return v;
}
}

0 个答案:

没有答案