2014-09-22 24 views
1

即时退出编程和Android的新功能。 我想让我的VideoView在它自己的类中,并在我的BaseAdapter中的getView()方法中调用它,所以我没有在我的Switch案例中的所有内容。在BaseAdapter中为VideoView设置VideoView

这是我的getView()方法。我知道MOKVideoView videoView = new MOKVideoView(context);部分是不正确的,但我无法弄清楚如何调用它。

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    int resource = 0; 

    switch (getItemViewType(position)) { 
     case 0: 
      resource = R.layout.headline_item; 
      break; 
     case 1: 
      resource = R.layout.videoplayer_item; 
      break; 
    } 

    View rowView = convertView; 

    if (rowView == null) { 
     rowView = mLayoutInflater.inflate(resource, null); 
    } 

    switch (resource){ 
     case R.layout.headline_item: 
      TextView headline = (TextView) rowView.findViewById(R.id.headline); 
      headline.setText("Hey Hey"); 
      break; 
     case R.layout.videoplayer_item: 
      MOKVideoView videoView = new MOKVideoView(context); 
      return videoView.getmVideoView(); 
    } 

    return rowView; 
} 

,在这里我MOKVideoView类

public MOKVideoView(Context context) { 
    super(context); 
    initVideoView(); 
} 

public MOKVideoView(Context context, AttributeSet attrs) { 
    this(context, attrs, 0); 
    initVideoView(); 
} 

public MOKVideoView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    initVideoView(); 
} 

public View initVideoView() { 
    View view = mLayoutInflater.inflate(R.layout.videoplayer_item, null, false); 
    mVideoView = (VideoView) view.findViewById(R.id.video); 
    countDownText = (TextView) view.findViewById(R.id.timer); 
    countDownTimer = new MyCountDownTimer(startTime, interval); 

    try { 
     //set the uri of the video to be played 
     mVideoView.setVideoURI(Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.test_vid)); 
    } catch (Exception e) { 
     Log.e("Error", e.getMessage()); 
     e.printStackTrace(); 
    } 

抱歉不好解释!

回答

1

我不知道我是否正确回答您的问题。你的意思是你想为每个listview元素创建视频视图吗?如果是这样,你必须创建一个视频视图的XML文件,并让你的适配器扩展它。

例:

listview_item.xml: 
<linearLayout 
    android:layout_width = "fill_parent" 
    android:layout_height = "fill_parent" 

    <MOKVideoView  
     android:layout_width = "fill_parent" 
     android:layout_height = "fill_parent" 
     android:id = "MOKVideo" /> 

</linearLayout> 

和适配器的时候可以调用findViewById()来查找视频观看:

MOKVideoView mokvideo = (MOKVideoView) getView.findViewById(R.id.MOKVideo); 
+0

谢谢,我理解了它看到您的帖子:)之后。不知道这是可能的。 – 2014-09-23 12:58:34