我正在尝试实现一个ViewPager,但是目前它在一个屏幕上呈现所有视图,当移动到下一个屏幕时它是空的,它包含所有内容。我有一种感觉它与碎片中的片段有关,但我无法弄明白。
在下面的屏幕截图中,您可以看到它:
这是代码的(部分)。 (ArticlePagerAdapter只是用于查找特定对象类型的proto的实现)。
public abstract class PrototypePagerActivity<T extends GenericModel> extends BaseActivity {
PrototypePagerAdapter mAdapter;
ViewPager mPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_proto_pager);
long categoryId = -1;
int articlePos = 0;
Bundle extras = getIntent().getExtras();
if (extras != null) {
//if (extras.containsKey("articleId")) articleId = extras.getInt("articleId");
if (extras.containsKey("position")) articlePos = extras.getInt("position");
if (extras.containsKey("categoryId")) categoryId = extras.getLong("categoryId");
}
mAdapter = new ArticlePagerAdapter(categoryId,getSupportFragmentManager());
mPager = (ViewPager) findViewById(R.id.protoPager);
mPager.setAdapter(mAdapter);
mPager.setCurrentItem(articlePos);
}
}
public abstract class PrototypePagerAdapter<T extends GenericModel> extends FragmentPagerAdapter {
@Override
public Fragment getItem(int position) {
try {
T object = getObjectForPosition(position);
ArticleFragment articleFragment = new ArticleFragment();
Bundle args = new Bundle();
args.putLong("articleId", object.getId());
articleFragment.setArguments(args);
return articleFragment;
} catch (Exception e) {
Log.w(iDomsAndroidApp.TAG, "Problem getting the object instance for pages", e);
return null;
}
}
public abstract T getObjectForPosition(int position) throws Exception;
@Override
public int getCount() {
return requestObjectCount();
}
public abstract int requestObjectCount();
}
public class ArticleFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_article, container, false);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.content_fragment, new PlaceholderFragment())
.commit();
}
ArticleHeaderFragment articleHeaderFragment = new ArticleHeaderFragment();
articleHeaderFragment.setArguments(getArguments());
getFragmentManager().beginTransaction().add(R.id.header_fragment, articleHeaderFragment).commit();
ArticleContentFragment articleContentFragment = ArticleContentFragment.newInstance();
articleContentFragment.setArguments(getArguments());
getFragmentManager().beginTransaction().add(R.id.content_fragment, articleContentFragment).commit();
return view;
}
}
activity_proto_pager.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/protoPager" />
</LinearLayout>
fragment_article.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/imageView"
android:src="@drawable/background"
android:scaleType="centerCrop"
android:contentDescription="@string/background_image"
android:alpha="0.6" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/audio_control_fragment" />
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/header_fragment" />
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/content_fragment" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
fragment_article_header.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/list_background_overlay"
android:layout_centerInParent="true"
tools:context="org.idoms.iDomsAndroid.fragments.ArticleHeaderFragment">
<org.idoms.iDomsAndroid.views.ResizableImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxHeight="200dp"
android:id="@+id/article_imageView"
android:layout_gravity="center_horizontal"
android:scaleType="centerCrop" />
</LinearLayout>
答案 0 :(得分:0)
你应该替换片段而不是添加。
所以,而不是.add(...)
使用.replace(....)
<强>更新强>
您正在另外两个地方添加片段。使用articleHeaderFragment
和articleContentFragment
执行上述步骤。你可以在else部分添加第二部分代码(如果是根据你的逻辑)
更新2
你也应该重温你的逻辑。与下面的代码一样,您在R.id.content_fragment
中添加了两个片段。替换可以在这里提供帮助,但您的代码将是多余的
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.content_fragment, new PlaceholderFragment())
.commit();
}
ArticleContentFragment articleContentFragment = ArticleContentFragment.newInstance();
articleContentFragment.setArguments(getArguments());
getFragmentManager().beginTransaction().add(R.id.content_fragment, articleContentFragment).commit();