繁体   English   中英

通过底部导航视图通过活动将价值从一个片段传递到另一个片段

[英]Pass value from fragment to fragment through activity with bottom navigation view

我正在尝试通过MainActivity将自动更新的文本(即经度和纬度信息)从fragment_map传递到fragment_clocking,但是我在fragment_clocking中获得的值返回为null,而Logcat内没有错误。

我正在尝试使用bundle传递值,但是由于我具有底部导航视图,因此我不确定在何处以及如何声明和调用bundle。

MainActivity.java

    public class MainActivity extends AppCompatActivity {

    TextView latitude;
    TextView longitude;
    Bundle bundle = new Bundle();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        BottomNavigationView bottomNav = findViewById(R.id.nav_view);
        bottomNav.setOnNavigationItemSelectedListener(navListener);

        latitude = findViewById(R.id.id_latitude);
        longitude = findViewById(R.id.id_longitude);

        bundle.putString("latitudeInfo", String.valueOf(latitude));
        bundle.putString("longitudeInfo", String.valueOf(longitude));

        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new MapFragment()).commit();
    }

    private BottomNavigationView.OnNavigationItemSelectedListener navListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Fragment selectedFragment = null;
                switch (item.getItemId()) {
                    case R.id.navigation_map:
                        selectedFragment = new MapFragment();
                        selectedFragment.setArguments(bundle);
                        break;
                    case R.id.navigation_clocking:
                        selectedFragment = new ClockingFragment();
                        selectedFragment.setArguments(bundle);
                        break;

                }

                getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.fragment_container, selectedFragment)
                .commit();
                return true;
                }
        };
    }

MapFragment.java

    public class MapFragment extends Fragment implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener, OnMapReadyCallback {

    TextView latitude;
    TextView longitude;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_map, null);

        latitude = rootView.findViewById(R.id.id_latitude);
        longitude = rootView.findViewById(R.id.id_longitude);

        SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
                .findFragmentById(R.id.map);

        mapFragment.getMapAsync(this);

        return rootView;
    }

    @Override
    public void onLocationChanged(Location location) {

        latitude.setText("Longitude: "+ location.getLongitude());
        longitude.setText("Latitude:    "+ location.getLatitude());
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

            mGoogleApiClient.connect();
        }
    }

ClockingFragment.java

    public class ClockingFragment extends Fragment implements LocationListener {

    TextView latitude, longitude;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_clocking, null);

        latitude = rootView.findViewById(R.id.id_latitudeLocation);
        longitude = rootView.findViewById(R.id.id_longitudeLocation);

       Bundle bundle = getArguments();
       if(bundle != null){
           String latitudetest = bundle.getString("latitudeInfo");
           String longitudetest = bundle.getString("longitudeInfo");

           latitude.setText(latitudetest);
           longitude.setText(longitudetest);
       }

        return rootView;
    }

activity_main.xml中

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/nav_view"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/bottom_nav_menu" />

fragment_clocking.xml

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

        <TextView
            android:id="@+id/id_latitudeLocation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginStart="20dp"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="20dp"
            android:text="TEST PLACEHOLDER"
            android:textSize="30sp" />

        <TextView
            android:id="@+id/id_longitudeLocation"
            android:layout_below="@+id/id_latitudeLocation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="20dp"
            android:layout_marginLeft="20dp"
            android:text="TEST PLACEHOLDER"
            android:textSize="30sp"/>

fragment_map.xml

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

        <fragment
            xmlns:map="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MapsActivity" />

        <TextView
            android:id="@+id/id_latitude"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="20dp"
            android:layout_marginLeft="20dp"
            android:textSize="30sp" />

        <TextView
            android:id="@+id/id_longitude"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:layout_below="@+id/id_latitude"
            android:layout_marginStart="20dp"
            android:layout_marginLeft="20dp"
            android:textSize="30sp" />

预期结果:FragmentMap.java中的纬度和经度值通过MainActivity.java传递并显示在FragmentClocking.java中

实际结果: https : //imgur.com/PWMPiaj

一般来说,如果您有一个Fragment收集数据,并且想要将此数据传递给另一个Fragment ,则应该在Activity实现一个回调接口。 收集数据的Fragment应该使用回调接口在Activity调用该方法。 Activity应实现回调接口。 Activity的方法在调用时应将数据存储在某些成员变量中。 Activity启动另一个Fragment ,它应将先前存储的数据添加到“参数” Bundle ,然后调用setArguments()将数据传递给新的Fragment

有关实现此功能的几种可能方法,请参见在片段及其容器活动之间传递数据

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM