2016-11-22 57 views
0

我如何可以加載圖像和視頻的RecyclerView的相同的列表,這樣,當我點擊一個視頻可以播放(如Facebook博客風格),在Facebook上的圖像被視爲與以及在列表cardViews上的視頻,有沒有任何教程,請幫助我。我真的陷入了困境,我對此沒有任何解決方案。加載視頻和圖片在RecyclerView一次

如何使一個單一RecyclerView與圖像和視頻兩者兼而有之?

下面是我onStart法及其那裏的FirebaseRecyclerAdapter它啓動列表

@Override 
    protected void onStart() { 
     super.onStart(); 

     //checkUserExists(); 
     // mAuth.addAuthStateListener(mAuthLitsener); 
try { 
    FirebaseRecyclerAdapter<Posts, BlogViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Posts, BlogViewHolder>(Posts.class, 
      R.layout.item_activity, 
      BlogViewHolder.class, 
      mDatabase) { 
     @Override 
     protected void populateViewHolder(BlogViewHolder viewHolder, final Posts model, final int position) { 



      final String key_post = getRef(position).getKey(); 
      viewHolder.setTitle(model.getEventTitle()); 
      viewHolder.setDesc(model.getEventDescription()); 
      viewHolder.setImage(c, model.getEventImage()); 
      viewHolder.setVideo(c,model.getEventImage()); 

      viewHolder.imagePost.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 

        Toast.makeText(getApplicationContext(),""+key_post,Toast.LENGTH_LONG).show(); 
        Intent singleView = new Intent(MainActivity.this,ViewSingleImage.class); 
        singleView.putExtra("Blog",key_post); 
        singleView.putExtra("EventName",eventname); 
        startActivity(singleView); 
       } 
      }); 

     } 
    }; 

    mRecyclerview.setAdapter(firebaseRecyclerAdapter); 
} 
catch (Exception ex){ 

    System.out.println("Error "+ ex); 
} 
    } 

這是MyViewHolder類,我宣佈兩個圖像和URL

public static class BlogViewHolder extends RecyclerView.ViewHolder{ 


     View mView; 
     private ImageView imagePost; 

     public BlogViewHolder(View itemView) { 
      super(itemView); 
      mView=itemView; 
      imagePost =(ImageView)mView.findViewById(R.id.post_image); 
     } 
     public void setTitle(String title){ 

      TextView post_title = (TextView)mView.findViewById(R.id.post_title); 
      post_title.setText(title); 

     } 

     public void setDesc(String desc){ 

      TextView post_desc = (TextView)mView.findViewById(R.id.post_desc); 
      post_desc.setText(desc); 
     } 


     public void setImage(final Context c,final String imageUrl){ 

     // 

      Picasso.with(c).load(imageUrl).error(R.mipmap.add_btn).fit().centerInside().placeholder(R.mipmap.add_btn) 
        .networkPolicy(NetworkPolicy.OFFLINE).into(imagePost, new Callback() { 
       @Override 
       public void onSuccess() { 

       } 

       @Override 
       public void onError() { 

        //Reloading an image again ... 
        Picasso.with(c).load(imageUrl).error(R.mipmap.add_btn).placeholder(R.mipmap.add_btn) 
          .into(imagePost); 
       } 
      }); 

     } 

     public void setVideo(final Context c, final String videoUrl){ 

     videoLayout = (FullscreenVideoLayout) mView.findViewById(R.id.post_video); 
      // videoLayout.setActivity(this); 
      // videoLayout.setActivity(get); 

      Uri videoUri = Uri.parse(videoUrl); 
      try { 
       videoLayout.setVideoURI(videoUri); 

      } catch (IOException e) { 
       e.printStackTrace(); 
      } 


     } 


    } 

所以一般在ViewHolder我宣佈,圖像和視頻,所以在OnStart方法是我把我的適配器,以便下載ImageUrlVideoUrl,但檢查他們的邏輯仍然是棘手的,在我的身邊

+0

請看看下面的鏈接,並使用圖像和文本視圖Viewholder和onBindViewHolder創建視圖()方法之前檢查的條件。 http://stackoverflow.com/questions/30978030/playing-video-using-textureview-in-recyclerview –

+0

@Chayan,感謝您的回覆,但是當我看到你纔會發送,它只是所有關於影片,帖子鏈接,我想要的是當人發佈一個圖像它加載到列表中,當人發佈一個視頻時,它也加載到RecyclerView,我希望它有一個自動感,當圖像和視頻加載(就像facebook的方式一樣)。 –

+0

@huzaifah ---你是否從服務器獲取圖像URL和視頻URL ??如果你正在從服務器獲取對象類型,那麼它是圖像或視頻,然後在OncreateViewHolder()方法中創建新的視圖持有者並綁定obBindView中的數據() 方法。你婉我送一些Pescudo代碼 –

回答

0
Please do following way:-- 

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<VideoView 
    android:id="@+id/videoView" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 

<ImageView 
    android:id="@+id/imageView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_alignParentTop="true" /> 

</RelativeLayout> 

    In Adapterclass:--- 
    public class MyRecyclerViewAdapter extends RecyclerView 
    .Adapter<MyRecyclerViewAdapter 
    .DataObjectHolder> { 
    private static String LOG_TAG = "MyRecyclerViewAdapter"; 
    private ArrayList<DataObject> mDataset; 
    private static MyClickListener myClickListener; 

    public static class DataObjectHolder extends RecyclerView.ViewHolder 
     implements View 
     .OnClickListener { 
    TextView label; 
    TextView dateTime; 

    public DataObjectHolder(View itemView) { 
     super(itemView); 
     label = (TextView) itemView.findViewById(R.id.textView); 
     dateTime = (TextView) itemView.findViewById(R.id.textView2); 
     Log.i(LOG_TAG, "Adding Listener"); 
     itemView.setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View v) { 
     myClickListener.onItemClick(getPosition(), v); 
    } 
} 

    public void setOnItemClickListener(MyClickListener myClickListener) { 
    this.myClickListener = myClickListener; 
} 

    public MyRecyclerViewAdapter(ArrayList<DataObject> myDataset) { 
    mDataset = myDataset; 
} 

    @Override 
    public DataObjectHolder onCreateViewHolder(ViewGroup parent, 
              int viewType) { 
    View view = LayoutInflater.from(parent.getContext()) 
      .inflate(R.layout.recyclerview_item, parent, false); 

    DataObjectHolder dataObjectHolder = new DataObjectHolder(view); 
    return dataObjectHolder; 
} 

    @Override 
    public void onBindViewHolder(DataObjectHolder holder, int position) { 
    DataObject dataset=mDataset.get(position) 
    // Check here image or video 
    if(mdataset contains image){ 
     videoview invisible or gone 
    }else{ 
     image invisible or gone 
    ) 

    public void addItem(DataObject dataObj, int index) { 
    mDataset.add(dataObj); 
    notifyItemInserted(index); 
} 

    public void deleteItem(int index) { 
    mDataset.remove(index); 
    notifyItemRemoved(index); 
} 

    @Override 
    public int getItemCount() { 
    return mDataset.size(); 
} 

    public interface MyClickListener { 
    public void onItemClick(int position, View v); 
} 

}

+0

可以嘗試回答這個帖子根據你的答案http:// stackoverflow。com/questions/40822813/both-images-and-videos-in-a-recyclerview-using-imageview-and-videoview謝謝 –

0

我可以看到你正在使用Firebase,所以你得到getEventImage()getEventVideo(),所以你必須檢查網址爲空或在setImage()setVideo()方法中。試試這個代碼。並參見:

public void setImage(final Context c,final String imageUrl){ 

      try { 
       if (imageUrl!=null) { 
        // 

        Picasso.with(c).load(imageUrl).error(R.mipmap.add_btn).fit().centerInside().placeholder(R.mipmap.add_btn) 
          .networkPolicy(NetworkPolicy.OFFLINE).into(imagePost, new Callback() { 
         @Override 
         public void onSuccess() { 

         } 

         @Override 
         public void onError() { 

          //Reloading an image again ... 
          Picasso.with(c).load(imageUrl).error(R.mipmap.add_btn).placeholder(R.mipmap.add_btn) 
            .into(imagePost); 
         } 
        }); 





       } else { 

        imagePost.setVisibility(View.GONE); 
       } 
      } 
      catch (Exception e){ 

      } 

     } 

     public void setVideo(final Context c, final String videoUrl){ 
      try { 
       if (videoUrl!=null) { 
        try { 
         Uri videoUri = Uri.parse(videoUrl); 
         try { 
          videoLayout.setVideoURI(videoUri); 
          videoLayout.setTag(videoUrl); 
          String hasVideo_string = (String) videoLayout.getTag(); 
          boolean hasVideo = Boolean.parseBoolean(hasVideo_string); 


         } catch (IOException e) { 
          e.printStackTrace(); 
         } 
        } catch (Exception e) { 
         System.out.println("Error :" + e); 
        } 

       } else { 
        videoLayout.setVisibility(View.GONE); 
       } 
      } 
      catch (Exception e){ 

      } 


     } 

希望這個工程。

+0

謝謝@Lutaaya,它是一個很好的簡單邏輯,我一直堅持下去,感謝兄弟們:) –

相關問題