Android GalleryView Problem

只愿长相守 提交于 2019-12-11 15:27:48

问题


I have a problem with android galleryview, in my app I have a lot of pictures. And I wanna display them by gallerview and make them selectable. I figured out displaying them horizontal scrollable and make them selectable with Galleryview, but I need one image on the screen at a time, after horizontal scrolling the scrollbar by the user, another picture must be shown. My test code is below and this shows 3 pictures on the screen, from http://www.androidpeople.com/android-gallery-example:

        package com.projects.cards;

    import android.app.Activity;
    import android.content.Context;
    import android.content.res.TypedArray;
    import android.os.Bundle;
    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.Toast;

public class GaleryTestActivity extends Activity {

private Gallery gallery;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.galery);

    gallery = (Gallery) findViewById(R.id.examplegallery);
    gallery.setAdapter(new AddImgAdp(this));

    gallery.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v, int position, long id) {
            // Displaying the position when the gallery item in clicked
            Toast.makeText(GaleryTestActivity.this, "Position=" + position, Toast.LENGTH_SHORT).show();
        }
    });

}

public class AddImgAdp extends BaseAdapter {
    int GalItemBg;
    private Context cont;

    // Adding images.
    private Integer[] Imgid = {
            R.drawable.a_1, R.drawable.a_2, R.drawable.a_3, R.drawable.a_4, R.drawable.a_5, R.drawable.a_6, R.drawable.a_7
    };

    public AddImgAdp(Context c) {
        cont = c;
        TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
        GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
        typArray.recycle();
    }

    public int getCount() {
        return Imgid.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 imgView = new ImageView(cont);

        imgView.setImageResource(Imgid[position]);
        // Fixing width & height for image to display
        imgView.setLayoutParams(new Gallery.LayoutParams(200, 160));
        imgView.setScaleType(ImageView.ScaleType.FIT_XY);
        imgView.setBackgroundResource(GalItemBg);

        return imgView;
    }
}

}

How can I solve this problem?

Thanks in advance..


回答1:


As I understand it, you want the currently focused image to take up all the screen space. You can achieve this by setting the width of the ImageView where you bind it to the Gallery (in getView). Try setting the width of the image to match the width of the device screen.

Note that you can also set the spacing between the items in the Gallery with the gallery.setSpacing(...) method.

I can not tell you how to show a scrollbar, sorry.




回答2:


I have implemented gallery view like, one that is mentioned in the below in the link. Please try that also: Android gallery with caption




回答3:


You can try returning view with fill parent as its width which contains your imageview from your adapters getView() ...

Or alse you can do something like this

getview function

public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater iteminflater = LayoutInflater.from(cont);
    View gallaryitem = iteminflater .inflate(R.layout.gallaryitem, null);
    ImageView imgView= (ImageView ) gallaryitem .findViewById(R.id.image);
    imgView.setImageResource(Imgid[position]);
    gallaryitem .setBackgroundResource(GalItemBg);
    return gallaryitem ;
    }

gallaryitem.xml

<LinearLayout android:layout_width="fill_parent" 
              android:layout_height="wrap_content" 
              android:orientation="vertical" 
              xmlns:android="http://schemas.android.com/apk/res/android" >    
       <LinearLayout android:layout_width="wrap_content" 
                     android:layout_height="wrap_content" 
                     android:orientation="horizontal" 
                     android:layout_gravity="center_horizontal"   
                     xmlns:android="http://schemas.android.com/apk/res/android" >

           <ImageView android:layout_width="wrap_content" 
                      android:id="@+id/image" 
                      android:layout_height="wrap_content"  
                      android:src="@drawable/icon"   
                      xmlns:android="http://schemas.android.com/apk/res/android" >

           </ImageView>
       </LinearLayout>
</LinearLayout>


来源:https://stackoverflow.com/questions/6503544/android-galleryview-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!