如何使用带有firebase数据库的图像轮播

时间:2017-04-16 06:19:23

标签: android firebase firebase-realtime-database

这是我用于图像轮播的示例,现在它适用于本地图像。我尝试了一切使其适用于来自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;
    }

}

1 个答案:

答案 0 :(得分:0)

您必须获取图片的网址,使用该网址加载图片 对于加载图像,您可以使用高效的图像加载库。 这些是图书馆 glidepicasso

这个库也很容易使用。

 @Override
 public void onStart() {
     super.onStart();
     EventBus.getDefault().register(this);
 }

 @Override
 public void onStop() {
     super.onStop();
     EventBus.getDefault().unregister(this);
 }