使用片段定向,不保存文本

时间:2017-07-18 10:04:41

标签: android android-recyclerview fragment orientation

当我更改手机的方向时,文本内容不会保存,默认文本会放在textview中,在我的活动自助餐清单中我的活动我有这个

android:configChanges="keyboardHidden|orientation"

咖啡馆

public class Cafeteria extends BaseActivity {

private SectionsPagerAdapter mSectionsPagerAdapter;


private ViewPager mViewPager;

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

    /*Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);*/

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());


    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.menu_cafeteria, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}


public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position){
            case 0:
                Oferta oferta = new Oferta();
                return oferta;
            case 1:
                MenuCafe men = new MenuCafe();
                return men;
            case 2:
                Carta carta = new Carta();
                return carta;
            default:
                return null;
        }
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return getString(R.string.ofertas);
            case 1:
                return getString(R.string.menu);
            case 2:
                return getString(R.string.carta);
        }
        return null;
    }
}

MenuCafe

public class MenuCafe extends Fragment{
    private RecyclerView recyclerView;
    private LinearLayoutManager linearLayoutManager;
    private AdaptadorMenu adapter;
    private List<Menu> menu;




    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_menu, container, false);
        recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view_menu);
        menu  = new ArrayList<>();
        load_data_from_server(0);
        linearLayoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(linearLayoutManager);
        adapter = new AdaptadorMenu(getContext(),menu);
        recyclerView.setAdapter(adapter);
        return rootView;
    }
}

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

  

尝试在Android清单文件中提供此功能

 android:screenOrientation="portrait"

答案 1 :(得分:0)

  • 您必须保存文本内容,因为在旋转屏幕时,您的活动将重置Android的生命周期,与首次打开相同。
  • 了解如何在此处保存状态:Losing data when rotate screen