向下滚动后,ListView 不显示实际的视频文件
Posted
技术标签:
【中文标题】向下滚动后,ListView 不显示实际的视频文件【英文标题】:ListView doesn't show the actual video files after scroll down 【发布时间】:2016-12-01 13:00:36 【问题描述】:在我的应用程序中,我在 ListView 中显示 SD 卡视频。 ListView 显示视频缩略图、标题、大小、持续时间。当我向下滚动时,它会显示相同的视频和缩略图,但如果我单击一个项目,它会向另一个活动显示不同的视频文件。我只想在 ListView 中显示实际视频是向下还是向上滚动。
这是我的代码。
公共类 LoadVideo 扩展 AppCompatActivity
private Cursor videoCursor;
private int video_column_index;
ListView videoList;
int count;
String[] thumbColumns = MediaStore.Video.Thumbnails.DATA,
MediaStore.Video.Thumbnails.VIDEO_ID;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.load_video_activity);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
init_phone_video_grid();
private void init_phone_video_grid()
String[] proj = MediaStore.Video.Media._ID,
MediaStore.Video.Media.DATA,
MediaStore.Video.Media.DISPLAY_NAME,
MediaStore.Video.Media.SIZE,
MediaStore.Video.Media.DURATION;
videoCursor = getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, proj, null, null, null);
count = videoCursor.getCount();
videoList = (ListView) findViewById(R.id.videoList);
videoList.setAdapter(new VideoAdapter(this.getApplicationContext()));
videoList.setOnItemClickListener(videoGridListener);
private AdapterView.OnItemClickListener videoGridListener = new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
video_column_index = videoCursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
videoCursor.moveToPosition(i);
String fileName = videoCursor.getString(video_column_index);
Intent intent = new Intent(LoadVideo.this, ShowVideo.class);
intent.putExtra("videofilename", fileName);
startActivity(intent);
;
public class VideoAdapter extends BaseAdapter
Context context;
VideoAdapter(Context context)
this.context = context;
@Override
public int getCount()
return count;
@Override
public Object getItem(int i)
return i;
@Override
public long getItemId(int i)
return i;
@Override
public View getView(int i, View view, ViewGroup viewGroup)
ViewHolder viewHolder;
String id;
if (view == null)
viewHolder = new ViewHolder();
view = LayoutInflater.from(context).inflate(R.layout.list_items, viewGroup, false);
viewHolder.titleName = (TextView) view.findViewById(R.id.title);
viewHolder.duration = (TextView) view.findViewById(R.id.duration);
viewHolder.size = (TextView) view.findViewById(R.id.size);
viewHolder.thumbImage = (ImageView) view.findViewById(R.id.imageIcon);
//Title
video_column_index = videoCursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME);
videoCursor.moveToPosition(i);
id = videoCursor.getString(video_column_index);
viewHolder.titleName.setText(id);
//Size
video_column_index = videoCursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE);
videoCursor.moveToPosition(i);
String vSize;
long videoSize = videoCursor.getLong(video_column_index);
double actualSizeInMB = videoSize/(1024.0 * 1024.0);
DecimalFormat dec = new DecimalFormat("0.00");
if (actualSizeInMB > 1)
vSize = dec.format(actualSizeInMB).concat(" MB");
viewHolder.size.setText(vSize);
else
vSize = dec.format(actualSizeInMB).concat(" KB");
viewHolder.size.setText(vSize);
//Duration
video_column_index = videoCursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION);
videoCursor.moveToPosition(i);
long time = videoCursor.getLong(video_column_index);
long seconds = time/1000;
long minutes = seconds/60;
seconds = seconds % 60;
String setTime ;
if (seconds < 10)
setTime = String.valueOf(minutes) + ":0" + String.valueOf(seconds);
viewHolder.duration.setText(setTime.concat( " | "));
else
setTime = String.valueOf(minutes) + ":" + String.valueOf(seconds);
viewHolder.duration.setText(setTime.concat( " | "));
//String[] proj = MediaStore.Video.Media._ID, MediaStore.Video.Media.DISPLAY_NAME, MediaStore.Video.Media.DATA ;
//Cursor cursor = getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, proj,
//MediaStore.Video.Media.DISPLAY_NAME + "=?", new String[] id , null);
//cursor.moveToFirst();
//long ids = cursor.getLong(cursor.getColumnIndex(MediaStore.Video.Media._ID));
long ids = videoCursor.getInt(videoCursor.getColumnIndex(MediaStore.Video.Media._ID));
ContentResolver crThumb = getContentResolver();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
Bitmap curThumb = MediaStore.Video.Thumbnails.getThumbnail(crThumb, ids, MediaStore.Video.Thumbnails.MICRO_KIND, options);
viewHolder.thumbImage.setImageBitmap(curThumb);
curThumb = null;
//video_column_index = videoCursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
//videoCursor.moveToPosition(i);
//String path = videoCursor.getString(video_column_index);
//Bitmap thumbnail = ThumbnailUtils.createVideoThumbnail(path, MediaStore.Video.Thumbnails.MICRO_KIND);
//viewHolder.thumbImage.setImageBitmap(thumbnail);
//videoThumbnailCursor.close();
return view;
private class ViewHolder
TextView titleName;
TextView size;
TextView duration;
ImageView thumbImage;
【问题讨论】:
您是否还看到重复的缩略图/数据? 前 6 或 7 项是实际的,然后一切都和以前一样。你能告诉我为什么会这样吗? @哈恩 问题是您仅在视图为空时设置所有数据/图像。当你滚动时,已经创建的视图会被回收,所以在这些情况下,视图不为空,但在上面的代码中,如果视图不为空,你什么也没做。我已经发布了答案,如果清楚请告诉我 是的,它奏效了。谢谢@Hahn 【参考方案1】:从这里开始:if(view == null)
,修改你的代码如下:
if (view == null)
viewHolder = new ViewHolder();
view = LayoutInflater.from(context).inflate(R.layout.list_items, viewGroup, false);
viewHolder.titleName = (TextView) view.findViewById(R.id.title);
viewHolder.duration = (TextView) view.findViewById(R.id.duration);
viewHolder.size = (TextView) view.findViewById(R.id.size);
viewHolder.thumbImage = (ImageView) view.findViewById(R.id.imageIcon);
view.setTag(viewHolder);
else
viewHolder = (ViewHolder)(view.getTag());
//Title.....and so on...
【讨论】:
以上是关于向下滚动后,ListView 不显示实际的视频文件的主要内容,如果未能解决你的问题,请参考以下文章
Listview setOnScrollListener 向上向下滚动
点击按钮后如何将ListView向下滚动到底部(例如使用scrollToIndex)? NativeScript-Vue
内部滚动视图( ScrollView / RecyclerView / ListView )视频未播放。