![](/img/trans.png)
[英]Passing ArrayList from Fragment to Activity and Using BottomNavigationView
[英]Passing value from Adapter to a BottomNavigationView Fragment in Navigation components
该应用程序在MainActivity
上有一个BottomNavigationView
,用于处理 Fragment 之间的导航。 其中一个片段中有一个RecyclerView
, RecyclerView
的项目有一个按钮。
我试图让RecyclerView
Items 的按钮导航到另一个 Fragment 并传递一个值,但我一直收到 null object 引用并传递数据。
MainActivity
的点击,并创建了一个 Bundle 将接收到的值传达给所需的 Fragment// The interface
public interface OnItemClickListener {
void onItemClick();
适配器 class
//Define interface
OnItemClickListener listener;
//Create constructor for interface
public LocationsAdapter(Activity activity) {
listener = (MainActivity)activity;
...
@Override
public void onBindViewHolder(@NonNull LocationsViewHolder holder, int position) {
//A click listener for the button
holder.showMarkerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Create instance of MapFragment to pass data as Bundle
MapFragment mapFragment = new MapFragment();
LatLng latLng = new LatLng(markerObject.getLatitude(),markerObject.getLongitude());
//Create Bundle and add data
Bundle bundle = new Bundle();
bundle.putString("latitude",markerObject.getLatitude().toString());
bundle.putString("longitude",markerObject.getLongitude().toString());
listener.onItemClick(bundle);
}
});
}
public class MainActivity extends AppCompatActivity implements OnItemClickListener {
BottomNavigationView navView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
navView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_home,R.id.navigation_map, R.id.navigation_locations, R.id.navigation_profile)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);
}
@Override
public void onItemClick() {
navView.setSelectedItemId(R.id.navigation_map);
}
}
//MapFragment
private void setCam(){
Bundle bundle = getArguments();
if (getArguments() != null ){
String SLatitude = bundle.getString("latitude");
String SLongitude = bundle.getString("longitude");
Double latitude = Double.parseDouble(SLatitude);
Double longitude = Double.parseDouble(SLongitude);
LatLng latLng = new LatLng(latitude,longitude);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 20));
Toast.makeText(getActivity(), latLng.toString(), Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getActivity(), "error present", Toast.LENGTH_SHORT).show();
}
}
提前致谢。
使用导航 controller 时,您通过参数传入数据。 Go 到您的导航组件 XML 文件并进行以下更改;
1.在导航动作中,您需要添加一个参数。
<action
android:id="@+id/action_id"
app:destination="@id/destinationFragment">
<argument
android:name="paramterName"
app:argType="string" />
</action>
2.将相同的参数添加到目标片段
<fragment
android:id="@+id/coursesNavFragment"
android:name="com.example.example.DestinationFragment"
android:label="destination_fragment"
tools:layout="@layout/destination_fragment" >
<argument
android:name="parameterName"
app:argType="string" />
</fragment>
您也可以使用设计视图轻松完成此操作。
现在,您调用以导航到目标片段的 function 期望您向其传递参数。
navigationController.navigate(actionSourceFragmentToDestinationFragment("text123"))
然后您可以在目标片段中提取捆绑包。
val parameter = DestinationFragmentArgs.fromBundle(requireArguments()).parameterName
// parameter has the value of "text123"
由于导航组件使用安全参数,因此您可以传递任何类型的继承自Parcellable的参数。
MapFragment mapFragment = new MapFragment();
LatLng latLng = new LatLng(markerObject.getLatitude(),markerObject.getLongitude());
//Create Bundle and add data
Bundle bundle = new Bundle();
bundle.putString("position",latLng.toString());
mapFragment.setArguments(bundle);
这里的mapFragment
只是一个创建的 object,没有在导航图中使用。 即它是独立于导航的独立object,因此它不参与导航,因为它与navGraph 中的当前mapFragment
片段不同。
解决方案:
为了解决这个问题,您需要在BottomNavigationView的片段之间切换时传递参数,并且注册OnNavigationItemSelectedListener
是一个好地方:
首先允许适配器通过侦听器回调将 arguments 发送回活动
因此,修改侦听器回调以接受参数
interface OnItemClickListener listener {
void onItemClick(Bundle args);
}
将其应用于适配器
@Override
public void onBindViewHolder(@NonNull LocationsViewHolder holder, int position) {
//A click listener for the button
holder.showMarkerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LatLng latLng = new LatLng(markerObject.getLatitude(),markerObject.getLongitude());
//Create Bundle and add data
Bundle bundle = new Bundle();
bundle.putString("position",latLng.toString());
//Interface to transport the click event to the MainActivity to switch Fragment in the BottomNavigationView
listener.onItemClick(bundle);
}
});
}
最后,将OnNavigationItemSelectedListener
注册到navView
并修改回调以接受活动中的 Bundle:
public class MainActivity extends AppCompatActivity implements OnItemClickListener {
BottomNavigationView navView;
private Bundle mapArgs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
navView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_home,R.id.navigation_map, R.id.navigation_locations, R.id.navigation_profile)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);
navView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
navController.navigate(item.getItemId(), mapArgs);
return true;
}
});
}
@Override
public void onItemClick(Bundle args) {
navView.setSelectedItemId(R.id.navigation_map);
mapArgs = args;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.