簡體   English   中英

檢索 Firebase 圖像作為可繪制對象並在回收站視圖中顯示

[英]Retrieve Firebase images as drawables and display in recycler View

我已經實現了 RecyclerView 可以成功顯示圖像,並且現在在我的 Fragment 中一切正常 我想從 Firebase 加載圖像(圖像已經存儲在實時數據庫中及其 url)這是我的片段代碼

public RecyclerView youthRecycler;
public RecyclerView.Adapter recyclerAdapter;
public RecyclerView.LayoutManager recyclerManager;


//i just want to populate this array with drawables from images stored on firebase
int[] imagesArray={R.drawable.youthtable1,R.drawable.youthtable2,R.drawable.youthtable3};

public tableYouthFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_table_youth, container, false);
}

@Override
public void onStart() {
    super.onStart();
    //all code goes here
    init();
}
public void init()
{
    youthRecycler=getActivity().findViewById(R.id.youthRecycler);
    youthRecycler.setHasFixedSize(true);
    recyclerManager=new LinearLayoutManager(getContext());
    youthRecycler.setLayoutManager(recyclerManager);
    recyclerAdapter =new MyRecyclerAdpater(getActivity(),imagesArray);
    youthRecycler.setAdapter(recyclerAdapter);
}

這個問題已經在這里問過了,但沒有任何經過驗證的答案,在 kotlin 和 java 答案中的經過驗證的答案要求我更改我的 RecyclerView 整個實施。 我也跟進了許多關於 glide 庫的教程,但不知道是否可以將圖像加載為可繪制對象,或者我必須更改我的整個適配器實現

你的 recyclerView 應該獲取一個 url 列表,你可以在 Glide 中加載它們,為什么你需要將它們加載為 drawable 而 glide 可以將它們加載為 url:

在 onBindViewHolder() 中:

Glide.with(context).load(url).into(holder.imageYouWantToShow);

現在在片段中獲取網址:

 DatabaseRefernce imagesRef = FirebaseDatabase.getInstance().getReference();
 ValueEventListener imagesListner = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot imageUrl: dataSnapshot.getChildren()) {
           String image = imageUrl.getValue(String.class);
           imagesArray.add(image);
      }
  
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
    Log.w(TAG, "loadPost:onCancelled", databaseError.toException());

    }
};
imagesRef.addValueEventListener(imagesListner );

recyclerManager=new LinearLayoutManager(getContext());
youthRecycler.setLayoutManager(recyclerManager);
recyclerAdapter = new MyRecyclerAdpater(this,imagesArray);
youthRecycler.setAdapter(recyclerAdapter);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM