Search code examples
javaandroidandroid-gridviewbaseadapterandroid-videoview

Video Views are replacing their position in a grid view and sometimes 1 video view is repeating in a grid view


Basically I have a list of videos I want to show all of the videos in a grid view and keep them playing repeatedly for example if 1 video reach to its end then it should start again.I have manged it all in my adapter code given.. Now here comes the problem the issue I am facing is on scrolling the grid view the items of a grid view (a single item of grid view consists of a video view and a delete button ) sometimes swap with one another or sometimes a single video view gets repeated in grid view.. It also have another abnormal behavior that the last item of grid sometimes stick to bottom of screen .. I just don't get it whats happening here..

public class MyGalleryAdapterMain extends BaseAdapter {

        private Activity mActivity;
        private LayoutInflater mLayoutInflater;
        private ArrayList<File> mDatalist;

        public MyGalleryAdapterMain(Activity mActivity, ArrayList<File> mDatalist) {
            this.mActivity = mActivity;
            this.mDatalist = mDatalist;

            mLayoutInflater=(LayoutInflater)mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public int getCount() {
            return mDatalist.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }



        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View v=convertView;
            final File mData=mDatalist.get(position);
            final Holder mHolder;

            if(convertView==null){


                v=mLayoutInflater.inflate(R.layout.grid_item,null);
                mHolder =new Holder();

                mHolder.mDeleteButton=(Button)v.findViewById(R.id.deleteButton);

                mHolder.mVideoView=(VideoView)v.findViewById(R.id.videoImageView);
                mHolder.mVideoNameTextView=(TextView)v.findViewById(R.id.videoNameTextView);

                v.setTag(mHolder);

            }else

                mHolder=(Holder)v.getTag();

       /* Bitmap thumbnail = ThumbnailUtils.createVideoThumbnail(mData.getAbsolutePath(), MediaStore.Video.Thumbnails.MINI_KIND);
       mHolder.mImageView.setImageBitmap(thumbnail);*/

            if(mHolder.mVideoView.getCurrentPosition()==0) {
                mHolder.mVideoView.setVideoPath(mData.getAbsolutePath());
                final Holder mTempHolder=mHolder;

                mHolder.mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {

                        mTempHolder.mVideoView.start();


                    }
                });

                mHolder.mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {

                        mTempHolder.mVideoView.start();

                    }
                });
            }







            mHolder.mDeleteButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    mData.delete();
                    mDatalist.remove(mData);
                    notifyDataSetChanged();
                }
            });

            return v;
        }



        public class Holder{

            private VideoView mVideoView;
            private TextView mVideoNameTextView;
            private Button mDeleteButton;

        }


    }

Solution

  • I would think its because the recycled videoviews you are using arent being told to reset themselves when being reused so they just play the same video they were previously playing. You should do a stop or release (Im not looking at android docs right now) and then re-load the video view on reuse. I dont see you doing that in your code you posted.