从父活动

时间:2016-12-26 11:41:53

标签: android android-fragments android-viewpager

ViewPager 中的4个片段有一个来自父活动的公共按钮,可以打开一个对话框,并刷新这4个片段中的 ListViews 。我尝试使用静态适配器但是没有用。现在我正在尝试使用广播,但我仍然不会从外部广播接收器看到我的适配器。请帮忙。

3 个答案:

答案 0 :(得分:2)

我有同样的要求我做了以下: - 1.使用FragmentStatePagerAdapter扩展pagerAdapter 2.在pagerAdapter类

中实现以下内容
  Carbon::setLocale('es');
$archive_current_year = Articles::whereBetween('created_at', [
    Carbon::now()->startOfYear(),
    Carbon::now()->endOfYear(),
  ])->get()->groupBy(function($item) {
    return $item->created_at->format('F');
  });
  1. 然后当你刷新只是打电话 -
  2.   

    notifyDataSetChanged();

    适配器上的

    您可以从viewpager获取适配器,如下所示:

      

    viewPager.getAdapter()notifyDataSetChanged();

    这里viewpager是我的viewpager的引用。

答案 1 :(得分:0)

使用单独的键(1,2,3,4)将您的4个列表添加到地图中。

if (viewPager.getCurrentItem() == 0){
// show list items with key 1
}else if(viewPager.getCurrentItem() == 1){
//show list items with key 2
}else if(....)

现在放一个监听器并按下按钮调用它。

答案 2 :(得分:0)

像往常一样,我已经尝试了几天做某事,当我在这里发布时,我发现了一件我做错的事:D

那么,我该怎么做:

在每个子片段中,我创建了一个广播接收器

    receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context contextBroadcast, Intent intent) {
            createListView(listView, context);
        }
    };

    IntentFilter filter = new IntentFilter();
    filter.addAction("com.cukrzyca.DISMISS");
    context.registerReceiver(receiver, filter);

事实证明你必须准备好IntentFilter,这就是我做错了。方法createListView只刷新listView,对话框在父活动中解除我只发送广播:

public void broadcastDismiss() {
    Intent intent = new Intent();
    intent.setAction("com.cukrzyca.DISMISS");
    getContext().sendBroadcast(intent);
}
相关问题