查看删除的寻呼机片段

时间:2019-06-19 09:32:32

标签: android android-viewpager

我的活动应该以不同的方式处理纵向和横向布局:如果布局是纵向的,则应该使用ViewPager和两个片段。如果布局是横向的,则应将屏幕分为两个部分,并在其中放置两个片段。问题是,当我从纵向切换到横向时,不会删除viewpager的片段,而是调用onCreateView()

调用FragmentTransaction删除片段无济于事。

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewModel = ViewModelProviders.of(this).get(MainViewModel.class);
        int orientation = getResources().getConfiguration().orientation;
        if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.weather_layout, new CurrentWeatherFragment(), "current");
            fragmentTransaction.replace(R.id.forecast_layout, new ForecastFragment(), "forecast");
            fragmentTransaction.commit();
        }
        else {
            ViewPager viewPager = findViewById(R.id.pager);
            WeatherPageAdapter weatherPageAdapter = new WeatherPageAdapter(getSupportFragmentManager());
            viewPager.setAdapter(weatherPageAdapter);
            viewPager.addOnPageChangeListener(new WeatherPageChangedListener());
            viewPager.setCurrentItem(viewModel.getViewPagerPosition(), false);
        }
    }

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import com.example.android.myapplication.ui.fragments.CurrentWeatherFragment;
import com.example.android.myapplication.ui.fragments.ForecastFragment;

public class WeatherPageAdapter extends FragmentPagerAdapter {
    private static final int pageNumber = 2;

    public WeatherPageAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
    }

    @Override
    public int getCount() {
        return pageNumber;
    }

    @Override
    public Fragment getItem(int i) {
        if (i == 0) {
            return new CurrentWeatherFragment();
        }
        else {
            return new ForecastFragment();
        }
    }
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:rowCount="7"
    android:columnCount="2"
    tools:context=".ui.activities.MainActivity">

    <androidx.viewpager.widget.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:rowCount="7"
    android:columnCount="2"
    tools:context=".ui.activities.MainActivity"
    android:orientation="horizontal"
    android:baselineAligned="false">

    <FrameLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:id="@+id/weather_layout">
    </FrameLayout>

    <FrameLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:id="@+id/forecast_layout">
    </FrameLayout>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

在您之前的代码中,进行以下修改:

private ViewPager viewPager;
private WeatherPageAdapter weatherPageAdapter;


protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewModel = ViewModelProviders.of(this).get(MainViewModel.class);
        changeViewAsPerOrientation(getResources().getConfiguration().orientation);
    }

private void changeViewAsPerOrientation(Configuration newConfig) {
      for (Fragment fragment : getSupportFragmentManager().getFragments()) {
            getSupportFragmentManager().beginTransaction().remove(fragment).commit();
        }
      if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.weather_layout, new CurrentWeatherFragment(), "current");
        fragmentTransaction.replace(R.id.forecast_layout, new ForecastFragment(), "forecast");
        fragmentTransaction.commit();
      }
      else {
         if(viewPager == null || weatherPageAdapter == null) {
          viewPager = findViewById(R.id.pager);
          weatherPageAdapter = new  WeatherPageAdapter(getSupportFragmentManager());
          viewPager.addOnPageChangeListener(new WeatherPageChangedListener());
        } else {
          adapter.notifyDataSetChanged();
        }

        viewPager.setCurrentItem(viewModel.getViewPagerPosition(), false);
      }
    }

@Override
  public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    changeViewAsPerOrientation(newConfig);
  }