我正在创建一个需要使用Gallery
的应用。我的问题是,Gallery
真的很不稳定。我的代码与android提供的示例代码非常相似,但不仅仅让ImageAdapter
返回ImageView
,我还是返回LinearLayout
,因为我需要下面的文本图片。有什么建议吗?
这是我的代码:
package org.example.gallery;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class GalleryTestActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery gallery = (Gallery) this.findViewById(R.id.gallery);
gallery.setAdapter(new ItemAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(GalleryTestActivity.this, "" + position,
Toast.LENGTH_SHORT).show();
}
});
}
public class ItemAdapter extends BaseAdapter {
private Context mContext;
private final Integer[] mImageIds = { R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4,
R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_8 };
private final String[] mStringIds = { "1-pc. Chicken with Rice",
"2-pc. Chicken with Rice", "Tower Burger", "Bucket", "Barrel",
"Chicken Fillet", "Chicken burger", "Another Chicken Burger" };
public ItemAdapter(Context c) {
this.mContext = c;
}
public int getCount() {
return mImageIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout l = new LinearLayout(this.mContext);
l.setOrientation(LinearLayout.VERTICAL);
ImageView iv = new ImageView(this.mContext);
iv.setImageResource(mImageIds[position]);
iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
TextView tv = new TextView(this.mContext);
tv.setText(mStringIds[position]);
tv.setTextSize(20);
tv.setTextColor(Color.RED);
tv.setGravity(Gravity.CENTER_HORIZONTAL);
l.addView(iv);
l.addView(tv);
return l;
}
}
}
答案 0 :(得分:1)
使用ViewHolder(所以你不会在每个getView调用中充气/创建视图(视图都是回收))模式,如下例所示:
编辑:
static class ViewHolder {
TextView tv;
ImageView iv;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null){
LinearLayout l = new LinearLayout(this.mContext);
l.setOrientation(LinearLayout.VERTICAL);
ImageView iv = new ImageView(this.mContext);
iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
TextView tv = new TextView(this.mContext);
tv.setTextSize(20);
tv.setTextColor(Color.RED);
tv.setGravity(Gravity.CENTER_HORIZONTAL);
l.addView(iv);
l.addView(tv);
convertView = l;
holder = new ViewHolder();
holder.tv = tv;
holder.iv = iv;
} else {
holder = (ViewHolder)convertView.getTag();
}
holder.tv.setText(mStringIds[position]);
holder.iv.setImageResource(mImageIds[position]);
return convertView;
}