简体   繁体   中英

Showing a Viewpager gallery in a dialog in android

public static void ShowImagesInDialogAsViewPager(Context context, final Dialog dialog, ArrayList<String> listOfImages) {


        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.dialog_view_images);

        AdptViewPagerGallery mCustomPagerAdapter = new AdptViewPagerGallery(context,listOfImages);


        ViewPager pagerId=(ViewPager)dialog.findViewById(R.id.pagerId);
        ImageView imgId=(ImageView)dialog.findViewById(R.id.imgId);
        Button btnSubmit=(Button)dialog.findViewById(R.id.btn_submit);

        pagerId.setAdapter(mCustomPagerAdapter);

        btnSubmit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                dialog.cancel();
            }
        });


        dialog.setCanceledOnTouchOutside(true);
        //dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        dialog.getWindow().setLayout(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
        dialog.show();
    }

listOfImages has:

[/storage/emulated/0/DCIM/Camera/IMG_20150114_185651974.jpg, /storage/emulated/0/DCIM/Camera/IMG_20150114_185524338.jpg, /storage/emulated/0/DCIM/Camera/IMG_20150114_185502114.jpg]

AdptViewPagerGallery.java

public class AdptViewPagerGallery extends PagerAdapter {

    Context mContext;
    LayoutInflater mLayoutInflater;
    List<String> images;

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return images.size();
    }

    public AdptViewPagerGallery(Context context,List<String> _images) {
        mContext = context;
        mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        images=_images;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        // TODO Auto-generated method stub
        return view == ((LinearLayout) object);
    }


    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View itemView = mLayoutInflater.inflate(R.layout.pager_item, container, false);
        try {
            ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);

            imageView.setImageBitmap(BitmapFactory.decodeFile(images.get(position)));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return itemView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((LinearLayout) object);
    }

}

OUTPUT: No image is displaying

How to achieve this !

EDIT

dialog_view_images.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/ll_top"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="@dimen/sub_title_layout_padding" >

        <android.support.v4.view.ViewPager
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/pagerId"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:paddingTop="10dp" >
        </android.support.v4.view.ViewPager>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/btn_submit"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/windhya_bluedark_color"
            android:gravity="center"
            android:padding="@dimen/sub_title_layout_padding"
            android:text="@string/Cancel"
            android:textColor="@android:color/white"
            android:textSize="@dimen/sub_sub_title"
            android:textStyle="bold" />
    </LinearLayout>

</LinearLayout>

pager_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

在此处输入图片说明

Add this line ((ViewPager) container).addView(itemView) before return itemView

View itemView = mLayoutInflater.inflate(R.layout.pager_item, container, false);

try
{
    ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);
    imageView.setImageBitmap(BitmapFactory.decodeFile(images.get(position)));
}
catch (Exception e) { 
    e.printStackTrace();
}

((ViewPager) container).addView(itemView);

return itemView;

change your instantiateItem method as written below:

@Override
public Object instantiateItem(View collection, int position) {
    View itemView = mLayoutInflater.inflate(R.layout.pager_item, null);
try {
        ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);

        imageView.setImageBitmap(BitmapFactory.decodeFile(images.get(position)));
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return itemView;
}

EDIT

try changing layout dialog_view_images.xml as below:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/ll_top"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:padding="@dimen/sub_title_layout_padding" >

    <android.support.v4.view.ViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/pagerId"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:paddingTop="10dp" >
    </android.support.v4.view.ViewPager>
</LinearLayout>

<LinearLayout
    android:id="@+id/ll_bottom"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/btn_submit"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/windhya_bluedark_color"
        android:gravity="center"
        android:padding="@dimen/sub_title_layout_padding"
        android:text="@string/Cancel"
        android:textColor="@android:color/white"
        android:textSize="@dimen/sub_sub_title"
        android:textStyle="bold" />
</LinearLayout>

</RelativeLayout>

Just change your instantiateItem

View itemView = mLayoutInflater.inflate(R.layout.pager_item, null);

to

View itemView = mLayoutInflater.inflate(R.layout.gallery_pager_item, container, false);

and add following line before return itemView;

container.addView(itemView);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM