2

我是 android 编程的新手,我希望实现一个画廊,它有一个网格视图,然后推送到视图寻呼机,但我在让它工作时遇到了一些问题。我目前只使用静态图像适配器工作,但不确定如何更改它以使用视图寻呼机。我还希望能够将自己的标题添加到每个屏幕,但我不确定如何执行此操作,或者它是否必须是出现的图像名称?

我现在的代码是......

带有网格视图的 .xml

<?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"
    android:background="#3d3d3e" >

    <GridView
        android:id="@+id/gridview1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/phone"
        android:columnWidth="90dp"
        android:gravity="center"
        android:horizontalSpacing="10dp"
        android:numColumns="3"
        android:stretchMode="columnWidth"
        android:verticalSpacing="10dp" >
</GridView>
</RelativeLayout>

网格视图.java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;


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);
            }
        });

    }
}

图像适配器 .java

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

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;
    }

}

最后是图像活动 .java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;

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);
    }
}

对此的所有帮助将不胜感激,因为我非常难过!

提前致谢!

4

1 回答 1

8

在继续之前,请将android.support.v4jar 文件添加到您的项目中。

你需要两件事:ViewPager在你的布局中,以及一个扩展的适配器PagerAdapter

首先将您的full_image.xml布局更改为:

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

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

</LinearLayout>

然后创建一个扩展的适配器PagerAdapter(需要ViewPager

public class ImagePagerAdapter extends PagerAdapter {

    private List<ImageView> images;

    public ImagePagerAdapter(List<ImageView> images) {
        this.images = images;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView imageView = images.get(position);
        container.addView(imageView);
        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(images.get(position));
    }

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

    @Override
    public boolean isViewFromObject(View view, Object o) {
        return view == o;
    }
}

在你的FullImageActivity1课堂上:

public class FullImageActivity1 extends Activity {

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

        // Loop through the ids to create a list of full screen image views
        ImageAdapter1 imageAdapter1 = new ImageAdapter1(this);
        List<ImageView> images = new ArrayList<ImageView>();

        for (int i = 0; i < imageAdapter1.getCount(); i++) {
            ImageView imageView = new ImageView(this);
            imageView.setImageResource(imageAdapter1.mThumbIds[i]);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            images.add(imageView);
        }

        // Finally create the adapter
        ImagePagerAdapter imagePagerAdapter = new ImagePagerAdapter(images);
        ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
        viewPager.setAdapter(imagePagerAdapter);

        // Set the ViewPager to point to the selected image from the previous activity
        // Selected image id
        int position = getIntent().getExtras().getInt("id");
        viewPager.setCurrentItem(position);
    }
}

总体思路是创建一个适配器,扩展PagerAdapter为向ViewPager. 有关更多信息,您可以访问PagerAdapter 上的 Android 文档

于 2013-04-09T16:15:11.103 回答