Android 如何从一个片段中的listadapter向另一个片段发送事件?

Android 如何从一个片段中的listadapter向另一个片段发送事件?,android,android-fragments,interface,Android,Android Fragments,Interface,让我先展示一下我的应用程序的基本结构: MainActivity | | ------------- ------------ | | SearchFragment WatchListFragment

让我先展示一下我的应用程序的基本结构:

                  MainActivity                    
                     |  |
         -------------  ------------                                  
         |                         |
   SearchFragment           WatchListFragment             
         |                         |                       
SearchListViewFragment   WatchListListViewFragment
         |                         |
   SearchAdapter             WatchListAdapter          
现在我遇到了一种情况,在SearchAdapter中处理listview中单击按钮,这需要将单击的listitem的ID传递给WatchListFragment,以便我可以将该项添加到WatchListViewFragment的listview中

将内容直接从一个片段传递到另一个片段似乎是不好的做法,因此我想我应该尝试使用接口,从SearchAdapter->SearchListViewFragment->SearchFragment->MainActivity->WatchListFragment传递ID。由于不太清楚我在做什么,我最终得到了四个看起来几乎相同的界面。这对我来说似乎不太合适。所以现在我想尝试一个由所有相关类实现的接口。但这感觉也不太对


是否有一些我应该使用的标准方法?“人人共享一个界面”是个好主意吗?我试着通过阅读接口来获得一个想法,虽然我在简化的教科书示例中得到了这个想法,但这个案例完全是另一个烂摊子…

拥有一个接口才是最好的选择。在处理片段时,您可以执行以下操作,而不是在所有涉及的类中实现它并传递侦听器引用

在SearchAdapter类中创建一个公共静态接口,并让主要活动实现它。然后,在SearchListViewFragment类中,按如下方式设置回调:

mySearchAdapter.setIdListener(getActivity())

因为活动实现了接口,而且片段可以访问活动,所以这是一条完全有效的语句。但是,如果该活动没有在接口中实现,您将得到一个ClassCastException

或者,您可以使用一系列getter方法,这些方法允许您从主活动访问搜索适配器,并允许您设置回调


例如,在MainActivity类中:
getSearchFragment().getSearchListViewFragment().getSearchAdapter().setIdListener(this)

我认为在这种特定情况下最好的解决方案是中介模式。实际上,我已经在同一个应用程序的另一个部分实现了它,与最初的问题相关。上面JustSoAmazing的回答只得到了从SearchAdapter到MainActivity的“消息”,而不是一直到预期的接收者,即WatchListFragment。当然,可以在MainActivity中实现接口,使其将消息向下转发到WatchListFragment。但是,此解决方案更为通用,可以由实现AppMediatorInterface的任何类用来发送任何消息,这使得它非常灵活



首先,我们定义一个接口,任何希望订阅通知的类都必须实现该接口

AppMediatorInterface.java

public interface AppMediatorInterface {

    public void onAppMediatorNotification(Object sender, Object data);

}
public class AppMediator{
    protected static List<AppMediatorInterface> observers = new ArrayList<AppMediatorInterface>();

    public static void addObserver(AppMediatorInterface observer){
                observers.add( observer );
    }

    public static void removeObserver(Object observer){
        int pos = observers.indexOf(observer);

        if( pos>-1 )
            observers.remove(pos);
    }

    public static void notifyObservers(Object sender, Object data){
        for( int i=0; i<observers.size(); i++ ){
            observers.get(i)).onAppMediatorNotification(sender, data);
        }
    }

}
AppMediator.notifyObservers( this, "Add-button clicked" );
public class WatchListFragment extends Fragment implements AppMediatorInterface{

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);                
        AppMediator.addObserver(this);
    }


    @Override
    public void onDestroy() {       
        AppMediator.removeObserver(this);
        super.onDestroy();
    }


    /* --- AppMediatorInterface --- */
    @Override
    public void onAppMediatorNotification(Object sender, Object data) {
        // Do something...
    }
}       


其次,我们定义了实际的中介类,我选择将其设置为静态,以便在应用程序中的任何地方都可以轻松使用

AppMediator.java

public interface AppMediatorInterface {

    public void onAppMediatorNotification(Object sender, Object data);

}
public class AppMediator{
    protected static List<AppMediatorInterface> observers = new ArrayList<AppMediatorInterface>();

    public static void addObserver(AppMediatorInterface observer){
                observers.add( observer );
    }

    public static void removeObserver(Object observer){
        int pos = observers.indexOf(observer);

        if( pos>-1 )
            observers.remove(pos);
    }

    public static void notifyObservers(Object sender, Object data){
        for( int i=0; i<observers.size(); i++ ){
            observers.get(i)).onAppMediatorNotification(sender, data);
        }
    }

}
AppMediator.notifyObservers( this, "Add-button clicked" );
public class WatchListFragment extends Fragment implements AppMediatorInterface{

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);                
        AppMediator.addObserver(this);
    }


    @Override
    public void onDestroy() {       
        AppMediator.removeObserver(this);
        super.onDestroy();
    }


    /* --- AppMediatorInterface --- */
    @Override
    public void onAppMediatorNotification(Object sender, Object data) {
        // Do something...
    }
}       


收到通知:

WatchListFragment.java

public interface AppMediatorInterface {

    public void onAppMediatorNotification(Object sender, Object data);

}
public class AppMediator{
    protected static List<AppMediatorInterface> observers = new ArrayList<AppMediatorInterface>();

    public static void addObserver(AppMediatorInterface observer){
                observers.add( observer );
    }

    public static void removeObserver(Object observer){
        int pos = observers.indexOf(observer);

        if( pos>-1 )
            observers.remove(pos);
    }

    public static void notifyObservers(Object sender, Object data){
        for( int i=0; i<observers.size(); i++ ){
            observers.get(i)).onAppMediatorNotification(sender, data);
        }
    }

}
AppMediator.notifyObservers( this, "Add-button clicked" );
public class WatchListFragment extends Fragment implements AppMediatorInterface{

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);                
        AppMediator.addObserver(this);
    }


    @Override
    public void onDestroy() {       
        AppMediator.removeObserver(this);
        super.onDestroy();
    }


    /* --- AppMediatorInterface --- */
    @Override
    public void onAppMediatorNotification(Object sender, Object data) {
        // Do something...
    }
}       

我最终使用了完全不同的解决方案。起初,我打算让我的WatchListAdapter扩展ArrayAdapter,但我意识到,如果我让它们都扩展SimpleCrsorAdapter的一个常见子类,我可以重用SearchAdapter(它扩展SimpleCrsorAdapter)中的几乎所有代码。因此,在SearchAdapter中单击按钮会导致DB向我的数据库中的watchlist_表添加一个条目。WatchListAdapter订阅my DB中的自定义onChange侦听器,并在watchlist_表中执行插入/更新/删除操作时收到通知。希望能帮助到别人!感谢您分享此解决方案。这真的很有帮助@坏帐