在事件中更新android片段

时间:2016-05-24 06:03:51

标签: android android-fragments bluetooth ibeacon ibeacon-android

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

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,&#34; MAP&#34;)仅在收到信标信号时加载。

@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 个答案:

答案 0 :(得分:2)

您必须通过TabPagerAdapter

处理

1,在TabPagerAdapter中,添加一个参数,例如:List<Fragment> mItems,首先将两个片段添加到mItems中。

覆盖TabPagerAdapter getItem函数,如下所示:

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

2,当你想通过TabPagerAdapter addItem函数显示MapFragment时,mItems有三个片段。您可以手动对片段进行排序。

使用addItem时,请勿忘记致电notifyDataSetChanged()。 希望能帮到你!

答案 1 :(得分:1)

您需要在&#34; OnEvent&#34;内部调用notifyDataSetChanged。并实施 Viewpager中的getItemPosition(Object map)。

这将在您每次调用notifyDatasetChanged时更新地图页面 Update Fragment from ViewPager