【问题标题】:Pass value from fragment to fragment through activity with bottom navigation view通过底部导航视图的活动将值从片段传递到片段
【发布时间】:2019-07-21 07:10:53
【问题描述】:

我正在尝试通过 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

【问题讨论】:

  • 您在创建Activity 时将纬度和经度值放在Bundle 中。我不清楚这些数据应该来自哪里。您希望从哪里获得经度和纬度?请解释

标签: java android android-fragments android-activity bottomnavigationview


【解决方案1】:

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

请参阅Passing data between a fragment and its container activity 了解几种可能的实现方式。

【讨论】:

    猜你喜欢
    • 2019-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多