Есть активити в нем через ViewPager отображаются статьи. Внутри ViewPager фрагменты. Проблема в том что фрагменты не отображаются, не могу понять почему((
ArticleActivity.java
public class ArticleActivity extends FragmentActivity {
private int articleOrderNumber;
private PagerAdapter pagerAdapter;
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_article);
pagerAdapter = new ArticleActivityPagerAdapter(getSupportFragmentManager(), ((MyApplication) getApplication()).getArticles().size());
viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPager.setAdapter(pagerAdapter);
viewPager.setCurrentItem(articleOrderNumber - 1);
}
}
activity_article.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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
tools:context=".ArticleActivity"
android:orientation="vertical">
<ImageView
android:id="@+id/backButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_gravity="left"
android:src = "@drawable/back"
android:gravity="left"/>
<ImageView
android:id="@+id/menuButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:clickable="true"
android:src="@drawable/pocke_ball"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true" />
<ImageView
android:id="@+id/nextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:clickable="true"
android:src = "@drawable/forward"
android:text="next" />
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:layout_above="@id/menuButton"
android:id="@+id/viewPager"/>
ArticleActivityPagerAdapter.java
public class ArticleActivityPagerAdapter extends FragmentStatePagerAdapter {
private int countOfArticles;
public ArticleActivityPagerAdapter(FragmentManager fm, int countOfArticles) {
super(fm);
this.countOfArticles = countOfArticles;
}
@Override
public int getCount() {
return countOfArticles;
}
@Override
public Fragment getItem(int position) {
return ArticleFragment.newInstance(position);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
ArticleFragment.java
public class ArticleFragment extends Fragment {
int articleOrderNumber;
public static ArticleFragment newInstance(int page) {
ArticleFragment fragment = new ArticleFragment();
Bundle args = new Bundle();
args.putInt(Article.ORDER_NUMBER, page + 1);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
articleOrderNumber = getArguments().getInt(Article.ORDER_NUMBER);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Activity activity = getActivity();
View rootView = inflater.inflate(
R.layout.article_fragment, container, false);
ArticleRepository articles = ((MyApplication) activity.getApplication()).getArticles();
Article article = articles.getByOrderNumber(articleOrderNumber);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(activity);
RecyclerView recView = (RecyclerView)rootView.findViewById(R.id.articleRecView);
recView.setLayoutManager(layoutManager);
recView.setAdapter(new ArticleRecViewAdapter(article, activity));
return rootView;
}
}
article_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".ArticleFragment">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:requiresFadingEdge="vertical"
android:id="@+id/articleRecView" />
</FrameLayout>
R.layout.article_fragment
)android:layout_height="wrap_content"
- выставить его вmatch_parent
в разных местах