java—与现有内容片段化的活动

gc0ot86w  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(209)

我写了一个简单的应用程序有两个活动,第一个是我的 MainActivity 第二个是我的 MapsActivity . 代码在我的电脑里运行得很好 MainActivity ,当我按下一个按钮,我会被重定向到我的 MapsActivity 固定的 LatLng 还有我的实际位置。
现在我得出的结论是我想暗示 TabLayout (共3个选项卡)。第一个选项卡应该包含我的 MainActivity 所有的按钮和代码都在我的 MainActivity 我要把它转移到第一个碎片上。这不起作用,因为我用了很多 findViewById 碎片不喜欢这样。 MainActivity :

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button button;
private Button btnred;
private Button btnaura;
private double v1;
private double v12;
private double vend;
private double v1end;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    toolbar.setLogo(R.drawable.fvroth);
    button =  findViewById(R.id.btnroth);
    btnred= findViewById(R.id.btnRednitz);
    btnaura = findViewById(R.id.btnaurach);
    btnaura.setOnClickListener(this);
    button.setOnClickListener(this);
    btnred.setOnClickListener(this);
    ViewPager2 viewPager2 = findViewById(R.id.viewPager);
    viewPager2.setAdapter(new weiherPageAdapter(this));

    TabLayout tabLayout =findViewById(R.id.tabLayout);
    TabLayoutMediator tabLayoutMediator= new TabLayoutMediator(
            tabLayout, viewPager2, new TabLayoutMediator.TabConfigurationStrategy() {
        @Override
        public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
            switch (position){
                case 0: {
                    tab.setText("Fliesgewässer");
                    tab.setIcon(R.drawable.ic_baseline_check_24);
                    break;
                }
                case 1: {
                    tab.setText("Weiher");
                    tab.setIcon(R.drawable.ic_baseline_account_circle_24);
                    break;
                }
                case 2: {
                    tab.setText("Schon-maß und Zeiten");
                    tab.setIcon(R.drawable.ic_baseline_announcement_24);
                    break;
                }

            }
        }
    }
    );
    tabLayoutMediator.attach();

}
public void openMapsActivity(){
    Intent intent=new Intent(this,MapsActivity.class);
    LatLng position= new LatLng(v1,v12);
    intent.putExtra("Pos",position);
    LatLng positionende = new LatLng(vend, v1end);
    intent.putExtra("PosEnde",positionende);
    startActivity(intent);

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.btnroth:
             v1= 49.21344;
            v12= 11.171002;
            vend= 49.211946;
            v1end= 11.158726;
            openMapsActivity();
            break;
        case R.id.btnRednitz:
            v1=  49.250451;
            v12= 11.084395;
            vend= 49.269385;
            v1end= 11.076057;
            openMapsActivity();
            break;
        case R.id.btnaurach:
            v1= 49.244672;
            v12= 11.043886;
            vend=49.254027;
            v1end= 11.080881;
            openMapsActivity();
            break;
                            }
}
``` `MapActivity` ```
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
    Location currentLocation;
    FusedLocationProviderClient fusedLocationProviderClient;
    private static final int REQUEST_CODE = 101;

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
        fetchLastLocation();
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.

        // Construct a GeoDataClient

    }

    private void fetchLastLocation() {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
           ActivityCompat.requestPermissions(this,new String[]
                   {Manifest.permission.ACCESS_FINE_LOCATION},REQUEST_CODE);
            return;
        }
        Task<Location> task = fusedLocationProviderClient.getLastLocation();
        task.addOnSuccessListener(new OnSuccessListener<Location>() {
            @Override
            public void onSuccess(Location location) {
                if(location != null){
                    currentLocation = location;
                    Toast.makeText(getApplicationContext(),currentLocation.getLatitude()+""+currentLocation.getLongitude(),Toast.LENGTH_SHORT).show();
                    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                            .findFragmentById(R.id.map);
                    mapFragment.getMapAsync(MapsActivity.this);
                }
            }
        });
    }

    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera

        LatLng position = getIntent().getExtras().getParcelable("Pos");
        LatLng positionende= getIntent().getExtras().getParcelable("PosEnde");
        LatLng aktpos = new LatLng(currentLocation.getLatitude(),currentLocation.getLongitude());
        mMap.addMarker(new MarkerOptions().position(position).title("Anfang"));
        mMap.addMarker(new MarkerOptions().position(positionende).title("Ende"));
        mMap.addMarker(new MarkerOptions().position(aktpos).title("ich").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(position,15),1000,null);
    }

}

为了更好地理解这里的应用程序图片。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题