当应用程序启动时,viewPager会正确显示,但是当我更改片段然后切换回来时,viewPager什么都不显示。我该如何解决这个问题?
结构是FragmentActivity> Fragment>(ViewPager with PagerAdapter)
应用启动时 before
切换到其他片段并返回后。 after
这是代码。
public class BuyFragment extends Fragment {
private final Float REFRESH_TRIGGER_DISTANCE = 100f; //pixel
private final Float iMAGE_HW_RATE = 0.4f;
ViewPager viewPager;
CustomPagerAdapter pagerAdapter;
ArrayList<String> adPaths;
DisplayMetrics metric;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
inflater.inflate(R.layout.fragment_buy, container);
metric = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metric);
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onResume() {
initViewPager();
initScrollView();
super.onResume();
}
private void initViewPager() {
viewPager = (ViewPager) getActivity().findViewById(R.id.viewpager_buy);
adPaths = EntityUtil.getADPaths();
pagerAdapter = new CustomPagerAdapter(getActivity(), adPaths);
viewPager.setAdapter(pagerAdapter);
int width = metric.widthPixels;
int height = Integer.parseInt(new java.text.DecimalFormat("0").format(width * iMAGE_HW_RATE));
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width, height);
viewPager.setLayoutParams(params);
params.setMargins(0, 10, 0, 0);
params.addRule(RelativeLayout.BELOW, R.id.layout_buy_category);
CirclePageIndicator pageIndicator = (CirclePageIndicator) getActivity().findViewById(R.id.viewpagerindicator_buy);
pageIndicator.setViewPager(viewPager);
}
private void initScrollView() {
final PullToRefreshScrollView pullToRefreshScrollView = (PullToRefreshScrollView) getActivity().findViewById(R.id.layout_buy_refresh);
pullToRefreshScrollView.setOnRefreshListener(new OnRefreshListener<ScrollView>() {
@Override
public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
pagerAdapter.clearPicassoCache(adPaths);
refreshView.onRefreshComplete();
}
});
}
}
fragment_buy.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="wrap_content"
android:background="@color/grey_200"
tools:context=".activity.MainActivity" xmlns:app="http://schemas.android.com/apk/res/com.liuchang">
<LinearLayout
android:id="@+id/layout_buy_titlebar"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:layout_alignParentTop="true"
android:background="@color/teal_500"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_localize"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/teal_500"
android:onClick="toLocalizeActivity"
android:text="城市"
android:textColor="@color/white" />
<TextView
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_marginBottom="12dip"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="12dip"
android:layout_weight="3"
android:background="@drawable/corner_bg"
android:clickable="true"
android:gravity="center"
android:onClick="toSearchActivity"
android:text="请输入药店或药品名称"
android:textColor="@color/black" />
</LinearLayout>
<com.handmark.pulltorefresh.library.PullToRefreshScrollView
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:id="@+id/layout_buy_refresh"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/layout_buy_titlebar" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/layout_buy_category"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
...several buttons...
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager_buy"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/layout_buy_category" />
<com.viewpagerindicator.CirclePageIndicator
android:id="@+id/viewpagerindicator_buy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/viewpager_buy"
android:layout_marginTop="5dip"
app:fillColor="@color/teal_500" />
</RelativeLayout>
</ScrollView>
</com.handmark.pulltorefresh.library.PullToRefreshScrollView>
</RelativeLayout>
答案 0 :(得分:0)
更改onCreateView
&amp; onResume
方法没有设置布局&amp;适配器相关。
在OnCreateView
您错过了返回虚增的视图。这里我在浏览视频通话onCreateView
&amp;之后更新了initScrollView();
方法。 initViewPager();
您也可以在Fragment的onViewCreated
方法中调用这两种方法。但在这里,我用onCreateView
尝试以下代码&amp;如果有的话,请告诉我
public class BuyFragment extends Fragment {
private final Float REFRESH_TRIGGER_DISTANCE = 100f; //pixel
private final Float iMAGE_HW_RATE = 0.4f;
private View fragmentView;
ViewPager viewPager;
CustomPagerAdapter pagerAdapter;
ArrayList<String> adPaths;
DisplayMetrics metric;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (fragmentView == null)
fragmentView = inflater.inflate(R.layout.fragment_buy, container, false);
metric = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metric);
initViewPager();
initScrollView();
return fragmentView;
}
@Override
public void onResume() {
super.onResume();
}
private void initViewPager() {
if (fragmentView != null) {
viewPager = (ViewPager) fragmentView.findViewById(R.id.viewpager_buy);
adPaths = EntityUtil.getADPaths();
pagerAdapter = new CustomPagerAdapter(getActivity(), adPaths);
viewPager.setAdapter(pagerAdapter);
int width = metric.widthPixels;
int height = Integer.parseInt(new java.text.DecimalFormat("0").format(width * iMAGE_HW_RATE));
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width, height);
viewPager.setLayoutParams(params);
params.setMargins(0, 10, 0, 0);
params.addRule(RelativeLayout.BELOW, R.id.layout_buy_category);
CirclePageIndicator pageIndicator = (CirclePageIndicator) fragmentView.findViewById(R.id.viewpagerindicator_buy);
pageIndicator.setViewPager(viewPager);
}
}
private void initScrollView() {
if (fragmentView != null) {
final PullToRefreshScrollView pullToRefreshScrollView = (PullToRefreshScrollView) fragmentView.findViewById(R.id.layout_buy_refresh);
pullToRefreshScrollView.setOnRefreshListener(new OnRefreshListener<ScrollView>() {
@Override
public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
pagerAdapter.clearPicassoCache(adPaths);
refreshView.onRefreshComplete();
}
});
}
}
}
答案 1 :(得分:0)
由于您的ViewPager位于Fragment而非活动内,因此您应该在适配器或适配器参考中尽可能使用getChildFragmentManager()
而不是getActivity().getSupportFragmentManager();
。