Android Recyclerview 中的 ViewPagerIndicator 没有显示
Android ViewPageIndicator inside a Recylerview is not showing
我在recylcerview
项中添加了一个ViewPageIndicator
,这是recyclerview
项layout
。
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardBackgroundColor="@color/background"
card_view:cardCornerRadius="0dp"
card_view:cardElevation="0dp"
android:fillViewport="true">
<LinearLayout
android:id="@+id/main_container"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="100dp" />
<com.viewpagerindicator.CirclePageIndicator
android:id="@+id/pager_indicator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:paddingLeft="10dp"
android:paddingRight="10dp" />
</LinearLayout>
</android.support.v7.widget.CardView>
ViewPageIndicator
在 scrolling down
时不显示,但在 scrolling up
时它在某些项目上显示,请帮助。
问题出在 viewpager 适配器上,我同时使用了 FragmentStatePagerAdapter
和 FragmentPagerAdapter
但没有成功。所以最后将适配器更改为 PagerAdapter
并且我的片段视图在 instantiateItem
中实现 PagerAdapter
public class FeedImageAdapter extends PagerAdapter {
private ArrayList<FeedContentGroup> feedContentGroups;
private Context mContext;
private LayoutInflater layoutInflater;
public FeedImageAdapter(Context context, ArrayList<FeedContentGroup> feedContentGroups) {
this.mContext = context;
this.feedContentGroups = feedContentGroups;
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public Object instantiateItem(ViewGroup collection, int position) {
View v = layoutInflater.inflate(R.layout.feed_image_view, null);
ImageView image1 = (ImageView) v.findViewById(R.id.image1);
FeedContentGroup mFeedContentGroup = feedContentGroups.get(position);
if (mFeedContentGroup.getContent1() != null && mFeedContentGroup.getContent1().getThumbnail_file_path() != null) {
Glide.with(mContext).load(mFeedContentGroup.getContent1().getThumbnail_file_path()).into(image1);
} else {
image1.setVisibility(View.GONE);
}
((ViewPager) collection).addView(v, 0);
return v;
}
@Override
public void destroyItem(View collection, int position, Object view) {
((ViewPager) collection).removeView((View) view);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public int getCount() {
return feedContentGroups.size();
}
}
我在recylcerview
项中添加了一个ViewPageIndicator
,这是recyclerview
项layout
。
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardBackgroundColor="@color/background"
card_view:cardCornerRadius="0dp"
card_view:cardElevation="0dp"
android:fillViewport="true">
<LinearLayout
android:id="@+id/main_container"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="100dp" />
<com.viewpagerindicator.CirclePageIndicator
android:id="@+id/pager_indicator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:paddingLeft="10dp"
android:paddingRight="10dp" />
</LinearLayout>
</android.support.v7.widget.CardView>
ViewPageIndicator
在 scrolling down
时不显示,但在 scrolling up
时它在某些项目上显示,请帮助。
问题出在 viewpager 适配器上,我同时使用了 FragmentStatePagerAdapter
和 FragmentPagerAdapter
但没有成功。所以最后将适配器更改为 PagerAdapter
并且我的片段视图在 instantiateItem
中实现 PagerAdapter
public class FeedImageAdapter extends PagerAdapter {
private ArrayList<FeedContentGroup> feedContentGroups;
private Context mContext;
private LayoutInflater layoutInflater;
public FeedImageAdapter(Context context, ArrayList<FeedContentGroup> feedContentGroups) {
this.mContext = context;
this.feedContentGroups = feedContentGroups;
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public Object instantiateItem(ViewGroup collection, int position) {
View v = layoutInflater.inflate(R.layout.feed_image_view, null);
ImageView image1 = (ImageView) v.findViewById(R.id.image1);
FeedContentGroup mFeedContentGroup = feedContentGroups.get(position);
if (mFeedContentGroup.getContent1() != null && mFeedContentGroup.getContent1().getThumbnail_file_path() != null) {
Glide.with(mContext).load(mFeedContentGroup.getContent1().getThumbnail_file_path()).into(image1);
} else {
image1.setVisibility(View.GONE);
}
((ViewPager) collection).addView(v, 0);
return v;
}
@Override
public void destroyItem(View collection, int position, Object view) {
((ViewPager) collection).removeView((View) view);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public int getCount() {
return feedContentGroups.size();
}
}