【问题标题】:Fragments with Interface class in AndroidAndroid中带有接口类的片段
【发布时间】: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


【解决方案1】:

在片段活动(主类)中这样做

    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    fragment1 fragment = fragment1.newInstance(main.this);
    transaction.add(layout, fragment);
    transaction.commit();

在fragment1中首先初始化你的接口类

 private Interface_class interface

在oncreateView()之前写

  public static fragment1 newInstance(main main) {
    // TODO Auto-generated method stub
    fragment1 fragment = new fragment1();
    fragment.interface = main
    return fragment;
}

然后在fragment1上写按钮clicklistener

 button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            interface.setVideoOff();
        }
    });

最后在作为主类的片段活动中,在实现的接口中编写以下代码

@override
void setVideoOff(){
//code to stop playing mediaplayer
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多