【发布时间】:2013-11-18 12:04:24
【问题描述】:
我有问题,如何在片段类中使用接口类。 在使用片段实现接口时它不能正常工作。
我有 1 个片段活动和 2 个片段。取片段1,片段2。
默认情况下,片段 1(视频查看其正在播放的视频)在页面片段活动的一半的框架布局中膨胀。 通过点击 Activity 中的按钮,fragment 2 会膨胀到页面的下半部分。
单击 fragment 2 中的按钮时,fragment 1 内容需要更改,因此我使用界面来停止 fragment 1 中的视频,但它不起作用。如果我在片段活动中实现接口它的工作,但在片段类中它不起作用..帮助我做到这一点..
片段活动
public class main extends FragmentActivity implements Interface_Class{
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.activixml);
replaceFragment(new fragment1,R.id.fram1);
replaceFragment(new fragment2,R.id.fram2);
}
}
public void replaceFragment(Fragment fragment,int layout) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(layout, fragment);
transaction.commit();
}
@Override
void setVideoOff(){
system.out.println("inside interface");
}
接口类
interface Interface_Class {
void setVideoOff()
}
片段 1
public class feagment1 extends Fragment implements OnCompletionListener,Interface_Class{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout, container, false);
videoView = (VideoView) view.findViewById(R.id.videoView);
Uri str=Uri.parse(path);
// Video path
videoView.setVideoURI(str);
videoView.requestFocus();
videoView.start();
return view;
}
}
@Override
void setVideoOff(){
system.out.println("inside interface");
videoView.stopPlayback();
}
片段 2
public class fragment1 extends Fragment{
Interface_Class interface_Class
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.lay1, container, false);
interface_Class.setVideoOff();
return view;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
interface_Class = (Interface_Class) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString());
}
}
}
【问题讨论】:
-
@Poovizhirajan.N developer.android.com/training/basics/fragments/…。该示例在文档中
-
你制作片段 1 的实例以将其添加到片段活动中??
-
@AvinashKumarPankaj 我无法理解你问的问题
-
@Poovizhirajan.N 将来自 Fragment2 的布尔值传达给使用按钮单击界面的活动,然后将相同的布尔值传达给 Fragment1。现在根据 fragment1 中的布尔值进行操作。从文档中引用所有片段到片段的通信都是通过关联的 Activity 完成的。两个 Fragment 永远不应该直接通信。
-
@Raghunandan 谢谢我试着告诉你
标签: android interface android-fragments media-player android-videoview