如何在父类中访问子类变量?

时间:2013-05-30 10:11:48

标签: android

大家好,              我初学到android,我对访问变量有一个疑问..在我的应用中, 我必须显示来自drawable的上一张和下一张图片..它已经完成了.. 但我的问题是,它应该根据子类中的图像播放父类中的视频,这样我就需要访问父类中的子类变量....是否可能?任何想法..谢谢!

// Main class
public class TransitionViewExampleActivity extends Activity implements
    OnClickListener {

    ImageButton play;
    private final int ANIMATION_DURATION_MSEC = 1000;

    private Button _leftButton;
    private Button _rightButton;
    private TransitionView _mainView;

    VideoView videoView;
    Context context;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    setContentView(R.layout.main);

    play = (ImageButton) findViewById(R.id.ib_play_btn);
    play.setOnClickListener(this);

    _leftButton = (Button) findViewById(R.id.buttonLeft);
    _rightButton = (Button) findViewById(R.id.buttonRight);
    _mainView = (TransitionView) findViewById(R.id.mainView);


    _mainView.setVisibility(View.VISIBLE);

}


    public void onClick(View v) {

 videoView = (VideoView) findViewById(R.id.videoView1);


    switch (v.getId()) {
    // TODO Auto-generated method stub

    case R.id.ib_play_btn:



    //  Toast.makeText(this, "Please Login to view video",Toast.LENGTH_SHORT).show();   

        play.setVisibility(View.INVISIBLE);
        _mainView.setVisibility(View.INVISIBLE);
        _leftButton.setVisibility(View.INVISIBLE);
        _rightButton.setVisibility(View.INVISIBLE);




        MediaController mediaController = new MediaController(this);
        mediaController.setAnchorView(videoView);
        videoView.setMediaController(mediaController);                    
      String path = "android.resource://" + getPackageName() + "/" + R.raw.test1;
      videoView.setVideoURI(Uri.parse(path));
      videoView.start();

        break;


    }

    if (v == _leftButton) {

        // fadeIn.setDuration(ANIMATION_DURATION_MSEC);

        this.overridePendingTransition(R.anim.slide_in_left,
                R.anim.slide_out_right);

        _mainView.changePage(false);
    } else if (v == _rightButton) {
        this.overridePendingTransition(R.anim.slide_in_right,
                R.anim.slide_out_left);

        _mainView.changePage(true);

    }

   }

 }




// Sub class (Child class)


    class TransitionView extends RelativeLayout {



   /** One of the two in-memory art images */
   private ImageView _artView1;
   /** The other of the two in-memory art images */
   private ImageView _artView2;
  /** Length of art view transition animation, in milliseconds */
   private final int ANIMATION_DURATION_MSEC = 1000;
  /** The underlying ImageSwitcher that performs transitions */
   private ImageSwitcher _imageSwitcher;
  /** Index into _imageIds array */
  private int _currentImage = 0;
  /** All available art image resource ids */

private final Integer[] _imageIds = { R.drawable.thuppaki,
        R.drawable.gouravam, R.drawable.splash2, R.drawable.pic04 };

Animation fadeIn, fadeOut;

Context context;


public TransitionView(Context context) {
    super(context);
    customInit(context);
}


private void customInit(Context context) {

    _imageSwitcher = new ImageSwitcher(context);


    _imageSwitcher.setInAnimation(fadeIn);
    _imageSwitcher.setOutAnimation(fadeOut);

    _artView1 = new ImageView(context);
    _artView1.setImageResource(_imageIds[_currentImage]);

    _artView2 = new ImageView(context);
    _artView2.setImageResource(_imageIds[_currentImage + 1]);

    LayoutParams fullScreenLayout = new LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    _imageSwitcher.addView(_artView1, 0, fullScreenLayout);
    _imageSwitcher.addView(_artView2, 1, fullScreenLayout);
    _imageSwitcher.setDisplayedChild(0);
    addView(_imageSwitcher, fullScreenLayout);
}

/** @see android.view.View#View(Context, AttributeSet) */
public TransitionView(Context context, AttributeSet attrs) {
    super(context, attrs);
    customInit(context);
}

/** @see android.view.View#View(Context, AttributeSet, int) */
public TransitionView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    customInit(context);
}


public void changePage(boolean pageRight) {
    _currentImage = (pageRight) ? (_currentImage + 1) : (_currentImage - 1);
    if (_currentImage < 0) {

        _currentImage = _imageIds.length - 1;
    } else if (_currentImage >= _imageIds.length) {
        _currentImage = 0;


    }

    if (_imageSwitcher.getCurrentView() == _artView1) {

        _artView2.setImageResource(_imageIds[_currentImage]);
        _imageSwitcher.showNext();
    } else {
        _artView1.setImageResource(_imageIds[_currentImage]);
        _imageSwitcher.showPrevious();
    }
}   

}

最后,我想要加入子类变量 _imageIds 和 父类中的 _currentImage ........任何想法???

3 个答案:

答案 0 :(得分:1)

您总是可以在父类中创建一个方法来进行常规工作,然后在子类中重新定义父方法,然后对_imageIds_currentImage进行操作。

示例:

class Parent{

    void doSomething(){
        //your general code
    }
}
class Child extends Parent{

    @Override
    void doSomething(){
        super.doSomething(); //calls the parents function

        //add the code where you manipulate the variables
    }
}

答案 1 :(得分:0)

没有办法做到这一点。您可以创建子类的多个对象,并且不清楚将访问哪个对象的成员。

只有在成员是静态的情况下才能这样做。在这种情况下,您可以将它们作为ClassName.memberName

进行访问

答案 2 :(得分:0)

我会使用回调方法创建Interface,例如onImageSelcted。像这样:

public interface OnImageSelectedListener(){

     public onImageSelected(String imageName);

}

然后在父类中implement

public class MyparentClass implements OnImageSelectedListner {

    @Override
    protected void onImageSelected(String imageName){
       playVideo(imageName);
    }

    childClass.setOnImageSelectedListener(this);

}


public class MyChildClass{

    private OnImageSelectedListener listener;

    public setOnImageSelectedListener(OnImageSelectedListener listener){
        this.listener = listener;
    }


    // when the image is selected
    listernet.onImageSelected(imageName);
}