问题
friends I just implement code for the View pager. But now I am stuck with the problem that how to fetch a collection from the firestore into a view pager. Means I have a collection in that collection I have 2 documents. each document has some parameters. I want the "mSliderImage" parameter will be fetched into a ViewPager from each and every document.
My Code Some How Looks Like This.
I am using some dependencies for my custom viewpager.
implementation 'com.github.smarteist:autoimageslider:1.3.2'
implementation 'com.github.bumptech.glide:glide:4.10.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.10.0'
image_slider_myshop.xml
<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"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_auto_image_slider"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY" />
<ImageView
android:id="@+id/iv_gif_container"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="bottom|start"
android:layout_margin="50dp" />
<FrameLayout
android:id="@+id/fl_shadow_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@drawable/bg_overlay">
<TextView
android:id="@+id/tv_auto_image_slider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="25dp"
android:padding="6dp"
android:textColor="#FFF"
android:textSize="18sp" />
</FrameLayout>
</FrameLayout>
main_image_slider.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/mpm_slider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
app:cardCornerRadius="10sp"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<com.smarteist.autoimageslider.SliderView
android:id="@+id/imageSlider"
android:layout_width="match_parent"
android:layout_height="150dp"
app:sliderAnimationDuration="600"
app:sliderAutoCycleEnabled="true"
app:sliderCircularHandlerEnabled="true"
app:sliderIndicatorAnimationDuration="600"
app:sliderIndicatorGravity="right|bottom"
app:sliderIndicatorMargin="15dp"
app:sliderIndicatorOrientation="horizontal"
app:sliderIndicatorPadding="3dp"
app:sliderIndicatorRadius="2dp"
app:sliderIndicatorSelectedColor="#5A5A5A"
app:sliderIndicatorUnselectedColor="#FFF"
app:sliderScrollTimeInSec="7"
app:sliderStartAutoCycle="true" />
</androidx.cardview.widget.CardView>
home_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/background"
android:fitsSystemWindows="true"
android:overScrollMode="never"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical">
<include layout="@layout/main_header"/>
</LinearLayout>
</ScrollView>
Java Codes:
Adapter Code: "The Images which get fetched in below switch cases code are an image which directly comes from google. Instead of this, I want image will get fetched from firebase firestore "Banner" collection for each document having "mSliderImage" parameter"
public class SliderImageAdapter extends SliderViewAdapter<SliderImageAdapter.SliderAdapterVH> {
private Context context;
private int mCount;
public SliderImageAdapter(Context context) {
this.context = context;
}
public void setCount(int count) {
this.mCount = count;
}
@Override
public SliderAdapterVH onCreateViewHolder(ViewGroup parent) {
View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.image_slider_myshop, null);
return new SliderAdapterVH(inflate);
}
@Override
public void onBindViewHolder(SliderAdapterVH viewHolder, final int position) {
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
switch (position) {
case 0:
Glide.with(viewHolder.itemView)
.load("https://laz-img-cdn.alicdn.com/images/ims-web/TB1pHADjBr0gK0jSZFnXXbRRXXa.jpg_1200x1200.jpg")
.fitCenter()
.into(viewHolder.imageViewBackground);
break;
case 1:
Glide.with(viewHolder.itemView)
.load(" switch (position) {
case 0:
Glide.with(viewHolder.itemView)
.load(" switch (position) {
case 0:
Glide.with(viewHolder.itemView)
.load("https://www.arihantwebtech.com/images/design-notice/banner-design.png")
.fitCenter()
.into(viewHolder.imageViewBackground);
break;
case 1:
Glide.with(viewHolder.itemView)
.load("https://www.arihantwebtech.com/images/design-notice/banner-design.png")
.fitCenter()
.into(viewHolder.imageViewBackground);
break;")
.fitCenter()
.into(viewHolder.imageViewBackground);
break;
case 1:
Glide.with(viewHolder.itemView)
.load("https://www.arihantwebtech.com/images/design-notice/banner-design.png")
.fitCenter()
.into(viewHolder.imageViewBackground);
break;")
.fitCenter()
.into(viewHolder.imageViewBackground);
break;
}
}
public int getCount() {
//slider view count could be dynamic size
return mCount;
}
class SliderAdapterVH extends SliderViewAdapter.ViewHolder {
View itemView;
ImageView imageViewBackground;
ImageView imageGifContainer;
TextView textViewDescription;
public SliderAdapterVH(View itemView) {
super(itemView);
imageViewBackground = itemView.findViewById(R.id.iv_auto_image_slider);
imageGifContainer = itemView.findViewById(R.id.iv_gif_container);
textViewDescription = itemView.findViewById(R.id.tv_auto_image_slider);
this.itemView = itemView;
}
}
}
Home Fragment Code:
SliderView sliderMyshop;
public static Fragment newInstance() {
Bundle args = new Bundle();
HomeFragment fragment = new HomeFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
}
}
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable final Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.home_fragment, container, false);
sliderMyshop = view.findViewById(R.id.imageSlider);
final SliderImageAdapter sliderImageAdapter = new SliderImageAdapter(getContext());
sliderImageAdapter.setCount(4);
sliderMyshop.setSliderAdapter(sliderImageAdapter);
sliderMyshop.setIndicatorAnimation(IndicatorAnimations.WORM); //set indicator animation by using SliderLayout.IndicatorAnimations. :WORM or THIN_WORM or COLOR or DROP or FILL or NONE or SCALE or SCALE_DOWN or SLIDE and SWAP!!
sliderMyshop.setSliderTransformAnimation(SliderAnimations.SIMPLETRANSFORMATION);
sliderMyshop.setIndicatorSelectedColor(Color.WHITE);
sliderMyshop.setIndicatorUnselectedColor(Color.GRAY);
sliderMyshop.startAutoCycle();
sliderMyshop.setOnIndicatorClickListener(new DrawController.ClickListener() {
@Override
public void onIndicatorClicked(int position) {
sliderMyshop.setCurrentPagePosition(position);
}
});
I am also attaching the pic of the
来源:https://stackoverflow.com/questions/59346895/how-to-fetch-collection-from-firebase-cloud-firestore-into-a-viewpager