在RecyclerView上加载视频和图像一次

时间:2016-11-22 08:22:28

标签: java android video firebase android-imageview

如何在RecyclerView的同一列表中加载图片和视频,以便当我点击可以播放的视频(如Facebook博客风格)时,可以看到Facebook上的图像以及视频上的视频列表的cardViews,有没有这方面的教程请帮助我。我真的陷入困境,我对此没有任何解决方案。

如何使用图像和视频制作单个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 Class,我声明了Images和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方法中我放置了我的适配器,以便下载ImageUrl和{{ 1}},但检查它们的逻辑仍然很棘手,在我身边

2 个答案:

答案 0 :(得分: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);
}

}

答案 1 :(得分:0)

我可以看到您正在使用<form action="uploadimage.php" method="POST" enctype="multipart/form-data"> <div align="center"><table border=0>'; echo"<tr><td>Caption: </td><td><input type='text' name='caption'></td></tr> <tr><td>Album</td><td>"; $Query='SELECT * FROM `gallery`'; $result=mysqli_query($connection,$Query); echo'<select name="albums">'; $count=0; $previous=-1; while($data=mysqli_fetch_array($result)) { if($previous<$data['album']) { echo "<option value=".$data['album'].">".$data['album']."</option>"; $previous=$data['album']; } if($count<=$data['album']) { $count=$data['album']+1; } } echo "<option value='.$count.'>New Album</option>"; echo"</select></td></tr> <tr><td>Picture: </td><td><input type='file' name='photo'></td></tr> <tr><td></td><td><input type='submit' name='upload_btn' value='upload'></td></tr> </table></div> </form> ,因此您获得了require_once"connection.php"; $target_Path='img/displays/'; $caption=$_POST['caption']; $albums=$_POST['albums']; $target_Path = $target_Path.basename($_FILES['photo']['name'] ); move_uploaded_file( $_FILES['photo']['tmp_name'], $target_Path); $withoutExt = preg_replace("/\\.[^.\\s]{3,4}$/", "", $target_Path); mysqli_query($connection,"INSERT INTO `ett`.`gallery` (`id` ,`album`,`name`,`path`)VALUES (NULL,'".$albums."','".$caption."','".$withoutExt."')"); Firebase,因此您必须检查网址是否为空或位于getEventImage()getEventVideo(),方法。试试这段代码。见:

setImage()

希望这有效。