简体   繁体   中英

How to listen a method from another class to stop a VideoView? -Android,Java

I'm having trouble about calling a method from another class. I have this class;

protected class ViewHolder { 
    protected VideoView itemVideo;
    protected ImageView itemBackgroundBlack;
    protected ImageView itemPlayIcon; }

And I have this method in another class which plays itemVideo when itemPlayIcon clicked.

public class CategoryItemAdapter extends PagerAdapter {
    public void setItemInfoView(int position, final ViewHolder holder) {
        holder.itemPlayIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.itemPlayIcon.setVisibility(View.INVISIBLE);
                holder.itemBackgroundBlack.setVisibility(View.VISIBLE);
                holder.itemVideo.setVisibility(View.VISIBLE);
                holder.itemVideo.start();
            }
        });
    }
}

And another class for

public class CategoryItemActivity extends FragmentActivity{
     protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.category_item_layout);

          init();
     }

     private void init() {
          final ViewPager pager = (ViewPager) findViewById(R.id.pager);
          pager.setAdapter(adapter);
          pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i2) {
            }

            @Override
            public void onPageSelected(int i) {
              int x = imageViews.get(i).getMeasuredWidth() * i + 
              imageViews.get(i).getMeasuredWidth() / 2;                       
              x = x - getWindowManager().getDefaultDisplay().getWidth() / 2;
              scrollView.smoothScrollTo(x, scrollView.getScrollY());

              for(int k = 0; k < imageViews.size(); k++) {
                  imageViews.get(k).setVisibility(View.VISIBLE);
              }
              imageViews.get(i).setVisibility(View.GONE);
            }

            @Override
            public void onPageScrollStateChanged(int i) {
            }
          });
      }
  }

And I want to stop the itemVideo when page is scrolled (means onPageScrollStateChanged() is used). I need a listener I know but I couldn't handle it. Please help. Thank you.

You can implement an interface to your other clase and when video play end handle it; http://www.tutorialspoint.com/java/java_interfaces.htm

u have to pass the holderView class instance to CategoryItemActivity, Then u can call stop method of HolderView class from ur CategoryItemActivity. If ur directly passing class context it will cause to coupling. So u have to use super class concept.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM