Android プロジェクトでギャラリーを作成しようとしていますが、正常に動作しないようです。
これが私がやろうとしていることです:
基本的に、1 つの画像のみを表示するが、同じコントロール (左右にスライド) を備えたギャラリーが必要です。インターネットから非同期に写真を取得しています。
私はどこにいますか?
さて、今のところギャラリーを表示しようとしているだけです...「1枚の写真」のステップが表示されます。
私の問題は何ですか?
うーん... ギャラリーがレイアウトに表示されません。まったく。
問題に見えるのは?
ログ情報によると、ギャラリー オブジェクトの高さと幅が... 0 です。
コードといえば…
レイアウト.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@drawable/fond">
<ImageView android:background="@drawable/banniere" android:layout_height="wrap_content" android:layout_width="fill_parent"/>
<Gallery
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
アクティビティ
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.galleryfullscreen);
Gallery gal = (Gallery) findViewById(R.id.gallery);
ArrayList<Photo> photos = (ArrayList<Photo>) this.getIntent().getExtras().getSerializable("Photos");
int pos = this.getIntent().getExtras().getInt("Index");
Log.i("Season",photos.toString());
gal.setAdapter(new PhotoAdapter(this, photos, false));
gal.setSelection(pos);
Log.d("Bottom", String.valueOf(gal.getBottom()));
Log.d("Right", String.valueOf(gal.getRight()));
Log.d("Width", String.valueOf(gal.getWidth()));
Log.d("Height", String.valueOf(gal.getHeight()));
}
エクストラは正しく設定されています。
アダプタ
public class PhotoAdapter は BaseAdapter を拡張します {
private ArrayList<PhotoView> views;
private ArrayList<Photo> season;
public PhotoAdapter(Context c, ArrayList<Photo> images, boolean mini) {
season = images;
views = new ArrayList<PhotoView>();
for (int i = 0; i < images.size() ; i++)
if (mini)
views.add(new PhotoView(c,images.get(i).getUrlMini(),false));
else{
views.add(new PhotoView(c,images.get(i).getUrlBig(),true));
}
}
@Override
public int getCount() {
return views.size();
}
@Override
public Object getItem(int position) {
return views.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return views.get(position);
}
public ArrayList<Photo> getSeason() {
return season;
}
}
このアダプターは、他のオブジェクト (gridview など) に対して正常に動作しています。
そしてPhotoView...
public class PhotoView は ImageView を拡張します {
Bitmap image;
public PhotoView(Context context, String url, boolean Gallery) {
super(context);
if (!Gallery){
this.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.WRAP_CONTENT,GridView.LayoutParams.WRAP_CONTENT));
this.setScaleType(ImageView.ScaleType.FIT_CENTER);
this.setImageDrawable((getResources().getDrawable(R.drawable.blk)));
new DownloadImageTask().execute(url);
}
else{
this.setLayoutParams(new Gallery.LayoutParams(100,100));
this.setScaleType(ImageView.ScaleType.FIT_CENTER);
this.setBackgroundDrawable(((getResources().getDrawable(R.drawable.blk))));
this.setImageDrawable((getResources().getDrawable(R.drawable.blk)));
}
}
public Bitmap getImage(){
return image;
}
public void setImage(Bitmap img){
image = img;
this.setImageBitmap(image);
}
同じように、グリッド ビューでも問題なく動作しています。ダウンロード タスクでは、まだギャラリー用に設定されていません。これを設定する前に、少なくともリソースを表示しようとしているので、問題はここから来ていないと確信しています。
すっごい...ログでギャラリーの幅と高さが「0」と表示されるのはなぜですか? LayoutParams は、オブジェクト (XML とコードの両方を試しました) と内部のビューに対して設定されます。アイテムが作成され、「getView」メソッドが呼び出されます。ここで LayoutParams を調べてみましたが、何も変わりません。
また、ギャラリーのレイアウトに xmlns:android="http://schemas.android.com/apk/res/android" の部分を追加しようとしましたが、何も変わりません。
何が欠けているのか本当にわかりません。ギャラリー オブジェクトに関する多くのチュートリアルを適応させようとしましたが、それでもわかりません! 私が何をすべきか/試してみるべきかについて何か考えがある場合は、お願いします...
皆さんありがとう!
乾杯