0

我正在做一个基于信标的室内本地化应用程序。在我的应用程序中,我在ViewPager中加载了3个选项卡。第二个选项卡是一个地图片段,它应该根据信标信号显示用户所在位置的地图。更新事件中的android片段

public class Shop extends AppCompatActivity implements IndoorsLocationListener, ProximityManager.ProximityListener { 

    private static final String TAG = Shop.class.getSimpleName(); 
    private IndoorsSurfaceFragment indoorsFragment; 
    private ProximityManagerContract proximityManager; 
    private ScanContext scanContext; 
    TabPagerAdapter adapter; 
    ViewPager viewPager; 


    // TODO : Add more event types like devices updated 
    private List<EventType> eventTypes = new ArrayList<EventType>() {{ 
//  add(EventType.DEVICES_UPDATE); 
     add(EventType.DEVICE_DISCOVERED); 
    }}; 

    private IBeaconScanContext beaconScanContext = new IBeaconScanContext.Builder() 
      .setEventTypes(eventTypes) //only specified events we be called on callback 
      .setRssiCalculator(RssiCalculators.newLimitedMeanRssiCalculator(5)) 
      .build(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     proximityManager = new KontaktProximityManager(this); 

     setContentView(R.layout.activity_shop); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     setTitle("Shopping"); 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
       this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
     drawer.setDrawerListener(toggle); 
     toggle.syncState(); 


     viewPager = (ViewPager) findViewById(R.id.tab_content); 

     MapTabContentFragment mtcf = new MapTabContentFragment(); 
     adapter = new TabPagerAdapter(getSupportFragmentManager()); 

     IndoorsFactory.Builder indoorsBuilder = new IndoorsFactory.Builder(); 
     IndoorsSurfaceFactory.Builder surfaceBuilder = new IndoorsSurfaceFactory.Builder(); 
     indoorsBuilder.setContext(this); 

     // Set the API Key for Indoo.rs 
     indoorsBuilder.setApiKey(getResources().getString(R.string.indoors_api_key)); 

     // Set the building's ID 
     indoorsBuilder.setBuildingId((long)762751544); 
     // Log.d(TAG+ " Building ID: ", Long.toString(pBuildingID)); 

     // callback for indoo.rs-events 
     indoorsBuilder.setUserInteractionListener(this); 
     surfaceBuilder.setIndoorsBuilder(indoorsBuilder); 
     indoorsFragment = surfaceBuilder.build(); 

     adapter.addFragment(new ListTabContentFragment(), "LIST"); 
     adapter.addFragment(indoorsFragment, "MAP"); 
     adapter.addFragment(new ARTabContentFragment(), "AR"); 

     viewPager.setAdapter(adapter); 
    } 
} 

此代码段只是在创建活动时一次加载所有选项卡。现在我需要的是第二个片段(mtcf,“MAP”),只有在收到信标信号时才加载。

@Override 
public void onEvent(BluetoothDeviceEvent bluetoothDeviceEvent) { 
    List<? extends RemoteBluetoothDevice> deviceList = bluetoothDeviceEvent.getDeviceList(); 
    long timestamp = bluetoothDeviceEvent.getTimestamp(); 
    DeviceProfile deviceProfile = bluetoothDeviceEvent.getDeviceProfile(); 
    switch (bluetoothDeviceEvent.getEventType()) { 
     case SPACE_ENTERED: 
      Log.d(TAG, "namespace or region entered"); 
      break; 
     case DEVICE_DISCOVERED: 
      Log.d(TAG, "found new beacon"); 
      Log.d(TAG, ((Integer.toString(deviceList.size())))); 

      String nameOfBeacon = deviceList.get(0).getName(); 

      if(nameOfBeacon.equals("Entrance")) { 
       long buildingMapID = lookupBuilding(deviceList.get(0).getName()); 
       IndoorsSurfaceFragment indoorsSurfaceFragment = createIndoorFragment(buildingMapID); 

       getSupportFragmentManager().beginTransaction().replace(R.id.indoorslayout,indoorsSurfaceFragment).commit(); 
      } 
      break; 

MapFragment.xml

<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layout_gravity="fill_vertical" 
app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

<FrameLayout 
    android:id="@+id/indoorslayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"></FrameLayout> 

</android.support.v4.widget.NestedScrollView>` 

我怎样才能实现这个?我只需要在接收到蓝牙信标事件后加载地图片段。

谢谢。

回答

2

你必须通过你的TabPagerAdapter处理:

1,在TabPagerAdapter,添加PARAM,如:List<Fragment> mItems,首先你加两个片段为mItems。

覆盖TabPagerAdapter的getItem函数是这样的:

public Fragment getItem(int position){ 
    return mItems.get(position); 
} 

2,当你想仅仅通过TabPagerAdapter的addItem函数来显示MapFragment,然后mItems有三个片段。您可以手动对片段进行分类

当您使用addItem时,不要忘记拨打notifyDataSetChanged()。 希望能帮到你!

+0

一直沿着错误的方式在最后两天搜索地图页面每次。再次感谢。 – FireDrakon

+0

还有一个问题。是否有可能用旧的替代一个新的片段?我尝试过这样的事情。 mFragmentList.set(1,newFragment); 但它没有得到更新。我也调用了notifyDataSetChanged()函数。任何理由? – FireDrakon

+0

是的,当你的适配器扩展了'FragmentPagerAdapter'时''notifyDataSetChanged()'总是不起作用。你应该重写'getItemPosition',并返回'POSITION_NONE'。你可以参考:[this](http://stackoverflow.com/questions/10849552/update-viewpager-dynamically) – yanzi1225627

1

您需要在“OnEvent”中调用notifyDataSetChanged,并在Viewpager中实现 getItemPosition(Object map)。

这将更新你invode notifyDatasetChanged Update Fragment from ViewPager

+0

它的工作原理!谢谢。 :) – FireDrakon

+0

将问题标记为已回答。 –

+0

你好,我已经选择了上面的最佳答案,因为他在你面前回答了同样的问题。感谢您的帮助顺便说一句。 :) – FireDrakon