convert mediaadapter to kotlin
This commit is contained in:
parent
d60521411e
commit
def05ffbe2
2 changed files with 72 additions and 91 deletions
|
@ -1,91 +0,0 @@
|
|||
package com.simplemobiletools.gallery.adapters;
|
||||
|
||||
import android.content.Context;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.BaseAdapter;
|
||||
import android.widget.ImageView;
|
||||
|
||||
import com.bumptech.glide.Glide;
|
||||
import com.bumptech.glide.load.engine.DiskCacheStrategy;
|
||||
import com.bumptech.glide.signature.StringSignature;
|
||||
import com.simplemobiletools.gallery.R;
|
||||
import com.simplemobiletools.gallery.models.Medium;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
|
||||
public class MediaAdapter extends BaseAdapter {
|
||||
private final Context mContext;
|
||||
private final List<Medium> mMedia;
|
||||
private final LayoutInflater mInflater;
|
||||
|
||||
public MediaAdapter(Context context, List<Medium> media) {
|
||||
mContext = context;
|
||||
mMedia = media;
|
||||
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public View getView(int position, View convertView, ViewGroup parent) {
|
||||
final Medium medium = mMedia.get(position);
|
||||
ViewHolder viewHolder;
|
||||
if (convertView == null) {
|
||||
convertView = mInflater.inflate(R.layout.photo_video_item, parent, false);
|
||||
viewHolder = new ViewHolder(convertView);
|
||||
convertView.setTag(viewHolder);
|
||||
} else {
|
||||
viewHolder = (ViewHolder) convertView.getTag();
|
||||
}
|
||||
|
||||
if (medium.getIsVideo()) {
|
||||
viewHolder.playOutline.setVisibility(View.VISIBLE);
|
||||
} else {
|
||||
viewHolder.playOutline.setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
final String path = medium.getPath();
|
||||
final StringSignature timestampSignature = new StringSignature(String.valueOf(medium.getTimestamp()));
|
||||
if (medium.isGif()) {
|
||||
Glide.with(mContext).load(path).asGif().diskCacheStrategy(DiskCacheStrategy.NONE).signature(timestampSignature).into(viewHolder.photoThumbnail);
|
||||
} else {
|
||||
Glide.with(mContext).load(path).diskCacheStrategy(DiskCacheStrategy.NONE).signature(timestampSignature)
|
||||
.placeholder(R.color.tmb_background).centerCrop().crossFade().into(viewHolder.photoThumbnail);
|
||||
}
|
||||
|
||||
return convertView;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount() {
|
||||
return mMedia.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getItem(int position) {
|
||||
return mMedia.get(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getItemId(int position) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public void updateItems(List<Medium> newPhotos) {
|
||||
mMedia.clear();
|
||||
mMedia.addAll(newPhotos);
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
|
||||
static class ViewHolder {
|
||||
@BindView(R.id.medium_thumbnail) ImageView photoThumbnail;
|
||||
@BindView(R.id.play_outline) View playOutline;
|
||||
|
||||
public ViewHolder(View view) {
|
||||
ButterKnife.bind(this, view);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,72 @@
|
|||
package com.simplemobiletools.gallery.adapters
|
||||
|
||||
import android.content.Context
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.BaseAdapter
|
||||
import android.widget.ImageView
|
||||
import com.bumptech.glide.Glide
|
||||
import com.bumptech.glide.load.engine.DiskCacheStrategy
|
||||
import com.bumptech.glide.signature.StringSignature
|
||||
import com.simplemobiletools.gallery.R
|
||||
import com.simplemobiletools.gallery.models.Medium
|
||||
import kotlinx.android.synthetic.main.photo_video_item.view.*
|
||||
import kotlinx.android.synthetic.main.photo_video_tmb.view.*
|
||||
|
||||
class MediaAdapter(private val mContext: Context, private val mMedia: MutableList<Medium>) : BaseAdapter() {
|
||||
private val mInflater: LayoutInflater
|
||||
|
||||
init {
|
||||
mInflater = mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
|
||||
}
|
||||
|
||||
override fun getView(position: Int, view: View?, parent: ViewGroup): View {
|
||||
var convertView = view
|
||||
val medium = mMedia[position]
|
||||
val viewHolder: ViewHolder
|
||||
if (convertView == null) {
|
||||
convertView = mInflater.inflate(R.layout.photo_video_item, parent, false)
|
||||
viewHolder = ViewHolder(convertView)
|
||||
convertView!!.tag = viewHolder
|
||||
} else {
|
||||
viewHolder = convertView.tag as ViewHolder
|
||||
}
|
||||
|
||||
viewHolder.playOutline.visibility = if (medium.isVideo) View.VISIBLE else View.GONE
|
||||
|
||||
val path = medium.path
|
||||
val timestampSignature = StringSignature(medium.timestamp.toString())
|
||||
if (medium.isGif) {
|
||||
Glide.with(mContext).load(path).asGif().diskCacheStrategy(DiskCacheStrategy.NONE).signature(timestampSignature).into(viewHolder.photoThumbnail)
|
||||
} else {
|
||||
Glide.with(mContext).load(path).diskCacheStrategy(DiskCacheStrategy.NONE).signature(timestampSignature)
|
||||
.placeholder(R.color.tmb_background).centerCrop().crossFade().into(viewHolder.photoThumbnail)
|
||||
}
|
||||
|
||||
return convertView
|
||||
}
|
||||
|
||||
override fun getCount(): Int {
|
||||
return mMedia.size
|
||||
}
|
||||
|
||||
override fun getItem(position: Int): Any {
|
||||
return mMedia[position]
|
||||
}
|
||||
|
||||
override fun getItemId(position: Int): Long {
|
||||
return 0
|
||||
}
|
||||
|
||||
fun updateItems(newPhotos: List<Medium>) {
|
||||
mMedia.clear()
|
||||
mMedia.addAll(newPhotos)
|
||||
notifyDataSetChanged()
|
||||
}
|
||||
|
||||
internal class ViewHolder(view: View) {
|
||||
val photoThumbnail: ImageView = view.medium_thumbnail
|
||||
val playOutline: View = view.play_outline
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue