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有:
[/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);
}
}
如何实现这一点!
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>
答案 0 :(得分:1)
在((ViewPager) container).addView(itemView)
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;
答案 1 :(得分:0)
更改你的instantiateItem方法,如下所示:
@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;
}
修改强>
尝试更改布局dialog_view_images.xml,如下所示:
<?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>
答案 2 :(得分:0)
只需更改 instantiateItem
即可View itemView = mLayoutInflater.inflate(R.layout.pager_item, null);
到
View itemView = mLayoutInflater.inflate(R.layout.gallery_pager_item, container, false);
并在返回itemView之前添加以下行;
container.addView(itemView);