更新事件中的 android 个片段
Updating android fragment inside an event
我正在做一个基于信标的室内定位应用程序。在我的应用程序中,我在 ViewPager 中加载了 3 个选项卡。第二个选项卡是地图片段,应根据信标信号显示用户所在位置的地图。
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);
}
}
此代码段仅在创建 activity 时立即加载所有选项卡。现在我需要的是第二个片段(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>`
我该如何实现??我只需要在收到蓝牙信标事件后才加载地图片段。
谢谢。
您必须通过您的 TabPagerAdapter
:
处理
1、在TabPagerAdapter中,添加一个param,如:List<Fragment> mItems
,首先将两个片段添加到mItems中。
像这样覆盖 TabPagerAdapter
getItem 函数:
public Fragment getItem(int position){
return mItems.get(position);
}
2、当要显示MapFragment时,只需通过TabPagerAdapter
addItem函数,mItems就有3个fragments。您可以手动对片段进行排序。
当你使用addItem
时,不要忘记调用notifyDataSetChanged()
。
希望能帮到你!
需要在"OnEvent"中调用notifyDataSetChanged,并实现
Viewpager 中的 getItemPosition(Object map)。
这将在您每次调用 notifyDatasetChanged 时更新地图页面
Update Fragment from ViewPager
我正在做一个基于信标的室内定位应用程序。在我的应用程序中,我在 ViewPager 中加载了 3 个选项卡。第二个选项卡是地图片段,应根据信标信号显示用户所在位置的地图。
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);
}
}
此代码段仅在创建 activity 时立即加载所有选项卡。现在我需要的是第二个片段(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>`
我该如何实现??我只需要在收到蓝牙信标事件后才加载地图片段。
谢谢。
您必须通过您的 TabPagerAdapter
:
1、在TabPagerAdapter中,添加一个param,如:List<Fragment> mItems
,首先将两个片段添加到mItems中。
像这样覆盖 TabPagerAdapter
getItem 函数:
public Fragment getItem(int position){
return mItems.get(position);
}
2、当要显示MapFragment时,只需通过TabPagerAdapter
addItem函数,mItems就有3个fragments。您可以手动对片段进行排序。
当你使用addItem
时,不要忘记调用notifyDataSetChanged()
。
希望能帮到你!
需要在"OnEvent"中调用notifyDataSetChanged,并实现 Viewpager 中的 getItemPosition(Object map)。
这将在您每次调用 notifyDatasetChanged 时更新地图页面 Update Fragment from ViewPager