0

我正在制作一个 Android 媒体播放器。我想制作一个自定义列表视图,它的图像具有视频文件的缩略图。缩略图应根据此列表视图中的文件名显示,并应从给定链接显示。

感谢你们对我的帮助。

4

2 回答 2

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 的整个过程。

于 2013-05-01T11:15:40.340 回答
1

您将需要一个带有自定义适配器的自定义列表视图。

    ListView lv= (ListView) findViewById(R.id.lv);
    CustomAdapter cus = new CustomAdapter(this);
    lv.setAdapter(cus);

自定义适配器类

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

这是 google 开发者关于 listview 的演讲

http://www.youtube.com/watch?v=wDBM6wVEO70&noredirect=1

于 2013-05-01T11:17:04.707 回答