我有一个片段。在这个片段中,我宣布了一个鳍状肢和一个水平回收视图。我遇到了问题而且不知道如何解决它们。
当我打开应用程序时,一切看起来都很不错screenshot link,但是当我将屏幕旋转到横向时,我看到了鳍状肢screenshot link的视觉错误,但当我将其旋转回肖像时已经看到了recyclerview已经screenshot link。
HomeFragment.class
public class HomeFragment extends RoboFragment {
@InjectView(R.id.viewFlipper)
ViewFlipper mViewFlipper;
ArrayList<Category> categoriesList;
private View rootView;
private GestureDetector mGestureDetector;
private int[] mResources = {
R.mipmap.temp_screen_1,
R.mipmap.temp_screen_2,
R.mipmap.temp_screen_3,
R.mipmap.temp_screen_4
};
public void createCategories() {
categoriesList = new ArrayList<>();
categoriesList.add(new Category("Android", R.mipmap.icon_android));
categoriesList.add(new Category("IOS", R.mipmap.icon_ios));
categoriesList.add(new Category("Windows Phone", R.mipmap.icon_windows_phone));
categoriesList.add(new Category("Blackberry", R.mipmap.icon_blackberry));
categoriesList.add(new Category("Budget phones", R.mipmap.icon_phone));
categoriesList.add(new Category("Tablets", R.mipmap.icon_tablet));
categoriesList.add(new Category("Jewelry & Watch", R.mipmap.icon_watch));
categoriesList.add(new Category("Health & Beauty", R.mipmap.icon_umbrella));
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_home, container, false);
return rootView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Utils.showToolbar(getActivity());
setSlideShow();
setCategories();
}
private void setCategories() {
createCategories();
RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.my_recycler_view);
recyclerView.setHasFixedSize(true);
HorizontalRecycleViewAdapter adapter = new HorizontalRecycleViewAdapter(getActivity(), categoriesList);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setAdapter(adapter);
}
private void setSlideShow() {
for (int mResource : mResources) {
ResizableImageView imgageView = new ResizableImageView(getActivity());
imgageView.setImageResource(mResource);
mViewFlipper.addView(imgageView);
}
mViewFlipper.setInAnimation(getActivity(), android.R.anim.fade_in);
mViewFlipper.setOutAnimation(getActivity(), android.R.anim.fade_out);
CustomGestureDetector customGestureDetector = new CustomGestureDetector(mViewFlipper);
mGestureDetector = new GestureDetector(getActivity(), customGestureDetector);
mViewFlipper.setAutoStart(true);
mViewFlipper.setFlipInterval(5000);
}
}
fragment_home.xml
<?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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true">
<ViewFlipper
android:id="@+id/viewFlipper"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v7.widget.RecyclerView
android:layout_below="@+id/viewFlipper"
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="200dp"
android:scrollbars="none" />
</RelativeLayout>
</ScrollView>
MainActivity.class
@ContentView(R.layout.activity_main)
public class MainActivity extends RoboActionBarActivity {
@InjectView(R.id.tool_bar)
Toolbar toolbar;
SlidingMenu menu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setToolBar();
setSlideMenu();
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new SplashFragment())
.commit();
} else {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new HomeFragment())
.commit();
}
}
private void setToolBar() {
setSupportActionBar(toolbar);
}
private void setSlideMenu() {
menu = new SlidingMenu(this);
menu.setMenu(R.layout.slidemenu);
menu.setMode(SlidingMenu.LEFT);
menu.setSlidingEnabled(false);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menu.setFadeDegree(0.35f);
menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
menu.setBehindWidthRes(R.dimen.slidingmenu_behind_width);// Setting toolbar as the ActionBar with setSupportActionBar() call
}
public void toggleSlidingMenu(View view) {
menu.toggle();
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<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=".activities.MainActivity">
<include
android:id="@+id/tool_bar"
layout="@layout/tool_bar" />
<FrameLayout
android:layout_below="@+id/tool_bar"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.MainActivity"
tools:ignore="MergeRootFrame" />
</RelativeLayout>
答案 0 :(得分:1)
您似乎多次添加您的片段,没有错误,您只有两次相同的视图,如果您将方向更改为纵向,则您有3 ...等等。
NULL
使用if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new SplashFragment())
.commit();
} else {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new HomeFragment()) // <- here
.commit();
}
或检查片段是否已添加。