假设我有一个带有ListView的片段或带有图像的GridView。当我使用编程方式或后退按钮进行转换时,我看到这些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();
}
以下片段具有顶级视图分页器,该分页器使用带有图像的gridview加载片段。在子级别片段上清除网格视图时会出现视觉问题。顶级片段没有这样的问题。
使用viewpager for tabs的Top 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>
答案 0 :(得分:1)
根据您的代码,您自己在onDestroy
内清除清单:
@Override
public void onDestroy() {
super.onDestroy();
if(puzzelList != null)
puzzelList.clear();
if(imagePuzzelList != null)
imagePuzzelList.clear();
if(adapter != null)
adapter.disponse();
}
此外,您替换您的片段,因此在这种情况下应调用onDestroy
。请记住生命周期流程,在您的情况下将是:
Fragment1.onPause();
Fragment1.onStop();
Fragment1.onDestroy();
Fragment2.onCreate();
Fragment2.onResume();
为了更好地理解您可以正确显示位图,我建议您检查Handling Bitmaps。