【发布时间】:2017-07-24 11:14:28
【问题描述】:
假设我有一个带有 ListView 或带有图像的 GridView 的片段。当我使用编程方式或后退按钮进行转换时,我看到这些列表视图/网格视图数据在动画之间被视觉清除。像 textviews 这样的其他控件没有这个问题。这种行为会造成不良的视觉效果。
以下是我用于片段转换的代码。
有没有办法减少这种丑陋的视觉影响。
String ttag = fragment
.getClass().toString();
Fragment tempF = fragmentManager.findFragmentByTag(ttag);
if (tempF != null && tempF.isVisible())
return;
Bundle args = new Bundle();
args.putParcelable(Utils.FRAGMENT_INPUT_KEY, fragment.getInput());
fragment.setArguments(args);
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.setAllowOptimization(true);
fragmentTransaction.setCustomAnimations(R.anim.fragment_enter,
R.anim.fragment_exit, R.anim.fragment_leftenter,
R.anim.fragment_leftexit);
fragmentTransaction.replace(R.id.fragment, fragment, ttag);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
以下片段具有顶级视图分页器,它使用带有图像的网格视图加载片段。清除子级片段上的网格视图时会出现视觉问题。顶级片段没有这样的问题。
使用 viewpager 的顶级 Fragment 生命周期方法。
ViewPager viewPager;
@Override
public void onActivityCreated(Bundle bundle) {
super.onActivityCreated(bundle);
activity = getActivity();
resource = activity.getResources();
view = getView();
viewPager = (ViewPager) view.findViewById(R.id.viewpager);
// some other setup
}
@Override
public void onStop() {
super.onStop();
}
@Override
public void onDetach() {
super.onDetach();
try {
Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
childFragmentManager.setAccessible(true);
childFragmentManager.set(this, null);
} catch (Exception e) {
}
try {
if(viewPager != null) {
viewPager.setAdapter(null);
viewPager.removeAllViews();
}
} catch (Exception e) {
int x = 0;
}
}
子选项卡级别的片段,带有加载的图像的网格视图,在按下后退按钮时会被清除。
GridView list;
GridViewIncompleteAdapter adapter;
ArrayList<String> puzzelList = null;
ArrayList<PuzzelFileData> imagePuzzelList = null;
Override
public void onActivityCreated(Bundle bundle) {
super.onActivityCreated(bundle);
activity = getActivity();
resource = activity.getResources();
View view = getView();
puzzelList = new ArrayList<String>();
imagePuzzelList = new ArrayList<PuzzelFileData>();
path = Utils.getAppFilePath(activity);
path = path + Utils.INC_FOLDER;
list = (GridView) view.findViewById(R.id.listIncomplete);
Point p = Utils.getDisplaySize(activity);
}
@Override
public void onStop() {
super.onStop();
if(task != null)
task.cancel(true);
if(handler != null && runnable != null)
handler.removeCallbacks(runnable);
}
@Override
public void onDestroy(){
super.onDestroy();
if(puzzelList != null)
puzzelList.clear();
if(imagePuzzelList != null)
imagePuzzelList.clear();
if(adapter != null)
adapter.disponse();
}
带有标签视图的顶部布局,
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frag"
android:orientation="vertical">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"/>
<android.support.v4.app.FragmentTabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
带有网格视图的布局,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame" >
<TextView
android:id="@+id/txt_inc_nodata"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="@string/LOADING_DATA"
android:textColor="@color/subtitle_text_color"
android:textSize="@dimen/text_size_smallmedium"
android:textStyle="bold" />
<GridView
android:id="@+id/listIncomplete"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="1dp"
android:gravity="center"
android:numColumns="3" />
</RelativeLayout>
【问题讨论】:
-
请从有问题的片段中显示生命周期方法。
-
我添加了片段方法。如果有不清楚的地方,请告诉我。谢谢
-
view = getView();内onActivityCreated看起来不太好。基于此,我假设您在活动的 xml 布局中声明<fragment/>,对吗? -
我也添加了 UI。我没有按照你说的去做。谢谢
标签: android android-fragments gridview