0

我正在尝试在我的应用程序中制作图像库,我目前将其作为网格视图,然后通过 ImageAdapter 传递到完整图像视图。

问题是我希望能够在从网格视图单击后在图片之间滑动。

有人提到我可能可以使用片段来做到这一点,但我是 android 开发的新手。

有人可以进一步解释如何做到这一点。谢谢

我的代码是:

    public class Marble extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.marble);

GridView gridView = (GridView) findViewById(R.id.gridview1);

        // Instance of ImageAdapter Class
        gridView.setAdapter(new ImageAdapter1(this));

        /**
         * On Click event for Single Gridview Item
         * */
        gridView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {

                // Sending image id to FullScreenActivity
                Intent i = new Intent(getApplicationContext(), FullImageActivity1.class);
                // passing array index
                i.putExtra("id", position);
                startActivity(i);
            }
        });

    }
}

然后

public class FullImageActivity1 extends Activity {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.full_image);

        // get intent data
        Intent i = getIntent();

        // Selected image id
        int position = i.getExtras().getInt("id");
        ImageAdapter1 imageAdapter1 = new ImageAdapter1(this);

        ImageView imageView = (ImageView) findViewById(R.id.fullimage);
        imageView.setImageResource(imageAdapter1.mThumbIds[position]);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
    }
}

最后:

public class ImageAdapter1 extends BaseAdapter {
    private Context mContext;

    // Keep all Images in array
    Integer[] mThumbIds = {
            R.drawable.arabescato, R.drawable.biancocarrara,
            R.drawable.botticinoclassico, R.drawable.calacattaoro,
            R.drawable.cremamarfil, R.drawable.cremavalencia,
            R.drawable.emperadordark, R.drawable.jurabeige,
            R.drawable.neromarquina, R.drawable.perlatoolympo,
            R.drawable.rojoalicante

    };

    // Constructor
    public ImageAdapter1(Context c){
        mContext = c;
    }

    @Override
    public int getCount() {
        return mThumbIds.length;
    }

    @Override
    public Object getItem(int position) {
        return mThumbIds[position];
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {         
        ImageView imageView = new ImageView(mContext);
        imageView.setImageResource(mThumbIds[position]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new GridView.LayoutParams(265, 265));
        return imageView;
    }

}
4

1 回答 1

1

而不是图像视图在您的 FullImageActivity1 中放置一个视图寻呼机

this.mPagerfrimage = new pagerfrimage(getSupportFragmentManager(),
                list of drawables here);
        ViewPager vp_image = (ViewPager) findViewById(R.id.viewpager1);
        vp_image.setAdapter(mPagerfrimage);

这是寻呼机适配器类

class pagerfrimage extends FragmentPagerAdapter {
    List<Integer> la;

    public pagerfrimage(FragmentManager fm, List<Integer> result) {
        super(fm);
        this.la = result;
        // TODO Auto-generated constructor stub
    }

    @Override
    public Fragment getItem(int arg0) {
        // TODO Auto-generated method stub
        fragmentforimage ffrFragmentforimage = new fragmentforimage();
        Bundle bundle = new Bundle();
        bundle.putInt("resourceid", la.get(arg0));
        bundle.putInt("number", arg0);
        ffrFragmentforimage.setArguments(bundle);
        return ffrFragmentforimage;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return la.size();
    }



}

片段类

public class fragmentforimage extends Fragment {
int number;
int resourceid;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        resourceid=getArguments()!=null?getArguments().getInt("resourceid"):0;
        number=getArguments()!=null?getArguments().getInt("number"):0;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View v=inflater.inflate(R.layout.yourlayot containing image view, container, false);
        ImageView iv=(ImageView)v.findViewById(R.id.your imageviews id);
        Drawable drawable=getResources().getDrawable(resourceid);
            iv.setImageDrawable(drawable);
            return v;}

您可以传递当前选择的图像 ID,这样您就可以首先根据需要更改代码

于 2013-03-28T10:58:40.173 回答