android - ViewPager android 中 ImageView 下方不需要的空白

标签 android android-studio android-fragments android-viewpager android-pageradapter

我使用 ViewPager 作为图像 slider 。我正在使用 Android Studio。一切正常,我可以在图像之间滑动。然而,出于某种原因,在我的图像下方有空白。我知道你不能在 ViewPager 上调用 wrap_content 并且我试图将包含图像 slider 的 fragment 放入一个 Activity 中,另一个 fragment 位于图像 slider fragment 下方,但它们之间仍然有一些奇怪的空白和内部我还可以滑动到另一个图像的空白区域,这当然只会在以后引起问题:

fragment _home.xml:

<FrameLayout 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"
tools:context="HomeFragment">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

<fragment
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:name="ImageSliderFragment"
    android:id="@+id/fragment1"
    tools:layout="@layout/fragment_image_slider"
    android:layout_weight="2"/>

    <fragment
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:name="BottomFragment"
        tools:layout="@layout/fragment_bottom"
        android:id="@+id/fragment2"
        android:layout_weight="2"/>

</LinearLayout>

</FrameLayout>

swipe_layout.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
    android:id="@+id/image_view_of_slider"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:scaleType="fitXY"/>
<!-- adjustviewBounds prevents non-defined padding -->
<TextView
    android:id="@+id/text_view_of_swipe_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"/>

</RelativeLayout>

fragment_image_slider.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="ImageSliderFragment"
android:id="@+id/image_slide_id">

<android.support.v4.view.ViewPager
    android:id="@+id/view_pager_of_image_slider"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
<!--height="wrap_content/match_parent" both have the same result -->

</FrameLayout>

CustomSliderAdapter.java:

public class CustomSliderAdapter extends PagerAdapter {

private int[] image_res = {R.drawable.a,R.drawable.b,
        R.drawable.c, R.drawable.d};
private String[] image_names = {"a", "b", "c", "d"};
private Context ctx;
private LayoutInflater layoutInflater;

public CustomSliderAdapter(Context ctx) {
    this.ctx = ctx;
}

@Override
public int getCount() {
    return image_res.length;
}

@Override
public int getItemPosition(Object object) {
    return super.getItemPosition(object);
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return (view == (RelativeLayout) object);
}

@Override
public Object instantiateItem(final ViewGroup container, final int position) {
    layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = layoutInflater.inflate(R.layout.swipe_layout, container, false);
    ImageView imageView = (ImageView) view.findViewById(R.id.image_view_of_slider);
    TextView textView = (TextView) view.findViewById(R.id.text_view_of_swipe_layout);
    imageView.setImageResource(image_res[position]);
    textView.setText(image_names[position]);
    container.addView(view);

    return view;
}

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

ImageSliderFragment.class:

public class ImageSliderFragment extends Fragment {
ViewPager viewPager;
CustomSliderAdapter adapter;
View myView;
public static int imageHeight;


public ImageSliderFragment() {}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    if(myView==null) {
        myView = inflater.inflate(R.layout.fragment_image_slider, container, false);
        viewPager = (ViewPager) myView.findViewById(R.id.view_pager_of_image_slider);
        adapter = new CustomSliderAdapter(getActivity());

        //With this I could set height of ViewPager manually
        ViewGroup.LayoutParams params = viewPager.getLayoutParams();
        params.height = imageHeight;
        viewPager.setLayoutParams(params);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        viewPager.setAdapter(adapter);
    } else {
        ((ViewGroup) myView.getParent()).removeView(myView);
    }

    return myView;
}

}

How activity looks

我还尝试用数字 (f.e.200dp) 更改我的 xml 文件的高度,这很有效,但是有太多不同的设备屏幕,有时它会在某些时候搞砸

所以 我的问题是:使 ViewPager 适合 Imageview,因为我也可以在空白处的图像之间滑动。 非常感谢您的帮助!

最佳答案

您可以在您的 imageview 上尝试这样查看 viewpager 图像....

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:scaleType="fitXY" />

关于android - ViewPager android 中 ImageView 下方不需要的空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38398204/

相关文章:

android - 其他选项菜单项在展开的搜索 View 折叠后不显示

android - android studio中的奇怪文字

java - 确定要更新的 fragment

android-studio - 错误:Conflict with dependency 'com.google.code.findbugs:jsr305'

android - 敏捷异常 : Cannot merge new index 65536 into a non-jumbo instruction

android - 如何在 viewpager 中重新创建 fragment

java - 从另一个 fragment 调用一个 fragment ,然后替换其布局

android - 安卓版基维

android - 以百分比(或类似百分比)动态调整 View 大小?

android - 如何制作实现可点击的自定义 RelativeLayout?