我有Activity
tabLayout
,我试图通过实施ViewPagerAdapter
在第一个片段上创建图片 SlidShow ,我是使用带圆圈指示符的slidshow,圆圈指示符会成功显示,但图像不会显示。
这就是我所做的:
我的片段xml布局:注意我RecyclerView
底部的水平ViewPager
正常工作。
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/topLayout">
<RelativeLayout
android:id="@+id/sliderLyout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent">
<android.support.v4.view.ViewPager
android:id="@+id/sliderPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true" />
<me.relex.circleindicator.CircleIndicator
android:id="@+id/indicator"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/title_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:paddingLeft="10dp"
android:paddingStart="10dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/sliderLyout">
<com.biliki.biliki.bilikiapp.uitemplate.font.RobotoTextView
android:id="@+id/itemTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:layout_toLeftOf="@+id/btnMore"
android:paddingLeft="10dp"
android:text="@string/lastoffersTitle"
style="@style/TextViewAppearance.Title"
/>
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/my_list_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/title_layout"
app:layout_constraintVertical_bias="0.0"
app:rvp_singlePageFling="true"
app:rvp_triggerOffset="0.1" />
<RelativeLayout
android:id="@+id/restLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/my_list_recycler">
<com.biliki.biliki.bilikiapp.uitemplate.font.RobotoTextView
android:id="@+id/restTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:paddingLeft="10dp"
android:text="@string/restTitle"
style="@style/TextViewAppearance.Title"/>
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/restaurantsRecycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:orientation="horizontal"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/restLayout">
</android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>
</ScrollView>
这是我的适配器类:我使用Glide Library加载图像。
public class SliderAdapter extends PagerAdapter {
private Context mContext;
private int[] mDataList;
public SliderAdapter(Context mContext, int[] mDataList) {
this.mContext = mContext;
this.mDataList = mDataList;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View rootView = LayoutInflater.from(mContext).inflate(R.layout.slider_item_layout, container , false);
ImageView slideImg = (ImageView) rootView.findViewById(R.id.slide_img);
int img = mDataList[position];
GlideApp.
with(mContext)
.load(img)
.centerCrop()
.transition(DrawableTransitionOptions.withCrossFade())
.into(slideImg);
container.addView(rootView, 0);
return rootView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public int getCount() {
if(mDataList.length >0){
return mDataList.length;
}
else{
return 0;
}
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}
}
片段类:
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
createSampleData();
//...
sliderPager = (ViewPager) rootView.findViewById(R.id.sliderPager);
sliderPager.setAdapter(new SliderAdapter(getContext(),picturesList));
CircleIndicator indicator = (CircleIndicator)rootView.findViewById(R.id.indicator);
indicator.setViewPager(sliderPager);
// Auto start of viewpager
final Handler handler = new Handler();
final Runnable Update = new Runnable() {
public void run() {
if (currentPage == picturesList.length) {
currentPage = 0;
}
sliderPager.setCurrentItem(currentPage++, true);
}
};
Timer swipeTimer = new Timer();
swipeTimer.schedule(new TimerTask() {
@Override
public void run() {
handler.post(Update);
}
}, 2500, 2500);
return rootView;
}
最后这是结果: