0

내 활동에 ViewPager가 있습니다. PagerAdapter로 어댑터를 설정했습니다.
어댑터 항목은 VideoViews 및 ImageViews입니다. 다른 활동에서 미리보기 이미지를 클릭하면 ViewPager가 나타납니다.
그러나 이미지 페이지를 선택하면 ViewPager 항목 (3) _left 및 활성 항목의 오른쪽 항목의 기본 인스턴스 수 때문에 비디오가 백그라운드에서 재생 중입니다.
ViewPager에서 이미지를 볼 때 동영상 재생을 처리하는 방법에 대한 아이디어.
내 이미지가 어떻게 표시되는지, 올바른 항목은 videoview 및 MediaController입니다. screenshot of my applicationViewPager와 3 개의 인스턴스를 한 번에 인스턴스화하면 왼쪽 이미지에서 비디오가 재생됩니다.

어댑터 클래스 : ViewPager있다

public class FullScreenViewAdapter extends PagerAdapter { 
ActivityFullscreenSlideView _activity; 
LayoutInflater inflater; 
ArrayList<HashMap<String, Integer>> _media; 
VideoView videosrc; 
ImageView imageView; 
MediaController mediaController; 
View viewLayout; 

public FullScreenViewAdapter(ActivityFullscreenSlideView activityFullscreenSlideView, 
          ArrayList<HashMap<String, Integer>> media) { 
    this._activity = activityFullscreenSlideView; 
    Collections.reverse(media); 
    this._media = media; 
} 

@Override 
public int getCount() { 
    return _media.size(); 
} 

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

@Override 
public Object instantiateItem(ViewGroup container, int position) { 
    inflater = (LayoutInflater) _activity 
      .getSystemService(G.context.LAYOUT_INFLATER_SERVICE); 
    if (_media.get(position).get("type").equals(ActivityAppPage.IMAGE_TYPE)) { 
     viewLayout = inflater.inflate(R.layout.fullscreenview_image, container, false); 
     imageView = (ImageView) viewLayout.findViewById(R.id.fullscreenimage); 
     imageView.setImageResource(_media.get(position).get("src")); 
    } else if (_media.get(position).get("type").equals(ActivityAppPage.VIDEO_TYPE)) { 
     viewLayout = inflater.inflate(R.layout.fullscreenview_video, container, false); 
     videosrc = (VideoView) viewLayout.findViewById(R.id.video); 
     Uri videoAddress = Uri.parse("android.resource://" + _activity.getPackageName() + "/" 
       + _media.get(position).get("src")); 
     videosrc.setVideoURI(videoAddress); 
     DisplayMetrics metrics = new DisplayMetrics(); 
     _activity.getWindowManager().getDefaultDisplay().getMetrics(metrics); 
     videosrc.setLayoutParams(new LinearLayout.LayoutParams(metrics.widthPixels, LinearLayout.LayoutParams.WRAP_CONTENT)); 
     if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { 
      mediaController = new MediaController(_activity); 
      mediaController.setAnchorView(videosrc); 
      videosrc.setMediaController(mediaController); 
      videosrc.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { 
       @Override 
       public void onCompletion(MediaPlayer mp) { 
        mediaController.hide(); 
       } 
      }); 
      videosrc.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { 
       @Override 
       public void onPrepared(MediaPlayer mp) { 
        Log.i("LOG", "\nonprepared"); 
        mediaController.show(0); 
        videosrc.start(); 
       } 
      }); 
     } 
    } else { 
     videosrc.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { 
      @Override 
      public void onPrepared(MediaPlayer mp) { 
       Log.i("LOG", "\nonprepared OLD API"); 
       videosrc.start(); 
      } 
     }); 
    } 
    ((ViewPager) container).addView(viewLayout); 
    return viewLayout; 
} 

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

내 활동 : 내가해야 할 일을했을 것을 발견

public class ActivityFullscreenSlideView extends AppCompatActivity 
    implements View.OnClickListener { 
private FullScreenViewAdapter adapter; 
private ViewPager viewPager; 
int dotsCount; 
ImageView[] dots; 
LinearLayout pager_indicator; 
ArrayList<Integer> viewPagersArray; 
ArrayList<HashMap<String, Integer>> media; 
ActivityFullscreenSlideView thisActivity = this; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.fullscreen_viewpager); 
    viewPager = (ViewPager) findViewById(R.id.pager); 
    Intent i = getIntent(); 
    viewPagersArray = (ArrayList<Integer>) i.getExtras().get("viewPagersArray"); 
    int position = i.getIntExtra("position", 0); 
    media = (ArrayList<HashMap<String, Integer>>) i.getExtras().get("mAdapter.media"); 
    adapter = new FullScreenViewAdapter(ActivityFullscreenSlideView.this, media); 
    viewPager.setAdapter(adapter); 
    int RtlPosition = media.size() - position - 1; 
    viewPager.setCurrentItem(RtlPosition); 
    pager_indicator = (LinearLayout) findViewById(R.id.viewPagerCountDots); 
    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { 
     @Override 
     public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 
     } 

     @Override 
     public void onPageSelected(final int position) { 
      if (adapter.mediaController != null) 
       if (media.get(position).get("type").equals(ActivityAppPage.IMAGE_TYPE)) { 
        Log.i("LOG", "Image Selected"); 
        adapter.mediaController.hide(); 
        adapter.videosrc.stopPlayback(); 
       } else if (media.get(position).get("type").equals(ActivityAppPage.VIDEO_TYPE)) { 
        Log.i("LOG", "Video Selected"); 
        adapter.mediaController.show(0); 
       } 
      for (int i = 0; i < dotsCount; i++) { 
       dots[i].setImageDrawable(getResources().getDrawable(R.drawable.nonselecteditem_dot)); 
      } 
      dots[position].setImageDrawable(getResources().getDrawable(R.drawable.selecteditem_dot)); 
     } 
     @Override 
     public void onPageScrollStateChanged(int state) { 
     } 
    }); 
    setUiPageViewController(RtlPosition); 
} 

답변

0

. 방금 어댑터에서 "setOnFocusChangeListener"를 사용했습니다.

videosrc.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
       @Override 
       public void onFocusChange(View v, boolean hasFocus) { 
        if (hasFocus) 
         videosrc.start(); 
        else 
         videosrc.stopPlayback(); 
       } 
      }); 

또한 "OnPreparedListener"도 제거했습니다.