美文网首页
ListView Adapter 的简单使用

ListView Adapter 的简单使用

作者: 华人有为 | 来源:发表于2020-07-10 20:58 被阅读0次

    前言:ListView 是一个“古老的”列表视图实现方案,推荐大家使用RecyclerView 这篇短文适合那些初学安卓的开发者

    package you package name;
    
    import android.content.Context;
    import android.text.format.Formatter;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    import com.example.mobileplayer.R;
    import com.bobo.mobileplayer.domain.MediaItem;
    import com.bobo.mobileplayer.utils.Utils;
    
    import java.util.ArrayList;
    
    /**
     * Created by Leon on 2015/1/7.
     * Functions: VideoPager的适配器
     */
    public class VideoPagerAdapter extends BaseAdapter {
    
        private final boolean isVideo;
        private Context context;
        private final ArrayList<MediaItem> mediaItems;
      
    
       /**
         * 通常我们通过构造参数把数据源传递过来
         */
        public VideoPagerAdapter(Context context, ArrayList<MediaItem> mediaItems,boolean isVideo) {
                this.context = context;
                this.mediaItems = mediaItems;
                this.isVideo = isVideo;
        }
    
        @Override
        public int getCount() {
            return mediaItems.size();
        }
    
        @Override
        public Object getItem(int i) {
            return null;
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHoder viewHoder;
            if (convertView == null){
                convertView = View.inflate(context, 你的item布局文件, null);
                viewHoder = new ViewHoder();
                viewHoder.iv_icon = (ImageView)convertView.findViewById(R.id.iv_icon);
                viewHoder.tv_name = (TextView)convertView.findViewById(R.id.tv_name);
                viewHoder.tv_time = (TextView)convertView.findViewById(R.id.tv_time);
                viewHoder.tv_size = (TextView)convertView.findViewById(R.id.tv_size);
    
                convertView.setTag(viewHoder);
            }else {
                viewHoder = (ViewHoder)convertView.getTag();
            }
            //根据position得到列表中对应位置的数据
            MediaItem mediaItem = mediaItems.get(position);
            viewHoder.tv_name.setText(mediaItem.getName());
            viewHoder.tv_size.setText(Formatter.formatFileSize(context,mediaItem.getSize()));
            viewHoder.tv_time.setText(mediaItem.getDuration()));
    
            return convertView;
        }
    
        static class ViewHoder{
            ImageView iv_icon;
            TextView tv_name;
            TextView tv_time;
            TextView tv_size;
        }
    }
    

    相关文章

      网友评论

          本文标题:ListView Adapter 的简单使用

          本文链接:https://www.haomeiwen.com/subject/fzkvcktx.html