im不再是一般编程和Android的新功能。
我想将我的VideoView放在其自己的类中,并在BaseAdapter的getView()方法中对其进行调用,因此我的Switch案例中没有所有内容。

这是我的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();
    }


对不起,不好的解释!

最佳答案

我不确定是否能正确回答您的问题。您是说要为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);

09-27 02:31