ImageView不显示图像

时间:2019-06-20 14:51:16

标签: java android android-viewpager imageview

我想在ImageView中滑动ViewPager内的3张图像,当我运行应用程序时,我可以滑动3张空白页面/图像,但没有图像显示。 图像位于res/drawable文件夹中。

当我浏览与该问题相关的所有文章时,我试图添加此处建议的每一行代码,以解决堆栈溢出问题(您将在我提供的代码中看到所有这些内容)。 我试图将图像缩小到300x300,但仍然无法正常工作。 我尝试将图像放在drawable文件夹的子文件夹中,或放在hdpi, mdpi,xdpi... etc文件夹中,发生编译错误。

1)活动的xml文件:

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

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="480dp"
        android:visibility="visible"
        tools:visibility="visible"> </androidx.viewpager.widget.ViewPager>


</RelativeLayout>

2)custom_layout代码(imageView):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerInside"
        android:src="@mipmap/ic_launcher" />

</RelativeLayout>

3)整个ViewPagerAdapter:

package com.example.apptest;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

public class ViewPagerAdapter extends PagerAdapter {

    private Context context;
    private LayoutInflater layoutInflater;
    private Integer[] images = {R.drawable.resized1, R.drawable.resized2, R.drawable.resized3};

    public ViewPagerAdapter(Context context){
        this.context = context;
    }


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

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

    @Override
    public Object instantiateItem( ViewGroup container, int position) {
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.custom_layout, null);
        ImageView imageView =  view.findViewById(R.id.imageView);
        imageView.setImageResource(images[position]);            
        container.addView(view);
        return view;
    }



    @Override
    public void destroyItem( ViewGroup container, int position,  Object object) {
        ViewPager vp = (ViewPager) container;
        View view = (View) object;
        vp.removeView((View)view);

    }

}

4)android清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.apptest">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission 
android:name="android.permission.READ_EXTERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:largeHeap="true"
        android:hardwareAccelerated="false"

        android:theme="@style/AppTheme">
        <activity android:name=".menuSliderActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity"></activity>

    </application>

</manifest>

5)活动的Java代码:

package com.example.apptest;

import android.app.Activity;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

public class menuSliderActivity extends AppCompatActivity {

    private ViewPager viewPager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu_slider);

        viewPager = (ViewPager) findViewById(R.id.viewPager);
        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(this);
        viewPager.setAdapter(viewPagerAdapter);

    }

}

我通常应该能够看到我的图像并在其中滑动,但是我没有任何错误,并且页面空白。

3 个答案:

答案 0 :(得分:1)

这很适合我

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

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

代替此

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

@Override
public void destroyItem( ViewGroup container, int position,  Object object) {
    ViewPager vp = (ViewPager) container;
    View view = (View) object;
    vp.removeView((View)view);

}

答案 1 :(得分:0)

尝试进行此测试

将wrap_content替换为match_parent

<ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent" 
        android:layout_height="match_parent"
        android:scaleType="centerInside"
        android:src="@mipmap/ic_launcher" />

答案 2 :(得分:0)

只需更改此

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