gpt4 book ai didi

Android Gallery 的 getView() 返回不正确的位置

转载 作者:太空宇宙 更新时间:2023-11-03 13:05:04 26 4
gpt4 key购买 nike

感谢阅读!

我使用 Android Gallery 将 LayoutParams 作为 MATCH_PARENT 一次显示一个全屏图像。

这是我的代码:

布局.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<Gallery
android:id="@+id/gallery" android:layout_width="fill_parent"
android:layout_height="fill_parent">
</Gallery>
<TextView android:id="@+id/tvShowText" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentBottom="true" />
</RelativeLayout>

HelloGallery.java


package com.android.sagar;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class HelloGallery extends Activity {

TextView tvShowText = null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

tvShowText = (TextView)findViewById(R.id.tvShowText);

Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));

g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
Toast.makeText(HelloGallery.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}

public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;

private Integer[] mImageIds = {
R.drawable.pic1,
R.drawable.pic2,
R.drawable.pic3
};

public ImageAdapter(Context c) {
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) {
ImageView i = new ImageView(mContext);

tvShowText.setText("ImageCaption for Image No.: "+position);

i.setImageResource(mImageIds[position]);
i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);

return i;
}
}
}

图像显示正确,但我打印的标题有一个偏差,有时甚至完全不同。 :(

我调试后发现,当我滑动一次时,getView() 会被调用多次——大约 2 到 3 次……它们都在不同的位置,但图像似乎一次移动一个。 :(请帮忙!

最佳答案

为了正确更新您的标题,您需要覆盖GalleryOnItemSelectedListener。 Sujit 在他的评论中是正确的,调用 getView 是为了渲染(或在某些情况下预渲染或后渲染)Gallery 中的每个图像。不应依赖于它来识别选择了哪个图像。相反,在 HelloGallery onCreate() 方法中覆盖 OnItemSelectedListener:

    g.setOnItemSelectedListener(new OnItemSelectedListener() 
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
tvShowText.setText("ImageCaption for Image No.: "+position);
}
@Override
public void onNothingSelected(AdapterView<?> arg0)
{
tvShowText.setText("No image selected");
}
});

关于Android Gallery 的 getView() 返回不正确的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6058609/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com