Xperia Arc 中不稳定的图库

发布于 2024-12-12 06:29:22 字数 2844 浏览 1 评论 0原文

我正在制作一个需要使用图库的应用程序。我的问题是,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇柔作态 2024-12-19 06:29:22

使用 ViewHolder(这样你就不会在每个 getView 调用中膨胀/创建视图(视图正在回收))模式,如下例所示:

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html

编辑:

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

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:

       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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文