ViewPager 片段偶尔不会膨胀/加载



我搜索了很多地方,尝试了很多解决方案来解决以下问题,希望你能有所帮助!

我制作了一个包含 6 个片段的选项卡式活动,以及一个空白活动的日志记录屏幕。选项卡式活动是主要活动,它有一个 ViewPager 容器来显示实际选项卡,但日志记录活动是第一个要加载和显示的活动。在这个过程中的某个地方(实际上是非常深入的(,我注意到容器内的碎片偶尔不会显示。当我旋转屏幕时,或者当我使用菜单中的注销功能然后重新登录时,它们有时会重新出现(有时需要多次尝试,直到显示备份(。 我已经通读了,但无法弄清楚。 我试过更改

(getChildFragmentManager((, getResources((((;

但我认为我没有把它放在正确的位置。

我的主要活动

public class MainActivity extends AppCompatActivity {
private boolean verified;
public static String token;
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;

/**
* The {@link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Get the Intent that started this activity and extract the string
Bundle extras = getIntent().getExtras();
if (extras != null) {
verified = extras.getBoolean("verify");
token = extras.getString("token");
Toast.makeText(getBaseContext(), "Login Successful", 
Toast.LENGTH_SHORT).show();
}
if(verified != true) {
logoutSuccessful();
}

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the 
three
// primary sections of the activity.
mSectionsPagerAdapter = new 
SectionsPagerAdapter(getSupportFragmentManager());
//mSectionsPagerAdapter.saveState();
// 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);
//getSupportActionBar().setIcon(R.drawable.logo_small);
//set icons for tabs
for (int i = 0; i < tabLayout.getTabCount(); i++) {
if (i == 0) {
tabLayout.getTabAt(i).setIcon(R.drawable.icon_scan);
} else {
if (i == 1) {
tabLayout.getTabAt(i).setIcon(R.drawable.icon_pricecheck);
} else {
tabLayout.getTabAt(i).setIcon(R.drawable.logo_small);
}
}
}

}
@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;
}
if (id == R.id.action_logout) {
logoutSuccessful();
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
//DELETED PLACEHOLDER CLASS
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class 
below).
//  return PlaceholderFragment.newInstance(position + 1);
switch (position){
case 0: Tab1 tab1 = new Tab1();
return tab1;
case 1: Tab2 tab2 = new Tab2();
return tab2;
case 2: Tab3 tab3 = new Tab3();
return tab3;
case 3: Tab4 tab4 = new Tab4();
return tab4;
case 4: Tab5 tab5 = new Tab5();
return tab5;
case 5: Tab6 tab6 = new Tab6();
return tab6;
}
return null;
}
@Override
public int getCount() {
// Show total pages.
return 6;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "SCAN";
case 1:
return "PRICE CHECK";
case 2:
return "SECTION 3";
case 3:
return "SECTION 4";
case 4:
return "SECTION 5";
case 5:
return "SECTION 6";
}
return null;
}
}
private void logoutSuccessful(){
Intent myIntent = new Intent(MainActivity.this, Login.class);
MainActivity.this.startActivity(myIntent);
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
finish();
}
}

选项卡 1:

public class Tab1 extends android.support.v4.app.Fragment {
Button btnUpc;
EditText txtUPC;
Vector<UPCProductObject> v = new Vector<>();
Editable value;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup 
container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.tab1, container,false);
lblUpcResult = (TextView) v.findViewById(R.id.lblUPCResult);
txtUPC = (EditText) v.findViewById(R.id.txtUPC);
btnUpc = (Button) v.findViewById(R.id.btnUPC);
btnUpc.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
//hide keyboard
InputMethodManager mgr = (InputMethodManager) 
getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(txtUPC.getWindowToken(), 0);
btnUpc.setEnabled(false);
value = txtUPC.getText();
new searchUPC().execute();
}});
return v;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
getChildFragmentManager().beginTransaction()
.add(R.id.container, new Tab1(), "Scan")
.commit();
}
}

activity_main xml

<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"
tools:context="com.dabush.shen.mcrpos.MainActivity">
<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="?attr/colorPrimary"
app:layout_scrollFlags="enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="90dp"
android:layout_height="50dp"
android:src="@drawable/logo_small"
android:layout_gravity="left"
/>
</FrameLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="75dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:minHeight="80dp">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"
app:tabSelectedTextColor="@color/textColorPrimary"
app:tabTextColor="#FFD5D2D2" />
</android.support.design.widget.AppBarLayout>
<!--FLOATING EDITABLE BUTTON
<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"
app:srcCompat="@android:drawable/ic_dialog_email" />
-->

Tab1 XML

<TextView
android:id="@+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="13dp"
android:layout_toLeftOf="@+id/tblSearch"
android:layout_toStartOf="@+id/tblSearch" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tab1_title"
android:textSize="24sp"
android:layout_alignTop="@+id/section_label"
android:layout_centerHorizontal="true" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="@string/scan_desc"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true" />
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:id="@+id/tblSearch"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/txtUPC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
android:textAlignment="center" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp">
<Button
android:id="@+id/btnUPC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SEARCH" />
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" />
</TableLayout>
<TextView
android:id="@+id/lblUPCResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tblSearch"
android:layout_centerHorizontal="true"
android:layout_marginTop="91dp"
android:textAlignment="center"
android:textSize="12sp" />
<ListView
android:id="@+id/listUpc"
android:layout_width="match_parent"
android:layout_height="375dp"
android:layout_below="@+id/tblSearch"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:textAlignment="center" />

你不需要在你的第一个片段中获取 ChildFragmentManager((.beginTransaction((。在活动生命周期中,onRotate,活动被销毁并重新创建,您需要将其保存在保存实例上,并在恢复实例上重新加载UI

最新更新