如何在使用ViewPager时使用getDrawable()

时间:2016-09-30 23:20:41

标签: android android-viewpager

我试图在我的 Viewpager 中使用 Drawable Animation 。但是当我尝试使用getDrawable()方法时,它会给我空指针异常。我的ViewPager有4页。我认为这是因为我的XML文件是在Viewpager中制作的,我无法使用此方法访问它们。我已经在我的Viewpager中设置了 ImageView 的来源,但它仍然不起作用。请查看我的代码并帮助我。

我的主要活动:

import android.graphics.drawable.AnimationDrawable;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.activity_main);

    ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
    viewPager.setAdapter(new CustomPagerAdapter(this));

    ImageView myAnimation = (ImageView)findViewById(R.id.imageView2);
    final AnimationDrawable myAnimationDrawable = (AnimationDrawable)myAnimation.getDrawable();
    myAnimationDrawable.start();
  }
}

我的ViewPager适配器:

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class CustomPagerAdapter extends PagerAdapter {
  private Context mContext;

  public CustomPagerAdapter(Context context) {
    mContext = context;
  }

  @Override
  public Object instantiateItem(ViewGroup collection, int position) {
    ModelObject modelObject = ModelObject.values()[position];
    LayoutInflater inflater = LayoutInflater.from(mContext);
    ViewGroup layout = (ViewGroup) inflater.inflate(modelObject.getLayoutResId(), collection, false);
    collection.addView(layout);
    return layout;
  }

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

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

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

  @Override
  public CharSequence getPageTitle(int position) {
    ModelObject customPagerEnum = ModelObject.values()[position];
    return mContext.getString(customPagerEnum.getTitleResId());
  }
}

我的enum 添加新页面:

public enum ModelObject {
  RED(R.string.wp1, R.layout.wp1),
  BLUE(R.string.wp2, R.layout.wp2),
  LOL(R.string.wp3, R.layout.wp3),
  WP(R.string.wp5,R.layout.wp4),
  GREEN(R.string.wp4, R.layout.wp4);

  private int mTitleResId;
  private int mLayoutResId;

  ModelObject(int titleResId, int layoutResId) {
    mTitleResId = titleResId;
    mLayoutResId = layoutResId;
  }

  public int getTitleResId() {
    return mTitleResId;
  }

  public int getLayoutResId() {
    return mLayoutResId;
  }
}

如果可以,请帮帮我 抱歉英语不好。

1 个答案:

答案 0 :(得分:0)

 @Override
        public void onPageSelected(int pos) {


            //find your layout in with the position & then implement
            ImageView myAnimation = (ImageView)findViewById(R.id.imageView2); // this imageview for particular viewpager page
            final AnimationDrawable myAnimationDrawable = (AnimationDrawable)myAnimation.getDrawable();
            myAnimationDrawable.start();


        }