我有一个在片段之间滑动的ViewPager。我使用FragmentStatePagerAdapter将片段馈送到ViewPager。如果用户以正常速度向左滑动,然后非常快地向右滑动,他们可以使ViewPager进入一个奇怪的状态,它显示多个片段。
例如,如果用户在片段A上,然后以正常速度向左滑动到片段B,然后快速向右滑动以返回片段A,则屏幕上显示片段A和B。
有人知道为什么会发生这种情况,或者有什么好的方法来防止它吗?
它看起来像这样:
下面是我的XML格式的ViewPager定义:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.company.views.CustomActionBar
android:id="@+id/customActionBar"
android:layout_width="match_parent"
android:layout_height="@dimen/height_actionbar"
android:layout_alignParentTop="true"/>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/customActionBar"/>
此外,我还记录了onPageChangeListener()的输出,并注意到当ViewPager在视图之间卡住时,它报告的positionOffset为0。下面是ViewPager从STATE_DRAGGING转换到STATE_SETTLING再到STATE_IDLE时的值,此时它处于这种奇怪的状态:
状态= 0先前状态:2.职位:1个位置偏移:0.0
状态= 1先前状态:0位置:1个位置偏移:0.0
状态= 2前一状态:1个位置:1个位置偏移:0.4069444
状态= 0先前状态:2.职位:2个位置偏移:0.0
因此,看起来好像ViewPager向我报告了错误的positionOffset。
完整示例代码活动和适配器:
public class ActivityBagelProfileViewer extends CustomAbstractFragmentActivity
implements CustomActionBarContract, ListenerProgress, ListenerSync
{
public static final String EXTRA_BAGEL_INDEX = "BAGEL";
public static final int REQUEST_CODE_BAGEL_PROFILE_VIEWER = 4000;
public static final int RESULT_GO_TO_PASS_FLOW = 12;
public static final int RESULT_GO_TO_LIKE_FLOW = 14;
public static final int RESULT_GO_TO_SEE_MORE_BAGELS = 16;
private ViewPager mProfilesViewPager;
private CustomActionBar mCustomActionBar;
private int mViewPagerPosition;
private DialogProgress mDialogProgress;
private BagelViewPagerAdapter mAdapterBagelViewPager;
private List<Bagel> mListBagels;
@Override
protected void onCreate(Bundle savedInstanceState)
{
Logger.d("ENTER");
super.onCreate(savedInstanceState);
if (ManagerGive.IS_BRANCH_SESSION_OPEN == false)
{
ManagerGive.initializeBranchMetricsSession();
}
setContentView(R.layout.activity_with_viewpager);
mCustomActionBar = (CustomActionBar) findViewById(R.id.customActionBar);
mCustomActionBar.setMenu(this);
mProfilesViewPager = (ViewPager) findViewById(R.id.viewPager);
if (getIntent().getExtras() != null)
{
mViewPagerPosition = getIntent().getExtras().getInt(EXTRA_BAGEL_INDEX, 0);
}
}
@Override
protected void onStop()
{
super.onStop();
ManagerGive.closeBranchMetricsSession();
}
public void onIconClick(View view)
{
Logger.d("ENTER");
finishWithAnimation();
}
private void finishWithAnimation()
{
setResult(RESULT_OK);
finish();
overridePendingTransition(R.anim.slide_in_from_left, R.anim.slide_out_to_right);
}
@Override
public void onBackPressed()
{
if (!super.handleBackPressedEvent())
{
finishWithAnimation();
}
}
private void setupNewAdapter()
{
mListBagels = Bakery.getInstance().getManagerBagel().getCopyOfBagelsWithoutCurrent();
mAdapterBagelViewPager = new BagelViewPagerAdapter(getSupportFragmentManager(), mListBagels, this);
mProfilesViewPager.setAdapter(mAdapterBagelViewPager);
mProfilesViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener()
{
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
{
}
@Override
public void onPageSelected(int position)
{
setActionBar(position);
mViewPagerPosition = position;
}
@Override
public void onPageScrollStateChanged(int state)
{
}
});
mProfilesViewPager.setCurrentItem(mViewPagerPosition, false);
}
@Override
protected void onResume()
{
Logger.d("ENTER");
super.onResume();
Bakery.getInstance().getManagerSyncData().addListener(this);
if (mProfilesViewPager.getAdapter() == null)
{
Logger.d("Adapter null. Setting new adapter");
setupNewAdapter();
}
else
{
if (mProfilesViewPager.getAdapter().getCount() !=
Bakery.getInstance().getManagerBagel().getCopyOfBagelsWithoutCurrent().size())
{
Logger.d("Bagel list in Bakery changed size. Setting new adapter");
setupNewAdapter();
}
}
if (mListBagels.size() > 0)
{
setActionBar(mViewPagerPosition);
mDialogProgress = new DialogProgress(this);
}
else
{
//kv Something has gone terribly wrong if we don't have any Bagels, just finish
finish();
}
}
private void setActionBar(int bagelIndex)
{
Logger.d("bagelIndex=" + bagelIndex);
Bagel bagel = mListBagels.get(bagelIndex);
//kv If this is our current bagel and we haven't taken action yet, then show timer
if (Bakery.getInstance().getManagerBagel().getCurrentBagel() == bagel
&& bagel.getAction() != Bagel.ACTION_LIKED && bagel.getAction() != Bagel.ACTION_PASSED)
{
Logger.d("Setting up #timer in action bar");
mCustomActionBar.startTimeLeftTimer(DateUtils.getMillisFromUtc(bagel.getEndDate()),
this, new ListenerTimer()
{
@Override
public void onTimerExpired()
{
Logger.d("ENTER");
Bakery.getInstance().getManagerSyncData().performSync(null, false);
}
}, mCustomActionBar.getTextViewTimeLeft(), R.string.timer_blank);
mCustomActionBar.setLabel(R.string.time_left);
mCustomActionBar.hideTitle();
}
//kv Otherwise show date
else
{
mCustomActionBar.setTitle(DateUtils.getLocalizedDateFromStringDate(bagel.getStartDate(), DateUtils.DATE_WITH_TIME_PATTERN));
mCustomActionBar.stopTimeLeftTimer();
mCustomActionBar.hideTimeLeft();
}
}
@Override
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
outState.putInt(EXTRA_BAGEL_INDEX, mViewPagerPosition);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
Logger.d("ENTER");
super.onRestoreInstanceState(savedInstanceState);
if (savedInstanceState.containsKey(EXTRA_BAGEL_INDEX))
{
mViewPagerPosition = savedInstanceState.getInt(EXTRA_BAGEL_INDEX);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
Logger.d("requestCode=" + requestCode + ", resultCode=" + resultCode + ", data=" + data);
switch (requestCode)
{
case ActivityBeanShop.REQUEST_CODE:
if (resultCode == Activity.RESULT_OK && data != null)
{
//fp user purchased sufficient beans to resume their transaction
PurchaseType interruptedPurchaseType = (PurchaseType) data.getSerializableExtra(ActivityBeanShop.EXTRA_PURCHASE_TYPE);
switch (interruptedPurchaseType)
{
case BONUS_BAGEL:
case OPEN_SESAME:
case REMATCH:
Bundle bundle = new Bundle();
bundle.putSerializable(ManagerPurchase.EXTRA_PURCHASE_TYPE, interruptedPurchaseType);
ManagerEvents.notifyListeners(EventType.BEAN_TRANSACTION_FOR_FEATURE_UNLOCK_COMPLETE, bundle);
Logger.d("Notified listeners about #purchase bean transaction, can now resume feature #purchase");
break;
default:
Logger.w("Unrecognized purchase type: " + interruptedPurchaseType.getItemName());
}
}
break;
default:
Logger.w("Could not recognize code: " + requestCode);
}
}
@Override
public int getTitleId()
{
return R.string.bagel_action_checked;
}
@Override
public int getIconId()
{
return R.drawable.selector_icon_up;
}
@Override
public void showProgress(int stringId)
{
mDialogProgress.setText(stringId);
mDialogProgress.show();
}
@Override
public void dismissProgress()
{
ViewUtils.safelyDismissDialog(mDialogProgress);
}
public void setActionBar()
{
setActionBar(mViewPagerPosition);
}
@Override
public void onSyncComplete()
{
Logger.d("ENTER");
mListBagels = Bakery.getInstance().getManagerBagel().getCopyOfBagelsWithoutCurrent();
mAdapterBagelViewPager.setBagels(mListBagels);
}
public boolean isShowingThisBagel(Bagel bagel)
{
Bagel currentlyShownBagel = mListBagels.get(mViewPagerPosition);
return bagel == currentlyShownBagel;
}
private static class BagelViewPagerAdapter extends FragmentStatePagerAdapter
{
private List<Bagel> mBagels;
private ListenerProgress mListenerProgress;
public BagelViewPagerAdapter(FragmentManager fragmentManager, List<Bagel> bagels,
ListenerProgress listenerProgress)
{
super(fragmentManager);
Logger.d("bagels=" + bagels);
this.mBagels = bagels;
mListenerProgress = listenerProgress;
}
@Override
public Fragment getItem(int i)
{
Logger.d("i=" + i);
UserProfile myProfile = Bakery.getInstance().getManagerUserProfile().getMyOwnProfile();
FragmentProfile fragment = FragmentProfile.newInstance(mBagels.get(i), false, myProfile);
fragment.setListenerProgress(mListenerProgress);
return fragment;
}
@Override
public int getCount()
{
return mBagels.size();
}
public void setBagels(List<Bagel> bagels)
{
mBagels = bagels;
notifyDataSetChanged();
}
}
}
下面是每个Fragment布局的XML布局代码(不得不去掉一些SO字符限制的B/c):
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scrollView">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="-0.5dp"
android:orientation="vertical"
android:animateLayoutChanges="true"
android:id="@+id/profile_top_container">
<!-- Photos section with pager/carousel -->
<FrameLayout
android:id="@+id/photoViewpagerContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.coffeemeetsbagel.views.CustomAsShitViewPager
android:id="@+id/pager_profile_images"
xmlns:android="http://schemas.android.com/apk/res/android"
app:aspectRatio="@integer/photo_ratio_height_over_width"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:id="@+id/linearLayout_bulletsAndFriendsContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="bottom">
<com.coffeemeetsbagel.views.CustomTextView
android:id="@+id/textView_stamp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
app:customFont="Raleway-Bold.ttf"
android:layout_gravity="end"
android:textSize="@dimen/text_stamp"
android:paddingTop="@dimen/margin_large"
android:layout_marginEnd="@dimen/margin_xxxxxsmall"
android:layout_marginRight="@dimen/profile_margin_smaller"/>
<!-- photo circle indicators -->
<com.viewpagerindicator.CirclePageIndicator
android:id="@+id/bullet_indicators"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/circle_indicator_margin_bottom"
android:clickable="false"
app:fillColor="@color/blue_cmb"
app:pageColor="@color/gray_background"
app:radius="@dimen/circle_indicator_radius"
app:strokeWidth="0dp"/>
<!-- container for mutual friends strip -->
<RelativeLayout
android:id="@+id/relativeLayout_mutual_friends_container"
android:layout_width="match_parent"
android:layout_height="@dimen/baseline_grid_component_touchable"
android:background="@color/white_transparent"
android:visibility="gone">
<com.coffeemeetsbagel.views.CustomTextView
android:id="@+id/textView_mutual_friends_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
style="@style/profile_mutual_friends_text"/>
<LinearLayout
android:id="@+id/linearLayout_mutual_friends_icons"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="@dimen/baseline_grid_small"
android:layout_marginRight="@dimen/baseline_grid_small"
android:layout_centerVertical="true">
<ImageView
android:id="@+id/imageView_icon0"
android:layout_width="@dimen/baseline_grid_component_touchable"
android:layout_height="@dimen/baseline_grid_component_touchable"
android:padding="@dimen/typography_smallest"
android:background="@color/transparent"
android:visibility="gone"/>
<ImageView
android:id="@+id/imageView_icon1"
android:layout_width="@dimen/baseline_grid_component_touchable"
android:layout_height="@dimen/baseline_grid_component_touchable"
android:background="@color/transparent"
android:padding="@dimen/typography_smallest"
android:visibility="gone"/>
<ImageView
android:id="@+id/imageView_icon2"
android:layout_width="@dimen/baseline_grid_component_touchable"
android:layout_height="@dimen/baseline_grid_component_touchable"
android:background="@color/transparent"
android:padding="@dimen/typography_smallest"
android:visibility="gone"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</FrameLayout>
<!-- Buttons section with User Actions for pass / like-->
<LinearLayout
android:id="@+id/linearLayout_buttons_pass_like"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/baseline_grid_smaller"
android:layout_marginLeft="@dimen/baseline_grid_small"
android:layout_marginRight="@dimen/baseline_grid_small"
android:layout_marginTop="@dimen/baseline_grid_medium"
android:orientation="horizontal"
android:visibility="gone">
<ImageView
android:id="@+id/button_pass"
android:layout_width="0dp"
android:layout_height="@dimen/profile_action_button_height"
android:layout_weight="1"
android:background="@drawable/ripple_button_pass"
android:clickable="true"
android:src="@drawable/icon_pass_pressed"
android:scaleType="center"
android:layout_marginRight="@dimen/margin_small"/>
<ImageView
android:id="@+id/button_like"
android:layout_width="0dp"
android:layout_height="@dimen/profile_action_button_height"
android:layout_weight="1"
android:background="@drawable/ripple_button_like"
android:clickable="true"
android:src="@drawable/icon_like_pressed"
android:scaleType="center"
android:layout_marginLeft="@dimen/margin_small"/>
</LinearLayout>
<!-- Buttons section with User Actions for rematch / give-->
<LinearLayout
android:id="@+id/linearLayout_buttons_rematch_give"
android:layout_width="match_parent"
android:layout_height="@dimen/give_ten_button_height"
android:layout_marginBottom="@dimen/baseline_grid_smaller"
android:layout_marginLeft="@dimen/baseline_grid_small"
android:layout_marginRight="@dimen/baseline_grid_small"
android:layout_marginTop="@dimen/baseline_grid_medium"
android:orientation="horizontal"
android:gravity="center"
android:visibility="gone">
<com.coffeemeetsbagel.views.CustomTextView
android:id="@+id/textView_rematch"
android:layout_width="@dimen/zero_dip"
android:layout_height="match_parent"
android:layout_marginRight="@dimen/give_take_button_margin_side"
android:layout_weight="1"
style="@style/button_give_take_rematch"
android:text="@string/rematch"/>
<com.coffeemeetsbagel.views.CustomTextView
android:id="@+id/text_view_give_with_rematch"
android:layout_width="@dimen/zero_dip"
android:layout_weight="1"
android:layout_height="match_parent"
style="@style/button_give_take_rematch"
android:text="@string/give"/>
</LinearLayout>
<com.coffeemeetsbagel.views.CustomTextView
android:id="@+id/textView_they_like_you"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/icon_like_alert"
android:drawablePadding="@dimen/margin_xxsmall"
style="@style/profile_info_item_value"
android:layout_marginLeft="@dimen/margin_med"
android:paddingTop="@dimen/baseline_grid_smaller"/>
<ViewStub
android:id="@+id/viewStub_profile_feedback"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout="@layout/profile_feedback"/>
<!-- Profile information table -->
<!-- Name -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/profile_info_item_layout_margins"
android:paddingTop="@dimen/baseline_grid_smaller"
android:orientation="horizontal">
<com.coffeemeetsbagel.views.CustomTextView
android:text="@string/profile_info_label_name"
style="@style/profile_info_item_label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<com.coffeemeetsbagel.views.CustomTextView
android:id="@+id/profile_info_value_name"
style="@style/profile_info_item_value"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"/>
</LinearLayout>
<!-- Age -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/profile_info_item_layout_margins"
android:orientation="horizontal">
<com.coffeemeetsbagel.views.CustomTextView
android:text="@string/profile_info_label_age"
style="@style/profile_info_item_label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<com.coffeemeetsbagel.views.CustomTextView
android:id="@+id/profile_info_value_age"
style="@style/profile_info_item_value"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"/>
</LinearLayout>
<!-- Location -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/profile_info_item_layout_margins"
android:orientation="horizontal">
<com.coffeemeetsbagel.views.CustomTextView
android:text="@string/location"
style="@style/profile_info_item_label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<com.coffeemeetsbagel.views.CustomTextView
android:id="@+id/profile_info_value_location"
style="@style/profile_info_item_value"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"/>
</LinearLayout>
<!-- Ethnicity -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/profile_info_item_layout_margins"
android:orientation="horizontal">
<com.coffeemeetsbagel.views.CustomTextView
android:text="@string/profile_info_label_ethnicity"
style="@style/profile_info_item_label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<com.coffeemeetsbagel.views.CustomTextView
android:id="@+id/profile_info_value_ethnicity"
style="@style/profile_info_item_value"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"/>
</LinearLayout>
<!-- Height -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/profile_info_item_layout_margins"
android:orientation="horizontal">
<com.coffeemeetsbagel.views.CustomTextView
android:text="@string/profile_info_label_height"
style="@style/profile_info_item_label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<com.coffeemeetsbagel.views.CustomTextView
android:id="@+id/profile_info_value_height"
style="@style/profile_info_item_value"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"/>
</LinearLayout>
<!-- Religion -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/profile_info_item_layout_margins"
android:orientation="horizontal">
<com.coffeemeetsbagel.views.CustomTextView
android:text="@string/profile_info_label_religion"
style="@style/profile_info_item_label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<com.coffeemeetsbagel.views.CustomTextView
android:id="@+id/profile_info_value_religion"
style="@style/profile_info_item_value"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"/>
</LinearLayout>
<!-- Occupation -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/profile_info_item_layout_margins"
android:orientation="horizontal">
<com.coffeemeetsbagel.views.CustomTextView
android:text="@string/profile_info_label_occupation"
style="@style/profile_info_item_label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<com.coffeemeetsbagel.views.CustomTextView
android:id="@+id/profile_info_value_occupation"
style="@style/profile_info_item_value"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"/>
</LinearLayout>
<!-- Employer -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/profile_info_item_layout_margins"
android:orientation="horizontal">
...
9条答案
按热度按时间h9vpoimq1#
我注意到,如果我有一些由animateLayoutChanges提供的动画,我会看到这个问题。只要在xml文件中停用它,就可以防止页面卡在中间。
ny6fqffe2#
尝试下面的示例代码并根据您的要求修改它(我猜您是在主UI线程上加载图像而不是缓存它,这只是一个猜测)。在此代码中,我从互联网下载并缓存图像:创建一个名为SomeFragTest的活动类
之后,为它创建名为activity_layout的xml
现在,我们已经创建了要在ViewPager中膨胀的Fragment类:创建一个名为ChildFrag的类,如下所示
现在我们已经为片段创建了xml作为fragtest:
在AndroidManifest.xml中添加以下权限
az31mfrm3#
在我的例子中,问题是一个空的片段。在创建了一个带有布局的片段后,它就开始按预期工作了。
基本上,我使用了一个空片段来测试视图:
当我使用具有布局的最后一个片段时,行为是正确的:
我知道这个回答并不能回答当前的问题,但是一些有类似问题的人来到了这里。所以,我希望它能有所帮助。
e1xvtsh34#
目前我认为布局的宽度有问题。到目前为止我只看到一个嫌疑人,有一个未知的属性
在UI元素
<com.coffeemeetsbagel.views.CustomAsShitViewPager
中...显然,在库/代码CustomAsShitViewPager中有一个自定义属性,至少可能会发布与aspectRatio相关的代码。
yh2wf1be5#
我刚刚意识到您在
onCreate
中做了大量的UI工作()。在onCreateView
中执行此工作更合适()。我相信Android框架在onCreate
中还没有完成UI工作(),因此您会看到不完整的UI渲染。我知道Android文档中没有明确说明这一点。如果您查看其他SO帖子或示例项目,其他开发人员在onCreate
()中很少做UI工作。至少,布局比您的简单。下面是我的建议。使用post上列出的ID,在方法
onCreateView
()中扩大Activity或Fragment中的fragtest
布局。请注意,override方法只会扩大。示例代码:在Fragment上,使用post上列出的ID开始访问UI元素和ViewPager。示例代码:
p5fdfcr16#
非常晚的回复,但如果有人有麻烦,这是什么为我工作。我 Package 的ViewPager内部视图与自定义样式如下:
我只是从文件中删除了
<View>
,ViewPager自行修复了。所以我要说的是,试着删除一些其他的父布局标记,也许是它们导致了这个问题。
附言:这在react-native中有效,但我希望它在其他领域也有帮助。
dhxwm5r47#
我也遇到过这个问题,我的解决办法如下:
sulc1iza8#
禁用动画和手动设置ViewPager的位置对我来说不起作用。在我的情况下,当导航到另一个屏幕并返回时,有时会发生bug。我最终做的是通过OnPageChangeListener保存水平偏移,如果它不为零,则在Activity的
onResume()
中将其“重置”为零。9wbgstp79#
目前,我怀疑有两种方法。
1)在片段代码中:
注意:
getCount()
应该返回片段的数目而不是列表的大小。我不能告诉你有多少片段。也许你必须在适配器中保持跟踪。2)另外,我怀疑
getItem()
方法和使用newInstance
().相关的具体代码:备注:
newInstance
应该创建一个新的片段,这可能是因为 *FragmentStatePagerAdapter在内存中没有该片段 *,或者从内存中释放了该片段。FragmentProfile.newInstance
相关的代码,特别是如果你不同意我上面的声明。