1

我在RecyclerView中只有很少的卡片。每張卡都擁有一個自定義視頻視圖。點擊視頻觀看時,視頻應該開始播放。但在我的情況下,它只顯示藍色邊框,視頻不運行。該代碼在下面給出,在CardView中播放視頻

custom_card_view.xml:

<?xml version="1.0" encoding="utf-8"?> 

<android.support.v7.widget.CardView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/card_view" 
    android:layout_gravity="center" 
    android:layout_width="match_parent" 
    android:layout_height="200dp" 
    android:layout_margin="5dip" 
    card_view:cardCornerRadius="4dp"> 


    <com.test.components.CustomVideoView 
     android:id="@+id/videoView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="center" 
     /> 

    <TextView 
     android:id="@+id/info_text" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_margin="2dip" 

     /> 


</android.support.v7.widget.CardView> 

CustomVideoView:

import android.content.Context; 
import android.net.Uri; 
import android.util.AttributeSet; 
import android.widget.MediaController; 
import android.widget.VideoView; 

import com.test.src.R; 

    /** 
    * Created by Psych on 12/20/14. 
    */ 
    public class CustomVideoView extends VideoView { 

     MediaController mMediaController = null; 
     private String mVideoUrl = null; 

     public CustomVideoView(Context context) { 
      super(context); 
     } 

     public CustomVideoView(Context context, AttributeSet attrs) { 
      super(context, attrs); 
     } 

     /** 
     * Initialize all the basic elements here. 
     */ 
     private void initialize() { 
      mMediaController = new MediaController(getContext()); 
     } 

     /** 
     * Starts up the video. 
     * Throws a null pointer exception if the URL is not set 
     */ 
     public void startVideo() { 

      // Return as is if there is no URL 
      if (mVideoUrl == null) { 
       throw new NullPointerException(getContext().getString(R.string.video_null_pointer_message)); 
      } 

      mMediaController.setAnchorView(this); 
      setMediaController(mMediaController); 
      start(); 
     } 

     public void setmVideoUrl(String mVideoUrl) { 
      this.mVideoUrl = mVideoUrl; 
      Uri vidUri = Uri.parse(mVideoUrl); 
      setVideoURI(vidUri); 
     } 

     public String getmVideoUrl() { 
      return mVideoUrl; 
     } 
    } 

MainActivity:

public class MainActivity extends ActionBarActivity { 

    RecyclerView.LayoutManager mLayoutManager = null; 
    RecyclerView mCardsRecyclerView = null; 
    RecyclerView.Adapter mAdapter = null; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     initComponents(); 

     // use this setting to improve performance if you know that changes 
     // in content do not change the layout size of the RecyclerView 
     mCardsRecyclerView.setHasFixedSize(true); 
     mCardsRecyclerView.setLayoutManager(mLayoutManager); 
     mCardsRecyclerView.setAdapter(mAdapter); 

    } 

    /** 
    * Initialize all ui components/widgets/elements 
    */ 
    private void initComponents() { 
     mCardsRecyclerView = (RecyclerView) findViewById(R.id.cardsListView); 
     mLayoutManager = new LinearLayoutManager(this); 
     mAdapter = new NewsFeedAdapter(); 
    } 
} 

activity_main.xml中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    style="@style/AppTheme" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity"> 


    <android.support.v7.widget.RecyclerView 
     android:id="@+id/cardsListView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@id/alertButton" /> 


</RelativeLayout> 

我希望每張卡最初只顯示一個縮略圖。點擊視頻時,相關卡片應該啓動視頻(不是全部)。視頻開始時,VideoView應占據整個屏幕。我怎樣才能做到這一點?

謝謝!

+0

這方面有什麼進展? – sirvon

+0

沒有。必須把它擱置一下。 – Ahmed

回答

0

我有同樣的問題。 This從另一個stackOverflow問題的答案,爲我解決它。這是通過在VideoView中給出高度(或寬度或兩者)的明確值。例如

<VideoView 
    android:id="@+id/mVideoView" 
    android:layout_width="match_parent" 
    android:layout_height="300dp" 
    android:paddingLeft="5dp" 
    android:paddingRight="5dp"/>