PagerAdapter被调用两次

时间:2019-02-03 21:28:29

标签: java android view android-pageradapter nestedlayout

我在android中将此应用程序设置为“ Edversity”,并且遇到了无法自行解决的错误。 布局很简单 This is the Layout of the Activity

所以问题是方法“ instantiateItem()”被调用了两次,这在我在该方法中调用随机函数及其创建过程中造成了问题  两次不同的值,并且在第一次调用时会分配正确的视图,但下次不会更新视图。 因此,请帮帮我,也请解释为什么它两次调用AnimalTestAdapter类。

XML设计

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


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"

    android:layout_margin="15dp"
    android:background="@drawable/slide_bg"
    android:elevation="4dp">

    <RelativeLayout
        android:id="@+id/rela1"
        android:layout_width="170dp"
        android:layout_height="170dp"
        android:layout_margin="10dp"
        android:elevation="2dp"
        android:background="@drawable/slide_bg">
        <ImageView
            android:id="@+id/imageanimaa"
            android:layout_width="match_parent"
            android:layout_height="483dp"
            android:src="@mipmap/a_for_apple" />
    </RelativeLayout>
    <RelativeLayout
        android:id="@+id/rela2"
        android:layout_width="170dp"
        android:layout_height="170dp"
        android:layout_margin="10dp"
        android:elevation="2dp"
        android:layout_alignParentRight="true"
        android:background="@drawable/slide_bg">
        <ImageView
            android:id="@+id/imageanimab"
            android:layout_width="match_parent"
            android:layout_height="483dp"
            android:src="@mipmap/b_for_ball" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/rela3"
        android:layout_width="170dp"
        android:layout_height="170dp"
        android:layout_margin="10dp"
        android:elevation="2dp"
        android:layout_below="@+id/rela1"
        android:background="@drawable/slide_bg">
        <ImageView
            android:id="@+id/imageanimac"
            android:layout_width="match_parent"
            android:layout_height="483dp"
            android:src="@mipmap/b_for_ball" />
    </RelativeLayout>
    <RelativeLayout
        android:id="@+id/rela4"
        android:layout_width="170dp"
        android:layout_height="170dp"
        android:layout_margin="10dp"
        android:elevation="2dp"
        android:layout_below="@+id/rela2"
        android:layout_alignParentRight="true"
        android:background="@drawable/slide_bg">
        <ImageView
            android:id="@+id/imageanimad"
            android:layout_width="match_parent"
            android:layout_height="483dp"
            android:src="@mipmap/b_for_ball" />
    </RelativeLayout>
</RelativeLayout>
</RelativeLayout>

AdapterClass

package com.example.akroc.edversity;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import java.util.ArrayList;
import java.util.Collections;

public class AnimalTestAdapter extends PagerAdapter {


    Context context;
    LayoutInflater layoutInflater;
    int randIndex;
    public int Answer;
    final String TAG="yowag";

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

    public int[] slideImages = {
            R.mipmap.idog,
            R.mipmap.icow,
            R.mipmap.ipeacock,
            R.mipmap.a_for_apple

    };

    public String[] slideHead = {

            "dog", "cow", "peacock","hint"
    };
    public int[] animalSound=
            {
                    R.raw.dog,
                    R.raw.cow,
                    R.raw.peacock,
                    R.raw.hint
            };

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

    public int[] getRandom()
    {
        int[] randomval= new int[4];
        ArrayList<Integer> list = new ArrayList<Integer>();
        for (int i=0; i<slideHead.length; i++) {
            list.add(new Integer(i));
        }
        Collections.shuffle(list);
        for (int i=0; i<4; i++) {
            randomval[i]=list.get(i);
        }
        return  randomval;
    }

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

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {

        layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        final View view = layoutInflater.inflate(R.layout.anitest_layout, container, false);
        final ImageView img1 = (ImageView) view.findViewById(R.id.imageanimaa);
        final ImageView img2 = (ImageView) view.findViewById(R.id.imageanimab);
        final ImageView img3 = (ImageView) view.findViewById(R.id.imageanimac);
        final ImageView img4 = (ImageView) view.findViewById(R.id.imageanimad);
        int[]temp=getRandom();
        img1.setImageResource(slideImages[temp[0]]);
        img2.setImageResource(slideImages[temp[1]]);
        img3.setImageResource(slideImages[temp[2]]);
        img4.setImageResource(slideImages[temp[3]]);

        randIndex=((int)(Math.random() * 4)); //0
        Answer=temp[randIndex]; //2
        Log.i(TAG,"Randomvalue: "+temp[0]+","+temp[1]+","+temp[2]+","+temp[3]+"Randindex "+randIndex+"Answer "+ Answer);

        container.addView(view);
        return view;
    }

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

Logcat

  

com.example.akroc.edversity I / yowag:随机值:2,0,3,1Randindex   3回答1 com.example.akroc.edversity I / yowag:随机值:   3,0,1,2Randindex 2答案1

0 个答案:

没有答案