我是 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);
}
}
对此的所有帮助将不胜感激,因为我非常难过!
提前致谢!