这是我用于图像轮播的示例,现在它适用于本地图像。我尝试了一切使其适用于来自firebase数据库的图像。这是适用于本地图像的示例。
public class ItemFragment extends Fragment {
FirebaseDatabase firebaseDatabaseInstance;
private DatabaseReference booksInstance;
BookGridAdapter adapter ;
AlbumDBHandler db;
private static final String POSITON = "position";
private static final String SCALE = "scale";
private static final String DRAWABLE_RESOURE = "resource";
private int screenWidth;
private int screenHeight;
private int[] imageArray = new int[]{R.drawable.zelal, R.drawable.mo2lfat,
R.drawable.saidkotb, R.drawable.sawtyat, R.drawable.black_image,
R.drawable.zelal, R.drawable.zelaltabs, R.drawable.mo2lfat,
R.drawable.sawtyat, R.drawable.zelal};
ArrayList<BookData> books = new ArrayList<>();
public static Fragment newInstance(testImage context, int pos, float scale) {
Bundle b = new Bundle();
b.putInt(POSITON, pos);
b.putFloat(SCALE, scale);
return Fragment.instantiate(context, ItemFragment.class.getName(), b);
}
private void getData(){
firebaseDatabaseInstance = FirebaseDatabase.getInstance();
// get reference to 'users' node
booksInstance = firebaseDatabaseInstance.getReference("mo2lfat");
books.clear();
books.addAll(db.getAllBook());
booksInstance.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
storeData(dataSnapshot);
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.w(TAG, "Failed to read value.", error.toException());
}
});
}
private void storeData(DataSnapshot snapshot) {
books.clear();
for (DataSnapshot alert: snapshot.getChildren()) {
BookData book = new BookData(
(String)alert.child("id").getValue(),
(String)alert.child("book_name").getValue(),
(String)alert.child("book_path").getValue(),
(String)alert.child("book_path").getValue(),
"",
(String)alert.child("image_path").getValue(),
(String)alert.child("image_path").getValue(),
""
);
db.insertBook(book);
}
books.addAll(db.getAllBook());
adapter.notifyDataSetChanged();
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWidthAndHeight();
}
@SuppressLint("SetTextI18n")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
CarouselLinearLayout root = (CarouselLinearLayout) linearLayout.findViewById(R.id.root_container);
ImageView imageView = (ImageView) linearLayout.findViewById(R.id.pagerImg);
imageView.setLayoutParams(layoutParams);
// imageView.setImageResource(imageArray.(postion))
imageView.setImageResource(imageArray[postion]);
root.setScaleBoth(scale);
return linearLayout;
}
}