Android监听器在Activity中正常工作但在Fragment中不起作用。

huangapple 未分类评论52阅读模式
英文:

Android Listener working in activity but not in fragment

问题

我有一个活动和两个片段。我正试图从一个ArrayList中获取一个被点击的项目。在我的FragmentA中,我有一个接口:

public interface GroupListener {
    public String onGroupSelected(String groupName);
}

以及

@Override
public void onAttach(@NonNull Context context) {
    super.onAttach(context);

    try {
        groupListener = (GroupListener) context;
    } catch (ClassCastException e) { 
        throw new ClassCastException(context.toString() + " 必须实现接口 " +
                "GroupListener!");
    }
}

这从列表中的一个被点击的项目获取位置:

@Override
public void onListItemClick(@NonNull ListView l, @NonNull View v, int position, long id) {
    groupListener.onGroupSelected((String) getListAdapter().getItem(position));
}

然后,我在我的活动和FragmentB中都有监听器,它们看起来是相同的:

@Override
public String onGroupSelected(String groupName) {
    System.out.println("在活动中的 onGroup 方法内");
    return groupName;
} 
@Override
public String onGroupSelected(String groupName) {
    System.out.println("在FragmentB中的 onGroup 方法内");
    return groupName;
} 

但是,当我点击一个项目时,只有我的活动中的监听器被触发,而我的片段中的监听器没有被触发。我漏掉了什么?

英文:

I have an activity and two fragments. Im trying to get a clicked item from an arraylist. In my fragmentA I have a interface:

public interface GroupListener {
        public String onGroupSelected(String groupName);
}

and

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);

        try {
            groupListener = (GroupListener) context;
        } catch (ClassCastException e)
        { 
            throw new ClassCastException(context.toString() + " must implement the interface" +
                    "called GroupListener!");
        }
    }

This gets the postion from an item clicked in a list :

    public void onListItemClick(@NonNull ListView l, @NonNull View v, int position, long id) {
        groupListener.onGroupSelected((String) getListAdapter().getItem(position));
    }

I then have the listner in my activity and in fragmentB which looks the same:

   @Override
    public String onGroupSelected(String groupName) {
        System.out.println("in onGroup  in Activity");
        return groupName;
    } 
   @Override
    public String onGroupSelected(String groupName) {
        System.out.println("in onGroup  in fragmentB");
        return groupName;
    } 

But when i click an item only the listener in my activity is activated, not the one in my fragment. What is it that im missing?

答案1

得分: 0

FragmentB 中的这部分不会触发,因为你只是从 Fragment A 与活动进行通信。

在你的活动中,可以像这样做:

@Override
public String onGroupSelected(String groupName) {
    System.out.println("在活动中的 onGroupSelected 方法内");
    FragmentB frag = //使用片段管理器找到你的片段
    frag.onGroupSelected(groupName);
    return groupName;
}

这就是如何从活动中调用片段内的方法。

英文:

The one in FragmentB will not fire, since you are only communicating with the activity from fragment A.

In your activity do something like this:

Override
    public String onGroupSelected(String groupName) {
        System.out.println("in onGroup  in Activity");
        FragmentB frag = //find your fragment using fragment manager
        frag.onGroupSelected(groupName);
        return groupName;
    } 

This is how you call methods inside fragment from an activity

答案2

得分: 0

这个问题有点晚回答,但我找到了适合我的方法。
由于侦听器将首先通过活动获取,您可以在主活动中定义目标片段,然后调用目标片段中的方法并执行您想要的操作:
在主活动的侦听器中:

DestinationFragment fragment = (DestinationFragment) getSupportFragmentManager().findFragmentById(R.id.destination_fragment);
fragment.sampleListener(bundle);

在目标片段中定义sampleListener并编写您的代码。

英文:

It is a little late to answer but I found a way that works for me.
As listener will first get by activity, you can define destination fragment in the Main activity and then call a method in the destination fragment and do what you want:
In the main Activity listener

DestinationFragment fragment = (DestinationFragment) getSupportFragmentManager().findFragmentById(R.id.destination_fragment);
fragment.sampleListener(bundle);

In the destination fragment define sampleListener and write your code.

huangapple
  • 本文由 发表于 2020年5月30日 06:05:08
  • 转载请务必保留本文链接:https://java.coder-hub.com/62095266.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定