Xperia Arc 中不稳定的图库
我正在制作一个需要使用图库
的应用程序。我的问题是,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;
}
}
}
I'm making an app that requires the use of a Gallery
. My problem is, the Gallery
is really choppy. My code is pretty much similar to the sample code that android provides, but rather than just having the ImageAdapter
return an ImageView
, I have it return a LinearLayout
, because I need text underneath the image. Any suggestions?
Here's my code:
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;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 ViewHolder(这样你就不会在每个 getView 调用中膨胀/创建视图(视图正在回收))模式,如下例所示:
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html
编辑:
use ViewHolder(so u will not inflating/creating view in every getView call(views are recycling)) pattern like in this example:
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html
EDIT: