Activity与Android中多个片段之间的通信

david96develop

我正在执行一个历史记录活动,该活动将带有For循环的片段实现为线性布局。

在该历史记录活动中,我有一个水平滚动条,当我单击视图时,它将返回该视图的索引。然后,我由侦听器将其发送给一个片段,但是问题是我有多个片段,并且只有一个片段正在获得响应。我希望所有片段都接听电话。我该如何解决?

这是我在历史活动中拥有的:

OnCustomEventListener mListener;
View.OnClickListener myClickListener = new OnClickListener(){
    @Override
    public void onClick(View v) {
    // here is where I get the index of the scroll bar
    indexChild = ((ViewGroup) uno.getParent()).indexOfChild(v);

    // here is where I call the method of the interface
    mListener.onEvent(indexChild);
}};

// here is where i set the fragments to the linear layout
for(i = 0; i<cant;i++){
    if(Utils.History_Thread == false) {
        final Fragment Frag = new PlaceholderFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("dispositivo", dev);
        bundle.putInt("row",i);
        Frag.setArguments(bundle);
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, Frag).commit();
        try {
                sleep(150);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    } else {
        break;
    }
}

这是片段:

public class PlaceholderFragment extends org.holoeverywhere.app.Fragment
        implements ScrollViewListener,OnCustomEventListener {
    private static LinearLayout padre;
    OnHeadlineSelectedListener mCallback;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            more.. implementations

            // here is when i implement the listener of mListener of History Activ.
            ((History)activity).mListener = this;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " implement headlistener);                      
        }
    }


    // here I override the method and set the background color of the view I want

    @Override
    public void onEvent(int index) { 
        padre.getChildAt(index).setBackgroundColor(Color.GREEN);
    }
}

仅调用了一个片段,因此仅绘制了该片段的视图。

接口

public interface OnCustomEventListener{
    public void onEvent(int index);
}

这是正在发生的情况的图片:

在此处输入图片说明

我希望所有人都被涂成绿色。

实际上,for的最后一个片段已绘制完毕,也许有帮助。

雪龙

我通常喜欢使用诸如greenrobot的EventBus之类的它确实使不同组件之间的通信非常整洁,还使处理后台/ UI线程更容易。它节省了很多重复的代码和接口,并由使用它的社区进行了测试。

例:

// sender (activity or other)
EventBus.getDefault().post(new ClickEvent(index));

// receiver (fragments, or other)
EventBus.getDefault().register(this); // in onCreate()
EventBus.getDefault().unregister(this); // in onDestroy()
public void onEvent(ClickEvent clickEvent) { ... }

包含其他信息的幻灯片:http : //www.slideshare.net/greenrobot/eventbus-for-android-15314813

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

android中片段之间的通信

来自分类Dev

片段之间的Android通信

来自分类Dev

ViewPager中片段之间的通信

来自分类Dev

Android:Activity和Fragment Activity之间的通信

来自分类Dev

Android在嵌套片段之间进行通信

来自分类Dev

导航抽屉中片段之间的通信

来自分类Dev

导航抽屉中片段之间的通信

来自分类Dev

Android服务与多个片段进行通信

来自分类Dev

如何在Android中的多个片段之间传递数据

来自分类Dev

片段之间的通信与ViewPager

来自分类Dev

片段之间进行通信

来自分类Dev

Android-片段onClick之间的多个信息

来自分类Dev

AccesibilityService与Activity之间的通信

来自分类Dev

在同一活动中的片段之间进行通信

来自分类Dev

Activity如何与Android中的ListFragment通信?

来自分类Dev

片段之间的动态通信失败

来自分类Dev

片段之间的正确通信方式

来自分类Dev

Android导航回到Activity的片段中

来自分类Dev

与FragmentTabHost中的片段通信

来自分类Dev

Android在选项卡中的多个片段之间切换

来自分类Dev

当片段中只有一个可见时,在片段之间进行通信

来自分类Dev

在Android中的片段之间传递数据

来自分类Dev

在Android片段中的不同图像之间滑动

来自分类Dev

NavigationDrawer 中的片段到片段通信

来自分类Dev

两个片段之间的通信

来自分类Dev

片段与未连接的活动之间如何通信?

来自分类Dev

嵌套片段/活动之间的通信(双向)

来自分类Dev

片段和非父活动之间的通信

来自分类Dev

Android Kotlin从Activity中获取片段中的Zxing条码结果