- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经使用 View 寻呼机使用 FragmentStatePagerAdapter 加载 fragment 。当我第一次来时它会工作,但如果我从寻呼机适配器重定向到其他 fragment 并返回它会显示空白屏幕。
**fragment_community.xml**
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="@+id/includeTop"
layout="@layout/layout_header"
android:layout_width="match_parent"
android:layout_height="50dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/includeTop"
android:orientation="vertical"
>
<android.support.design.widget.TabLayout
android:id="@+id/tabAddFriend"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_alignParentTop="true"
android:background="@drawable/tab_friend_selector"
android:fadeScrollbars="false"
app:tabGravity="fill"
app:tabIndicatorHeight="4dp"
app:tabMaxWidth="0dp"
app:tabMode="fixed"
app:tabSelectedTextColor="@color/colorDarkGray"
app:tabTextColor="@color/colorGray">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/pagerCommunity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/tabAddFriend"/>
</LinearLayout>
</LinearLayout>
**Community Fragment :**
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.****.app.R;
import com.****.app.adapter.CommunityPagerAdapter;
import com.****.app.customcontrol.CustomTextViewMedium;
import com.****.app.customcontrol.CustomTextViewRegular;
import com.****.app.utils.General;
import com.****.app.utils.ManageFragment;
public class CommunityFragment extends Fragment{
public static final int FOLLOWERS = 0;
public static final int FOLLOWING = 1;
public static final int GROUPS = 2;
public static String LOGTAG = CommunityFragment.class.getSimpleName ();
// define activity layouts
LinearLayout linearBack, linearAction;
ImageView imgBack, imgAction;
CustomTextViewRegular txtTitle;
FragmentActivity activity;
String[] tabList;
//CustomViewPager pagerCommunity;
ViewPager pagerCommunity;
TabLayout tabAddFriend;
FragmentManager manager;
CommunityPagerAdapter communityPagerAdapter;
String operation="";
public CommunityFragment (){
}
@Override
public void onCreate (Bundle savedInstanceState){
super.onCreate (savedInstanceState);
activity = this.getActivity ();
tabList = activity.getResources ().getStringArray (R.array.communityArray);
//Bundle bundle=activity.getIntent ().getExtr
if(getArguments ()!=null){
operation= getArguments ().getString (General.REQUEST_TYPE);
}
}
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate (R.layout.fragment_community, container, false);
linearAction = (LinearLayout) view.findViewById (R.id.linearAction);
linearAction.setVisibility (View.VISIBLE);
linearBack = (LinearLayout) view.findViewById (R.id.linearBack);
imgBack = (ImageView) view.findViewById (R.id.imgBack);
imgAction = (ImageView) view.findViewById (R.id.imgAction);
imgAction.setImageResource (R.drawable.ic_block);
txtTitle = (CustomTextViewRegular) view.findViewById (R.id.txtTitle);
txtTitle.setText (getString (R.string.community));
tabAddFriend = (TabLayout) view.findViewById (R.id.tabAddFriend);
pagerCommunity = (ViewPager) view.findViewById (R.id.pagerCommunity);
linearBack.setOnClickListener (new View.OnClickListener (){
@Override
public void onClick (View v){
ManageFragment.back (activity);
}
});
imgBack.setOnClickListener (new View.OnClickListener (){
@Override
public void onClick (View v){
ManageFragment.back (activity);
}
});
pagerCommunity.addOnPageChangeListener (new ViewPager.OnPageChangeListener (){
@Override
public void onPageScrolled (int position, float positionOffset, int positionOffsetPixels){
}
@Override
public void onPageSelected (int position){
if (position == FOLLOWERS){
imgAction.setImageResource (R.drawable.ic_block);
} else if (position == FOLLOWING){
imgAction.setImageResource (R.drawable.ic_plus);
} else if (position == GROUPS){
imgAction.setImageResource (R.drawable.ic_plus);
}
updateCustomTabTextView (position);
}
@Override
public void onPageScrollStateChanged (int state){
}
});
linearAction.setOnClickListener (new View.OnClickListener (){
@Override
public void onClick (View v){
int position = pagerCommunity.getCurrentItem ();
if (position == FOLLOWERS){
imgAction.setImageResource (R.drawable.ic_block);
ManageFragment.replace (activity, R.id.content_frame, new BlockedUsersFragment (), BlockedUsersFragment.LOGTAG, BlockedUsersFragment.LOGTAG);
} else if (position == FOLLOWING){
imgAction.setImageResource (R.drawable.ic_plus);
ManageFragment.replace (activity, R.id.content_frame, new FollowingAddFragment (), FollowingAddFragment.LOGTAG, FollowingAddFragment.LOGTAG);
} else if (position == GROUPS){
imgAction.setImageResource (R.drawable.ic_plus);
ManageFragment.replace (activity, R.id.content_frame, new NewGroupFragment (), NewGroupFragment.LOGTAG, NewGroupFragment.LOGTAG);
}
}
});
setTabLayout ();
return view;
}
private void setTabLayout (){
manager = activity.getSupportFragmentManager ();
communityPagerAdapter = new CommunityPagerAdapter (activity, manager);
pagerCommunity.setAdapter (communityPagerAdapter);
tabAddFriend.setupWithViewPager (pagerCommunity);
setCustomTabTextView ();
if(!operation.equalsIgnoreCase ("")){
int type=Integer.parseInt (operation);
if(type==FOLLOWERS){
imgAction.setImageResource (R.drawable.ic_block);
}else if(type==FOLLOWING){
imgAction.setImageResource (R.drawable.ic_plus);
}
pagerCommunity.setCurrentItem (type);
}
}
public void updateCustomTabTextView (int position){
for (int i = 0; i < tabAddFriend.getTabCount (); i++){
View view = tabAddFriend.getTabAt (i).getCustomView ();
CustomTextViewMedium textTabTitle = (CustomTextViewMedium) view.findViewById (R.id.textTabTitle);
if (i == position){
textTabTitle.setTextColor (ContextCompat.getColor (activity, R.color.colorDarkGray));
} else{
textTabTitle.setTextColor (ContextCompat.getColor (activity, R.color.colorGray));
}
}
}
private void setCustomTabTextView (){
for (int i = 0; i < tabAddFriend.getTabCount (); i++){
TabLayout.Tab tab = tabAddFriend.getTabAt (i);
View view = LayoutInflater.from (activity).inflate (R.layout.layout_add_friend_tab, null);
CustomTextViewMedium textTabTitle = (CustomTextViewMedium) view.findViewById (R.id.textTabTitle);
textTabTitle.setText (tabList[i]);
textTabTitle.setTextSize (12);
if (pagerCommunity.getCurrentItem () == i){
textTabTitle.setTextColor (ContextCompat.getColor (activity, R.color.colorDarkGray));
} else{
textTabTitle.setTextColor (ContextCompat.getColor (activity, R.color.colorGray));
}
tab.setCustomView (view);
}
}
}
**CommunityPagerAdapter.java**
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import com.****.app.R;
import com.****.app.fragment.ActivityFragment;
import com.****.app.fragment.FollowingFragment;
import com.****.app.fragment.GroupFragment;
public class CommunityPagerAdapter extends FragmentStatePagerAdapter{
Activity activity;
public CommunityPagerAdapter (Activity activity, FragmentManager fm){
super (fm);
this.activity = activity;
}
@Override
public Fragment getItem (int position){
switch (position){
case 0:
return new ActivityFragment ();
case 1:
return new FollowingFragment ();
case 2:
return new GroupFragment ();
}
return null;
}
@Override
public int getCount (){
return 3;
}
@Override
public CharSequence getPageTitle (int position){
String title = " ";
switch (position){
case 0:
title = activity.getResources ().getString (R.string.followers);
break;
case 1:
title = activity.getResources ().getString (R.string.following);
break;
case 2:
title = activity.getResources ().getString (R.string.groups);
break;
}
return title;
}
}
最佳答案
否决了从另一篇文章提供的解决方案?谢谢!
好吧,我再试一次来描述发生了什么以及为什么Fragment
没有重新附加:
恢复的Fragment
(从返回 a.k.a. 弹出 back-stack)完全重新创建了 View
-层次结构但不是View
-弹出状态的层次结构。解决方法是确保已恢复 Fragment
s' View
重新附加,其中之一是编写 FragmentStatePagerAdapter
的另一个实现。是否使用了原始代码并进行了以下更改:
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
// If we already have this item instantiated, there is nothing
// to do. This can happen when we are restoring the entire pager
// from its saved state, where the fragment manager has already
// taken care of restoring the fragments we previously had instantiated.
if (mFragments.size() > position) {
Fragment f = mFragments.get(position);
if (f != null) {
return f;
}
}
...
}
稍微修改为:
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
// If we already have this item instantiated, there is nothing
// to do. This can happen when we are restoring the entire pager
// from its saved state, where the fragment manager has already
// taken care of restoring the fragments we previously had instantiated.
if (mFragments.size() > position) {
Fragment f = mFragments.get(position);
if (f != null) {
if (mCurTransaction == null) {
mCurTransaction = mFragmentManager.beginTransaction();
}
mCurTransaction.detach(f);
mCurTransaction.attach(f);
return f;
}
}
...
}
请查看我最近发布的一个非常相似的场景以及我解决它的方法。
关于android - 当我们回来时,在 View 寻呼机 fragment 状态寻呼机适配器中出现白屏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42483105/
我有一个像这样的 fragment 栈 F1 -> F2 -> F3 -> F4 -> F5 现在我需要删除 F2、F3、F4 fragment 。 我需要如果我从 F5 fragment 按下后退按
我需要帮助来理解以下场景的工作原理以及如何实现结果。 我有一个名为 ShoppingCart 的类。它有一个名为 addItemsToShoppingCartFromPreviousOrder 的方法
我有一个包含 ViewPager 的 fragment 。这个 ViewPager 显然是由其他 Fragments 填充的。 我的问题是,ViewPager 中的 Fragment 是否有任何方法(
所以我正在实现一个 FragmentActivity 并尝试添加一个 fragment ,但是我遇到了多个问题。我以前做过这个,实际上我使用的代码与上一个项目(它工作的地方)相同,但由于某种原因它在这
Closed. This question needs details or clarity。它当前不接受答案。
我想将 Android X fragment (androidx.fragment.app.Fragment) 转换为 Android native fragment (android.app.Fra
假设我有一个包含 10 列文本类型 (20) 的 SQLite 表。 ListFragment 将从数据库中提取 4 列并使用 SimpleCursorAdapter 显示在列表中。 选择后,List
我有一个对话框 fragment ,我为延迟初始化创建了一个类。当我显示对话框时,它显示正常。但是,当我关闭对话框时,它崩溃的原因是: fragment 与 fragment 管理器无关。 我也尝试过
我想在 View 分页器更改为 fragment 时刷新该 fragment 。 package com.mcivisoft.rcbeam; import android.os.Bundle; imp
我有一个名为的 Activity MainActivity 我在容器 R.id.mainContainer 中添加了 fragment “BenefitFragment”。 在 BenefitFrag
所以我有这个 ClientListView,效果很好,可以显示客户,我可以单击一个客户并在右侧获取他们的详细信息(在我的第二个 fragment 中)。由此处的布局定义: 这很好用,但后来我
我试过这段代码,但点击第一个 fragment 中的按钮并没有改变第二个 fragment 中的字符串值。 这是第一个 fragment 的 kotlin 文件。它有两个按钮,点击它们可以更改字符串值
我有以下情况:我打开 fragment A 和目标,通过按钮的单击事件转到 fragment B。当我在 fragment B 中并点击后退按钮(为了返回 fragment A) 我想将一些参数传递给
我制作了一个 NavigationDrawer fragment ,其中包含 Home、Settings、Feedback 等项目。 home 项在点击 home 时应该打开,home 是在应用程序启
我正在一个接一个地替换 2 个 fragment ,两个 fragment 都有不同的选项菜单。当我替换第二个 fragment 时,它也显示第一个 fragment 的菜单。 setHasOptio
我有问题: android.app.Fragment$InstantiationException: Unable to instantiate fragment ${packageName}.${a
我正在研究 fragment 转换。当我用第二个 fragment 替换第一个 fragment 时,它出现在第一个 fragment 的下方。我希望它移动到第一个 fragment 之上。我该怎么做
我在抽屉导航里总共有 12 个 fragment 。每个 fragment 都有 volley 方法。每个 fragment 都显示自己的 Volley 响应,除了 position = 1 和 po
我在我的 Activity 中使用了两个 fragment 。最初我将向 Activity 添加一个 fragment 。然后在第一个 fragment 中使用监听器我想用第二个 fragment 替
我正在实现一个“fragments-101”程序,当相应的按钮被点击时我会替换一个 fragment 。然而,下面的事情发生了: 为什么会这样?为什么初始 fragment 没有被完全替换?MainA
我是一名优秀的程序员,十分优秀!