我正在制作Android Media Player。我想制作一个自定义列表视图,其中包含一个具有视频文件缩略图的图像。缩略图图像应根据此列表视图中的文件名显示,并应从给定链接显示。
感谢您帮助我。
答案 0 :(得分:2)
为此目的使用自定义适配器会很聪明。然后重写getView方法以构建布局。这是我的一个使用ViewHolder设计模式并异步加载图像的项目的示例:
package com.suterastudio.aca.gui.adapters;
import java.util.List;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.suterastudio.aca.R;
import com.suterastudio.aca.data.Tweet;
import com.suterastudio.aca.data.UserProgramme;
import com.suterastudio.aca.gui.images.LinkToBitmap;
public class TweetsAdapter extends ArrayAdapter<UserProgramme> {
private static List<Tweet> Tweets;
public TweetsAdapter(Context context, int textViewResourceId,
List<Tweet> tweets) {
super(context, textViewResourceId);
Tweets = tweets;
}
@Override
public int getCount() {
return Tweets.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Tweet thisTweet = Tweets.get(position);
ViewHolder holder;
// Inflate the view if it is null
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_tweets_row, null);
holder = new ViewHolder();
holder.profilePic = (ImageView) convertView
.findViewById(R.id.img_picture);
holder.userName = (TextView) convertView
.findViewById(R.id.txt_name);
holder.text = (TextView) convertView.findViewById(R.id.txt_content);
holder.position = position;
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// Make sure we've got an actual event, here
if (thisTweet != null) {
// Additional null exception swaddling
if (holder.profilePic != null && holder.userName != null
&& holder.text != null) {
try {
if (thisTweet.profile_image_url != null) {
new ThumbnailTask(position, holder, thisTweet)
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, null);
}
if (thisTweet.from_user_name != null) {
holder.userName.setText(thisTweet.from_user_name);
}
if (thisTweet.text != null) {
holder.text.setText(thisTweet.text);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
return convertView;
}
private static class ThumbnailTask extends AsyncTask {
private int mPosition;
private ViewHolder mHolder;
private Tweet mTweet;
public ThumbnailTask(int position, ViewHolder holder, Tweet thisTweet) {
mPosition = position;
mHolder = holder;
mTweet = thisTweet;
}
@Override
protected void onPostExecute(Object result) {
if (mHolder.position == mPosition) {
mHolder.profilePic.setImageBitmap((Bitmap) result);
}
}
@Override
protected Object doInBackground(Object... params) {
LinkToBitmap linkToBitmap = new LinkToBitmap(
mTweet.profile_image_url);
Bitmap userIconBitmap = linkToBitmap.getRemoteImage();
return userIconBitmap;
}
}
private static class ViewHolder {
public TextView text;
public TextView userName;
public ImageView profilePic;
public int position;
}
}
然后,在您的父活动或片段中,只需附加适配器即可。像这样:
LvMenu = (ListView) mView.findViewById(R.id.listViewTweets);
TAdapter.notifyDataSetChanged();
LvMenu.setAdapter(TAdapter);
使用适配器不仅有利于提高性能;它简化了使用数据驱动视图的整个过程,特别是ListViews。
答案 1 :(得分:1)
您需要一个带自定义适配器的自定义列表视图。
ListView lv= (ListView) findViewById(R.id.lv);
CustomAdapter cus = new CustomAdapter(this);
lv.setAdapter(cus);
CustomAdapter类
class CustomAdapter extends BaseAdapter
{
public CustomAdapter(CustomListView customListView) {
super(customListView, 0);
// TODO Auto-generated constructor stub
this.mInflater = LayoutInflater.from(customListView);
c=customListView;
}
public int getCount() {
return 20; //listview item count
}
public Object getItem(int arg0) {
return arg0;
}
public long getItemId(int arg0) {
return arg0;
}
public View getView(final int arg0, View arg1, ViewGroup arg2) {
//Inflate a custom layout
return arg1;
}
}
工作示例@ http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/
除了自定义适配器,您还应使用ViewHolder进行平滑滚动和性能。
http://developer.android.com/training/improving-layouts/smooth-scrolling.html
以下是谷歌开发者关于listview的讨论