0

私はいくつかの例に基づいてこれをつなぎ合わせました、結果はごちゃごちゃした混乱です。画像/テキストが重なり合っており、単一のリストビューアイテムのように前後に移動します。画像のリストのようなものはありません。私の目標は、その下にテキストビューがある画像でした。それがすべてでした。注意:アダプターのgetView()から膨張したビューを返そうとしましたが、結果はほとんど同じでした。GalleryがタイプとしてImageView以上のものをサポートできるのは本当に本当かどうか疑問に思い始めています。

package com.example.elgallery;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

private GalleryAdapter mGalleryAdapter;
private ArrayList<GalleryItem> mGalleryItems;
private Gallery mGallery;

public class GalleryItem{
  int imageId=R.drawable.ic_launcher;
  String caption;
   public int getImageId() {
    return imageId;     }

public String getCaption() {
    return caption;
}
    public GalleryItem(int i,String s) {
    imageId=i;
    caption=s;  
    }   
}




  int[] resourceImages =     {R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
        R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
 GalleryItem[] item = new GalleryItem[6];
 mGalleryItems = new ArrayList<GalleryItem>();
    //initialising all items, change member variables according to needs
   for(int i=0;i<6;i++){
       mGalleryItems.add(new GalleryItem(resourceImages[i], "pic no"   +(i+1)));          
    }  

 mGallery = (Gallery)findViewById(R.id.gallery);
 mGalleryAdapter = new GalleryAdapter(this);
 mGalleryAdapter.setGalleryItems(mGalleryItems);
 mGallery.setAdapter(mGalleryAdapter);

 mGalleryAdapter.notifyDataSetChanged();

}

private class GalleryAdapter extends BaseAdapter {

   private Context mContext;
   mArrayList<GalleryItem> galleryItems;

   public ArrayList<GalleryItem> getGalleryItems() {
    return galleryItems;
}
public void setGalleryItems(ArrayList<GalleryItem> galleryItems) {
    this.galleryItems = galleryItems;
}
public GalleryAdapter (Context context)
{
    mContext = context;
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return galleryItems.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return galleryItems.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{ 
    LinearLayout ll = new LinearLayout(mContext); 
    ll.setOrientation(LinearLayout.VERTICAL);
    ImageView i = new ImageView(ll.getContext()); 
    i.setTag("someTage"); //i.setImageURI(mUrls[position]);
    i.setImageResource(R.drawable.ic_launcher);
    i.setScaleType(ImageView.ScaleType.FIT_XY); 
    i.setLayoutParams(new Gallery.LayoutParams(48, 48)); 
    TextView tv = new TextView(ll.getContext()); 
    tv.setTag("someTag2"); tv.setText("someText");
    tv.setLayoutParams(new Gallery.LayoutParams(48, 48)); 
    ll.addView(tv); 
    return ll; 
} 
  }

 }

XMLは次のとおりです。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
 <Gallery
    android:id="@+id/gallery"
    android:layout_width="fill_parent"
    android:layout_height="200dip"
   />
 </LinearLayout>

結果:ごちゃごちゃした混乱。3つの画像とテキストビューが重なり合っているように見えます。

4

1 に答える 1

0

私の目標は、その下にテキストビューがある画像でした。

いくつかのエラーがあるため、現在のgetView方法ではそれができません。ImageViewまず第一に、LinearLayoutラッパーに追加する場所がどこにもありません。第 2 に、ビューは の子であるため、LayoutParamsこれらのビューの はタイプではありません。以下のコードを確認してください。Gallery.LayoutParamsLinearLayout

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LinearLayout ll = new LinearLayout(mContext);
        ll.setLayoutParams(new Gallery.LayoutParams(
                Gallery.LayoutParams.MATCH_PARENT,
                Gallery.LayoutParams.MATCH_PARENT));
        ll.setOrientation(LinearLayout.VERTICAL);
        ImageView i = new ImageView(ll.getContext());
        i.setTag("someTage"); // i.setImageURI(mUrls[position]);
        i.setImageResource(R.drawable.ic_launcher);
        i.setScaleType(ImageView.ScaleType.FIT_XY);
        i.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        ll.addView(i);
        TextView tv = new TextView(ll.getContext());
        tv.setTag("someTag2");
        tv.setText("someText");
        tv.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        ll.addView(tv);         
        return ll;
    }

また、Android アダプターの最適化についてもお読みになることをお勧めします。

于 2012-11-22T12:46:14.827 に答える