2014-09-22 27 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