我有一个带有三个标签的TabLayout,每个标签都包含一个ListFragment。这些由SectionsPagerAdapter处理。当旋转屏幕时,似乎他们已经丢失了ListAdapter,因为它们正在加载但没有任何反应: example image
实际上我有很多关于碎片的生命周期的东西,但是我无法为我的问题实现它。
当旋转屏幕时,活动被破坏并再次调用onCreate()。所以我把以下内容放在那里:
创建ListFragment的新实例
创建CursorAdapter的新实例
那么我的谬误在哪里?
编辑:
我现在解决了问题,覆盖了onSaveInstanceState
方法。但到底是什么导致了所描述的问题呢?
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
FragmentManager mFragmentManager;
private ProgressDialog pDialog;
// Fragments for the three Tabs
public Contact fragment_all;
public Contact fragment_official;
public Contact fragment_alumni;
// Handler for SQLite DB Access
public DBHandler dbhandler;
private MemberListAdapter ca_all;
private MemberListAdapter ca_alu;
private MemberListAdapter ca_off;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_members);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mFragmentManager = getSupportFragmentManager();
mSectionsPagerAdapter = new SectionsPagerAdapter(mFragmentManager);
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
fragment_all = new Contact();
fragment_alumni = new Contact();
fragment_official = new Contact();
dbhandler = new DBHandler(this);
ca_all = new MemberListAdapter(this);
ca_alu = new MemberListAdapter(this);
ca_off = new MemberListAdapter(this);
ca_all.ID = 1;
ca_alu.ID = 3;
ca_off.ID = 2;
fragment_all.setListAdapter(ca_all);
fragment_alumni.setListAdapter(ca_alu);
fragment_official.setListAdapter(ca_off);
updateAdapters();
}
@Override
protected void onDestroy() {
super.onDestroy();
dbhandler.close();
}
@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_activity_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// ...unimportant code
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
if (position == 0){
return fragment_all;
}else if (position == 1){
return fragment_official;
}else{
return fragment_alumni;
}
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getResources().getString(R.string.title_section1);
case 1:
return getResources().getString(R.string.title_section2);
case 2:
return getResources().getString(R.string.title_section3);
}
return null;
}
}
private void updateAdapters() {
ca_all.changeCursor(dbhandler.query());
ca_alu.changeCursor(dbhandler.query());
ca_off.changeCursor(dbhandler.query());
}
}
layout_main:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="@color/colorSecond">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorContrast"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
app:tabMaxWidth="0dp"
app:tabGravity="fill"
app:tabMode="fixed"
android:background="@color/colorContrast"
android:fillViewport="false"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/updatedb" />
</android.support.design.widget.CoordinatorLayout>
ListFragment Contact.java:
public class Contact extends ListFragment {
private CursorAdapter ca;
private MemberListAdapter mla;
@Override
public void onListItemClick(ListView l, View v, int position, long id){
mla = (MemberListAdapter) getListAdapter();
String myid = Integer.toString(mla.ID);
Log.i("Position: ", Integer.toString(position));
Log.i("ID: ", myid);
}
public Contact(){
super();
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
CursorAdapter MemberListAdapter.java:
public class MemberListAdapter extends CursorAdapter {
private LayoutInflater inflator;
public int ID;
public MemberListAdapter(Context context) {
super(context, null, 0);
inflator = LayoutInflater.from(context);
}
@Override
public int getViewTypeCount() {
return 3;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
int ciName = cursor.getColumnIndex(DBHandler.USERS_NAME);
int ciSurname = cursor.getColumnIndex(DBHandler.USERS_SURNAME);
int ciMail = cursor.getColumnIndex(DBHandler.USERS_MAIL);
String mail = cursor.getString(ciMail);
String name = cursor.getString(ciName);
String surname = cursor.getString(ciSurname);
ImageView image = (ImageView) view.findViewById(R.id.icon);
TextView textview1 = (TextView) view.findViewById(R.id.text1);
TextView textview2 = (TextView) view.findViewById(R.id.text2);
image.setImageResource(R.drawable.ksatlogo);
textview1.setText(name + ", " + surname);
textview2.setText(mail);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return inflator.inflate(R.layout.icon_text_text, null);
}
}
答案 0 :(得分:0)
尝试添加android:configChanges =&#34; orientation&#34;在你的清单活动中。