【问题标题】:Why are viewpager swipe slow?为什么 viewpager 滑动缓慢?
【发布时间】:2012-09-25 20:41:17
【问题描述】:

我有一个ViewPagerlayouts。布局有大约30张图片,左右滑动是正常的,但从最后一页滑动到第一页很慢。

很慢;

if (position == 0){
  ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
    myPager.setCurrentItem(pageCount-2,false); 
} else if (position == pageCount-1){ 
  ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
    myPager.setCurrentItem(1,false); 
} 

非常感谢,

我的页面适配器;

package com.example.pictures;

import android.content.Context;
import android.media.AudioManager;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;


public class MyPagerAdapter extends  PagerAdapter{

public int getCount() {
    return 30;
}

public Object instantiateItem(View collection, int position) {

    View view=null;

    LayoutInflater inflater = (LayoutInflater) collection.getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    int resId = 0;
    switch (position) {
    case 0:
        resId = R.layout.picture1;
        view = inflater.inflate(resId, null);         
        break;
    case 1:
        resId = R.layout.picture2;   
        view = inflater.inflate(resId, null);
        break;
    case 2:
        resId = R.layout.picture3;
        view = inflater.inflate(resId, null);
        break;
    case 3:
        resId = R.layout.picture4;
        view = inflater.inflate(resId, null);
        break;
    case 4:
        resId = R.layout.picture5;
        view = inflater.inflate(resId, null);   
        break;
    case 5:
        resId = R.layout.picture6;
        view = inflater.inflate(resId, null);
        break;
    .....
    .....

    }

    ((ViewPager) collection).addView(view, 0);

    return view;
}

@SuppressWarnings("unused")
private Context getApplicationContext() {
    // TODO Auto-generated method stub
    return null;
}


private void setVolumeControlStream(int streamMusic) {
    // TODO Auto-generated method stub

}


@SuppressWarnings("unused")
private Context getBaseContext() {
    // TODO Auto-generated method stub
    return null;
}


@SuppressWarnings("unused")
private PagerAdapter findViewById(int myfivepanelpager) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
    ((ViewPager) arg0).removeView((View) arg2);
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
    return arg0 == ((View) arg1);

}

@Override
public Parcelable saveState() {
    return null;
}

public static Integer getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

}

OnPageChangeListener;

package com.example.pictures;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


public class Pictures extends Activity implements OnPageChangeListener{
SoundManager snd; 
int sound1,sound2,sound3;
View view=null;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.picturespage);

 MyPagerAdapter adapter = new MyPagerAdapter();
 ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
 myPager.setAdapter(adapter);
 myPager.setCurrentItem(15); 
 myPager.setOnPageChangeListener(this);
snd = new SoundManager(this);
 sound1 = snd.load(R.raw.sound1);
 sound2 = snd.load(R.raw.sound2);
 sound3 = snd.load(R.raw.sound3);

}
public void onPageScrollStateChanged(int arg0) {
    // TODO Auto-generated method stub

}

public void onPageScrolled(int arg0, float arg1, int arg2) {
    // TODO Auto-generated method stub

}

public void onPageSelected(int position) {
    // TODO Auto-generated method stub
      int pageCount = getCount(); 
        if (position == 0){ 
      ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
            myPager.setCurrentItem(pageCount-2,false); 
        } else if (position == pageCount-1){
       ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager); 
            myPager.setCurrentItem(1,false); 
        } 

    switch (position) {
    case 0:

        break;
    case 1:
        snd.play(sound1);
        break;
    case 2:
      snd.play(sound2);
        break;
    case 3:
      snd.play(sound3);
        break;
    case 4:
       Toast.makeText(this, "4", Toast.LENGTH_SHORT).show();
        break;
    case 5:
        Toast.makeText(this, "5", Toast.LENGTH_SHORT).show();
        break;
    ....
    ....
    }
}

public int getCount() { 
return count; 
} 
};

【问题讨论】:

  • 请去掉instantiateItem中的那个switch语句,不需要它。只需动态获取资源 id —— 在我的脑海中,它会类似于 int resId = getResources().getIdentifier("picture" + (position + 1), "layout", getPackageName());
  • 怎么办,能不能举个例子,我要picture1-->play Sound1,picture2-->play sound2...
  • 您应该删除这两个 switch 语句,因为它们非常多余。在instantiateItem 中,只需像我说的那样获取 resId,然后在其后面写上view = inflater.inflate(resId, null);。在onPageSelected中,使用getResources().getIdentifier同样的方式加载正确的声音文件。

标签: android android-layout android-viewpager


【解决方案1】:

你不需要在onPageSelected中每次调用ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager),只需要在onCreate中。将 ViewPager myPager 设为类字段,并在所有活动方法中使用相同的对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-13
    • 2018-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多