如何从一个片段中制作cardview onclick onclick开放视频而不制作多个片段
我想使用具有 cardView 的 fragment 进行页面。假设我有4个卡片视图。我希望可以单击卡片视图,并且单击时,它们将使用 videoview 打开一个新片段。我只能制作一个片段,可以根据单击哪个cardView播放不同的视频。
意思是,假设我单击CardView1,它将打开一个带有Video1的片段,然后单击CardView 2,它将从相同的片段开放,但使用Video2打开。是否只能使用一个片段具有多个视频评论?
如果可能,我该怎么做?有人请帮忙。
这是我的代码 fragment_exercise.xml(带卡视图的片段)
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fillViewport="true"
tools:context=".ExerciseFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:orientation="vertical"
android:background="#FFF8DD">
<ImageView
android:id="@+id/logo2"
android:layout_width="149dp"
android:layout_height="48dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="30dp"
android:layout_marginRight="50dp"
android:src="@drawable/logobg" />
<androidx.cardview.widget.CardView
android:id="@+id/c1"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="100dp"
android:layout_marginLeft="50dp"
app:cardCornerRadius="26dp"
app:cardElevation="10dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/gradient16"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="4dp"
android:layout_marginTop="80dp"
android:fontFamily="@font/poppins"
android:text="Running"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="18dp"
android:textStyle="bold" />
</RelativeLayout>
<ImageView
android:layout_width="102dp"
android:layout_height="88dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:src="@drawable/running" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/c2"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="100dp"
android:layout_marginLeft="220dp"
app:cardCornerRadius="26dp"
app:cardElevation="10dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/gradient3"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="4dp"
android:layout_marginTop="80dp"
android:fontFamily="@font/poppins"
android:text="Walking"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="18dp"
android:textStyle="bold" />
</RelativeLayout>
<ImageView
android:layout_width="102dp"
android:layout_height="88dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:src="@drawable/walking" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/c3"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="250dp"
android:layout_marginLeft="50dp"
app:cardCornerRadius="26dp"
app:cardElevation="10dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/gradient22"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="4dp"
android:layout_marginTop="80dp"
android:fontFamily="@font/poppins"
android:text="Aerobics"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="18dp"
android:textStyle="bold" />
</RelativeLayout>
<ImageView
android:layout_width="102dp"
android:layout_height="88dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:src="@drawable/aerobatic" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/c4"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="250dp"
android:layout_marginLeft="220dp"
app:cardCornerRadius="26dp"
app:cardElevation="10dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/gradient13"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="4dp"
android:layout_marginTop="80dp"
android:fontFamily="@font/poppins"
android:text="Yoga"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="18dp"
android:textStyle="bold" />
</RelativeLayout>
<ImageView
android:layout_width="102dp"
android:layout_height="88dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:src="@drawable/yoga2" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/c5"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="400dp"
android:layout_marginLeft="50dp"
app:cardCornerRadius="26dp"
app:cardElevation="10dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/gradient29"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="4dp"
android:layout_marginTop="80dp"
android:fontFamily="@font/poppins"
android:text="Boxing"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="18dp"
android:textStyle="bold" />
</RelativeLayout>
<ImageView
android:layout_width="102dp"
android:layout_height="88dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:src="@drawable/boxing" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/c6"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="400dp"
android:layout_marginLeft="220dp"
app:cardCornerRadius="26dp"
app:cardElevation="10dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/gradient10"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="4dp"
android:layout_marginTop="80dp"
android:fontFamily="@font/poppins"
android:text="Cycling"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="18dp"
android:textStyle="bold" />
</RelativeLayout>
<ImageView
android:layout_width="102dp"
android:layout_height="88dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:src="@drawable/biking" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/c7"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="550dp"
android:layout_marginLeft="50dp"
app:cardCornerRadius="26dp"
app:cardElevation="10dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/gradient24"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="4dp"
android:layout_marginTop="80dp"
android:fontFamily="@font/poppins"
android:text="Dancing"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="18dp"
android:textStyle="bold" />
</RelativeLayout>
<ImageView
android:layout_width="102dp"
android:layout_height="88dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:src="@drawable/dance" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/c8"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="550dp"
android:layout_marginLeft="220dp"
app:cardCornerRadius="26dp"
app:cardElevation="10dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/gradient30"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="4dp"
android:layout_marginTop="80dp"
android:fontFamily="@font/poppins"
android:text="Swimming"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="18dp"
android:textStyle="bold" />
</RelativeLayout>
<ImageView
android:layout_width="102dp"
android:layout_height="88dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:src="@drawable/m2" />
</androidx.cardview.widget.CardView>
</RelativeLayout>
</ScrollView>
coramefragment.java
public class ExerciseFragment extends Fragment implements View.OnClickListener{
View v;
CardView cv1, cv2, cv3, cv4, cv5, cv6, cv7, cv8;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_exercise, container, false);
cv1 = (CardView) v.findViewById(R.id.c1);
cv1.setOnClickListener(this);
cv2 = (CardView) v.findViewById(R.id.c2);
cv2.setOnClickListener(this);
cv3 = (CardView) v.findViewById(R.id.c3);
cv3.setOnClickListener(this);
cv4 = (CardView) v.findViewById(R.id.c4);
cv4.setOnClickListener(this);
cv5 = (CardView) v.findViewById(R.id.c5);
cv5.setOnClickListener(this);
cv6 = (CardView) v.findViewById(R.id.c6);
cv6.setOnClickListener(this);
cv7 = (CardView) v.findViewById(R.id.c7);
cv7.setOnClickListener(this);
cv8 = (CardView) v.findViewById(R.id.c8);
cv8.setOnClickListener(this);
// Inflate the layout for this fragment
return v;
}
@Override
public void onClick(View v) {
Fragment fragment1, fragment2, fragment3, fragment4, fragment5, fragment6, fragment7, fragment8;
switch (v.getId()) {
case R.id.c1:
fragment1 = new VideoFragment();
FragmentTransaction transaction1 = getParentFragmentManager().beginTransaction();
transaction1.replace(R.id.container,fragment1);
transaction1.commit();
break;
case R.id.c2:
fragment2 = new VideoFragment();
FragmentTransaction transaction2 = getParentFragmentManager().beginTransaction();
transaction2.replace(R.id.container,fragment2);
transaction2.commit();
break;
case R.id.c3:
fragment3 = new VideoFragment();
FragmentTransaction transaction3 = getParentFragmentManager().beginTransaction();
transaction3.replace(R.id.container,fragment3);
transaction3.commit();
break;
case R.id.c4:
fragment4 = new VideoFragment();
FragmentTransaction transaction4 = getParentFragmentManager().beginTransaction();
transaction4.replace(R.id.container,fragment4);
transaction4.commit();
break;
case R.id.c5:
fragment5 = new VideoFragment();
FragmentTransaction transaction5 = getParentFragmentManager().beginTransaction();
transaction5.replace(R.id.container,fragment5);
transaction5.commit();
break;
case R.id.c6:
fragment6 = new VideoFragment();
FragmentTransaction transaction6 = getParentFragmentManager().beginTransaction();
transaction6.replace(R.id.container,fragment6);
transaction6.commit();
break;
case R.id.c7:
fragment7 = new VideoFragment();
FragmentTransaction transaction7 = getParentFragmentManager().beginTransaction();
transaction7.replace(R.id.container,fragment7);
transaction7.commit();
break;
case R.id.c8:
fragment8 = new VideoFragment();
FragmentTransaction transaction8 = getParentFragmentManager().beginTransaction();
transaction8.replace(R.id.container,fragment8);
transaction8.commit();
break;
}
}
}
fragment_video.xml(带视频的片段)
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".VideoFragment">
<VideoView
android:id="@+id/videoView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" />
</FrameLayout>
videofragment.java 这就是我现在拥有的
public class VideoFragment extends Fragment {
View v;
VideoView view;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
view = (VideoView) v.findViewById(R.id.videoView1);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(view);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()+"/raw/boxing.mp4");
view.setMediaController(mediaController);
view.setVideoURI(uri);
view.requestFocus();
view.start();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_exercise, container, false);
return v;
}
}
该代码在行中有错误:
MediaController mediaController = new MediaController(this);
请提供帮助,我如何制作一个片段,该片段将根据点击的cardviews播放不同的视频而无需制作多个片段。是否有可能,如果是,请帮助我编写代码。太感谢了。
I want to make a page using fragment that has a CardView. Let say I have 4 cardviews. I want the cardviews to be clickable and when clicked, they will open a new fragment with a VideoView. Can I only make one fragment that can play different video based on which cardview is clicked.
Meaning, let say, I click on cardview1, it will open a fragment with video1, then i click cardview 2, it will open from the same fragment but with video2. Is it possible to use only one fragment to have more than one videoView?
If possible how can I do that? someone please help.
this is my code
fragment_exercise.xml (fragment with cardviews)
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fillViewport="true"
tools:context=".ExerciseFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:orientation="vertical"
android:background="#FFF8DD">
<ImageView
android:id="@+id/logo2"
android:layout_width="149dp"
android:layout_height="48dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="30dp"
android:layout_marginRight="50dp"
android:src="@drawable/logobg" />
<androidx.cardview.widget.CardView
android:id="@+id/c1"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="100dp"
android:layout_marginLeft="50dp"
app:cardCornerRadius="26dp"
app:cardElevation="10dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/gradient16"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="4dp"
android:layout_marginTop="80dp"
android:fontFamily="@font/poppins"
android:text="Running"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="18dp"
android:textStyle="bold" />
</RelativeLayout>
<ImageView
android:layout_width="102dp"
android:layout_height="88dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:src="@drawable/running" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/c2"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="100dp"
android:layout_marginLeft="220dp"
app:cardCornerRadius="26dp"
app:cardElevation="10dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/gradient3"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="4dp"
android:layout_marginTop="80dp"
android:fontFamily="@font/poppins"
android:text="Walking"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="18dp"
android:textStyle="bold" />
</RelativeLayout>
<ImageView
android:layout_width="102dp"
android:layout_height="88dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:src="@drawable/walking" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/c3"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="250dp"
android:layout_marginLeft="50dp"
app:cardCornerRadius="26dp"
app:cardElevation="10dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/gradient22"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="4dp"
android:layout_marginTop="80dp"
android:fontFamily="@font/poppins"
android:text="Aerobics"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="18dp"
android:textStyle="bold" />
</RelativeLayout>
<ImageView
android:layout_width="102dp"
android:layout_height="88dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:src="@drawable/aerobatic" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/c4"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="250dp"
android:layout_marginLeft="220dp"
app:cardCornerRadius="26dp"
app:cardElevation="10dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/gradient13"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="4dp"
android:layout_marginTop="80dp"
android:fontFamily="@font/poppins"
android:text="Yoga"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="18dp"
android:textStyle="bold" />
</RelativeLayout>
<ImageView
android:layout_width="102dp"
android:layout_height="88dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:src="@drawable/yoga2" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/c5"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="400dp"
android:layout_marginLeft="50dp"
app:cardCornerRadius="26dp"
app:cardElevation="10dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/gradient29"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="4dp"
android:layout_marginTop="80dp"
android:fontFamily="@font/poppins"
android:text="Boxing"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="18dp"
android:textStyle="bold" />
</RelativeLayout>
<ImageView
android:layout_width="102dp"
android:layout_height="88dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:src="@drawable/boxing" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/c6"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="400dp"
android:layout_marginLeft="220dp"
app:cardCornerRadius="26dp"
app:cardElevation="10dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/gradient10"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="4dp"
android:layout_marginTop="80dp"
android:fontFamily="@font/poppins"
android:text="Cycling"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="18dp"
android:textStyle="bold" />
</RelativeLayout>
<ImageView
android:layout_width="102dp"
android:layout_height="88dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:src="@drawable/biking" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/c7"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="550dp"
android:layout_marginLeft="50dp"
app:cardCornerRadius="26dp"
app:cardElevation="10dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/gradient24"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="4dp"
android:layout_marginTop="80dp"
android:fontFamily="@font/poppins"
android:text="Dancing"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="18dp"
android:textStyle="bold" />
</RelativeLayout>
<ImageView
android:layout_width="102dp"
android:layout_height="88dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:src="@drawable/dance" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/c8"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="550dp"
android:layout_marginLeft="220dp"
app:cardCornerRadius="26dp"
app:cardElevation="10dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/gradient30"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="4dp"
android:layout_marginTop="80dp"
android:fontFamily="@font/poppins"
android:text="Swimming"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="18dp"
android:textStyle="bold" />
</RelativeLayout>
<ImageView
android:layout_width="102dp"
android:layout_height="88dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:src="@drawable/m2" />
</androidx.cardview.widget.CardView>
</RelativeLayout>
</ScrollView>
ExerciseFragment.java
public class ExerciseFragment extends Fragment implements View.OnClickListener{
View v;
CardView cv1, cv2, cv3, cv4, cv5, cv6, cv7, cv8;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_exercise, container, false);
cv1 = (CardView) v.findViewById(R.id.c1);
cv1.setOnClickListener(this);
cv2 = (CardView) v.findViewById(R.id.c2);
cv2.setOnClickListener(this);
cv3 = (CardView) v.findViewById(R.id.c3);
cv3.setOnClickListener(this);
cv4 = (CardView) v.findViewById(R.id.c4);
cv4.setOnClickListener(this);
cv5 = (CardView) v.findViewById(R.id.c5);
cv5.setOnClickListener(this);
cv6 = (CardView) v.findViewById(R.id.c6);
cv6.setOnClickListener(this);
cv7 = (CardView) v.findViewById(R.id.c7);
cv7.setOnClickListener(this);
cv8 = (CardView) v.findViewById(R.id.c8);
cv8.setOnClickListener(this);
// Inflate the layout for this fragment
return v;
}
@Override
public void onClick(View v) {
Fragment fragment1, fragment2, fragment3, fragment4, fragment5, fragment6, fragment7, fragment8;
switch (v.getId()) {
case R.id.c1:
fragment1 = new VideoFragment();
FragmentTransaction transaction1 = getParentFragmentManager().beginTransaction();
transaction1.replace(R.id.container,fragment1);
transaction1.commit();
break;
case R.id.c2:
fragment2 = new VideoFragment();
FragmentTransaction transaction2 = getParentFragmentManager().beginTransaction();
transaction2.replace(R.id.container,fragment2);
transaction2.commit();
break;
case R.id.c3:
fragment3 = new VideoFragment();
FragmentTransaction transaction3 = getParentFragmentManager().beginTransaction();
transaction3.replace(R.id.container,fragment3);
transaction3.commit();
break;
case R.id.c4:
fragment4 = new VideoFragment();
FragmentTransaction transaction4 = getParentFragmentManager().beginTransaction();
transaction4.replace(R.id.container,fragment4);
transaction4.commit();
break;
case R.id.c5:
fragment5 = new VideoFragment();
FragmentTransaction transaction5 = getParentFragmentManager().beginTransaction();
transaction5.replace(R.id.container,fragment5);
transaction5.commit();
break;
case R.id.c6:
fragment6 = new VideoFragment();
FragmentTransaction transaction6 = getParentFragmentManager().beginTransaction();
transaction6.replace(R.id.container,fragment6);
transaction6.commit();
break;
case R.id.c7:
fragment7 = new VideoFragment();
FragmentTransaction transaction7 = getParentFragmentManager().beginTransaction();
transaction7.replace(R.id.container,fragment7);
transaction7.commit();
break;
case R.id.c8:
fragment8 = new VideoFragment();
FragmentTransaction transaction8 = getParentFragmentManager().beginTransaction();
transaction8.replace(R.id.container,fragment8);
transaction8.commit();
break;
}
}
}
fragment_video.xml (fragment with videoview)
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".VideoFragment">
<VideoView
android:id="@+id/videoView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" />
</FrameLayout>
VideoFragment.java
this is what i have for now
public class VideoFragment extends Fragment {
View v;
VideoView view;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
view = (VideoView) v.findViewById(R.id.videoView1);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(view);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()+"/raw/boxing.mp4");
view.setMediaController(mediaController);
view.setVideoURI(uri);
view.requestFocus();
view.start();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_exercise, container, false);
return v;
}
}
this code has error at line:
MediaController mediaController = new MediaController(this);
please help, how do i make one fragment that will play different videos based on the clicked cardviews without having to make multiple fragments. Is it possible, if yes please help write the code for me. Thank you so much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论