如何使用两个片段实现视频和视频列表

Posted

技术标签:

【中文标题】如何使用两个片段实现视频和视频列表【英文标题】:How to implement Video and video list using two Fragments 【发布时间】:2012-08-30 12:41:22 【问题描述】:

我正在为HonyComb 创建一个应用程序,我必须在其中显示视频和视频列表。 为此,我创建了两个片段,一个用于显示视频,另一个用于 videoList。当用户单击视频列表时,该视频应在其他片段中的 videoview 开始播放。

我创建了自定义expandablelist 用于显示视频列表(因为我的视频被归类为组)。我必须显示视频图像、标题和描述,我已经为其创建了一个带有imageview 和两个textview 的布局。我能够显示视频列表。现在我被困在两点:

    单击视频列表时无法播放视频。 onChildCLick() 未被调用,但 onTouch()onGroupClick() 能够被调用。 我想在用户点击VideoView 时全屏播放视频。我不知道如何实现。

请帮帮我..

    fragment_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_
    android:orientation="horizontal" > 
    <fragment
        android:id="@+id/detailFragment"
        android:layout_
        android:layout_
        android:layout_weight="1"
        class="com.abs.qpr.VideoFragment" >
    </fragment>
    
    <fragment
        android:id="@+id/listFragment"
        android:layout_
        android:layout_
        android:layout_weight="0.6"
        class="com.abs.qpr.VideoListFragment" > 
    </fragment> 
    </LinearLayout>
    

    我的活动.java

     import android.os.Bundle;
    
     public class MyActivity extends android.support.v4.app.FragmentActivity
    
    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_layout);
    
    
    
    

    VideoFragment.java

    import android.net.Uri;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    
    public class VideoFragment extends Fragment
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) 
    Log.d("VideoFragment", "On Create View");
    
    View view = inflater.inflate(R.layout.activity_my_video, container,  false);
    return view;
    
     
    
      @Override
      public void onActivityCreated(Bundle savedInstanceState) 
    super.onActivityCreated(savedInstanceState);
       
    
    public void playVideo() 
    
    VideoView myVideoView2=  (VideoView)getView().findViewById(R.id.my_tecnique_video);
    String temp="android.resource://" + getActivity().getApplicationContext().getPackageName() + "/" 
    + R.raw.back_circles;
    
    System.out.println(temp + ": " + getActivity().getApplicationContext());
     Uri video = Uri.parse("android.resource://" + getActivity().getApplicationContext().getPackageName() + "/" 
                + R.raw.back_circles);
    
        myVideoView2.setVideoURI(video);
        myVideoView2.setMediaController(new MediaController(getActivity()));
        myVideoView2.requestFocus();
        myVideoView2.start();
      
    
    

    VideoListFragment.java

    public class VideoListFragment extends Fragment 
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) 
    Log.e("VideoFragment", "On Create View");
    View view = inflater.inflate(
            R.layout.activity_my_techniques_video_list, container, false);
    return view;
    
    
    @Override
    public void onActivityCreated(Bundle savedInstanceState) 
    super.onActivityCreated(savedInstanceState);
    Log.e("VideoListFragment", "On Activity Created ");
    
    ExpandableListView expList = (ExpandableListView) getView()
            .findViewById(R.id.my_tech_video_listview);
    
    expList.setAdapter(new VideoExpandableListAdapter(getActivity()));
    
    expList.setOnChildClickListener(new OnChildClickListener() 
    
     @Override
     public boolean onChildClick(ExpandableListView expandablelistview,
                View view, int i, int j, long l) 
    
            // TODO Auto-generated method stub
            Log.d("Checking.....", "onGroupClick()");
    
            VideoFragment fragment = (VideoFragment) getFragmentManager()
                    .findFragmentById(R.id.detailFragment);
            if (fragment != null && fragment.isInLayout()) 
                fragment.playVideo();
             else 
    
                Intent videointent = new Intent(getActivity()
                        .getApplicationContext(), VideoActivity.class);
    
                startActivity(videointent);
    
            
    
            return false;
        
    );
    
    

    VideoExpandableListAdapter.java

    public class VideoExpandableListAdapter extends BaseExpandableListAdapter
    
    private Context myContext;
    ArrayList<VideoListVO> videoListVO;
    
    public VideoExpandableListAdapter(Context context) 
    myContext = context;        
    
    
    @Override
    public Object getChild(int groupPosition, int childPosition) 
    // TODO Auto-generated method stub
    return null;
    
    
    @Override
    public long getChildId(int groupPosition, int childPosition) 
    // TODO Auto-generated method stub
    return 0;
    
    
    @Override
    public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) 
    
    if (convertView == null) 
        LayoutInflater inflater = (LayoutInflater) myContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.childrow, null);
    
    
    
    VideoListVO vlvo= videoListVO.get(groupPosition);
    VideoDetailsVO vdvo=vlvo.getVideoDetailList().get(childPosition);
    
    InputStream is = null;
    try 
        String imageName=vdvo.getImageName();
        imageName="images/"+imageName+".png";
      is = myContext.getResources().getAssets().open(imageName);
     catch (IOException e) 
      Log.w("EL", e);
    
    
    Bitmap image = BitmapFactory.decodeStream(is);
    
    ImageView ib2 = (ImageView) convertView.findViewById( R.id.video_image);
    ib2.setImageBitmap(image);
    
    TextView titleText=(TextView)convertView.findViewById(R.id.video_title_text);
    titleText.setText(vdvo.getVideoTitle());
    
    TextView descriptionText=(TextView)convertView.findViewById(R.id.video_description);
    descriptionText.setText(vdvo.getVideoDescription());
    
    return convertView;
    
    
    @Override
    public int getChildrenCount(int groupPosition) 
    // TODO Auto-generated method stub
    return videoListVO.get(groupPosition).getVideoDetailList().size();
    
    
    @Override
    public Object getGroup(int groupPosition) 
    // TODO Auto-generated method stub
    return null;
    
    
    @Override
    public int getGroupCount() 
    // TODO Auto-generated method stub
    return videoListVO.size();
    
    
    @Override
    public long getGroupId(int groupPosition) 
    // TODO Auto-generated method stub
    return 0;
    
    
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) 
    
    if (convertView == null) 
        LayoutInflater inflater =  (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.group_row, null);
       
    TextView tvGroupName = (TextView) convertView.findViewById(R.id.tvgrpname);
    tvGroupName.setText(videoListVO.get(groupPosition).getVideoType());
    
    return convertView;
    
    
    @Override
    public boolean hasStableIds() 
    // TODO Auto-generated method stub
    return false;
    
    
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) 
    // TODO Auto-generated method stub
    return true;
    
    
    
    

【问题讨论】:

能否分享您的代码以便我们提供帮助 @EldhoseMBabu 我已经添加了代码.. 【参考方案1】:

请从 ListFragment 扩展 VideoListFragment(正确方式),您可以在其中直接设置适配器,可以收听 listitemclick 事件等,这将有助于解决您的问题。

为此,您可以阅读this 教程。

然后解决您的第二个问题(点击时全屏查看):

当用户触摸视频部分两次(点击)(您可以借助 ontouchlistener 和计时器来检查是否发生连续触摸),调用视图部分中的另一个活动,设计为全屏看法。所以当用户点击返回时,它会回到片段活动。

【讨论】:

感谢您的快速回复...我在我的视频列表中使用ExpandableListView 我无法扩展ListFragment并且不支持扩展ExpandableListFragment。我会尝试你的逻辑视频视图。 你能在我的代码中看到为什么onChildClickListener()没有被调用 我没有发现你的代码有任何问题。你可以试试 setOnItemClickListener 吗??

以上是关于如何使用两个片段实现视频和视频列表的主要内容,如果未能解决你的问题,请参考以下文章

如何解析 MPD 清单视频文件并获取图像适配集的片段?

如何在不下载整个片段的情况下使用媒体源缓冲区和分段 mp4 在特定时间启动视频?

Mediaplayer在切换片段时没有暂停

如何获取 .mp4 视频文件的 Dash 片段

如何阻止从我的网站下载视频

Camtasia如何剪辑视频,剪切视频的片段