convert ItemsAdapter to kotlin

This commit is contained in:
tibbi 2016-09-08 22:57:07 +02:00
parent bb371a3cc9
commit ccb2f00014
2 changed files with 78 additions and 98 deletions

View File

@ -1,98 +0,0 @@
package com.simplemobiletools.filemanager.adapters;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.simplemobiletools.filemanager.R;
import com.simplemobiletools.filemanager.Utils;
import com.simplemobiletools.filemanager.models.FileDirItem;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
public class ItemsAdapter extends BaseAdapter {
private final List<FileDirItem> mItems;
private final LayoutInflater mInflater;
private final Bitmap mFileBmp;
private final Bitmap mDirectoryBmp;
private final Resources mRes;
public ItemsAdapter(Context context, List<FileDirItem> items) {
mItems = items;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mRes = context.getResources();
mDirectoryBmp = Utils.getColoredIcon(mRes, R.color.lightGrey, R.mipmap.directory);
mFileBmp = Utils.getColoredIcon(mRes, R.color.lightGrey, R.mipmap.file);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
final FileDirItem item = mItems.get(position);
viewHolder.name.setText(item.getName());
if (item.isDirectory()) {
viewHolder.icon.setImageBitmap(mDirectoryBmp);
viewHolder.details.setText(getChildrenCnt(item));
} else {
viewHolder.icon.setImageBitmap(mFileBmp);
viewHolder.details.setText(Utils.getFormattedSize(item));
}
return convertView;
}
private String getChildrenCnt(FileDirItem item) {
final int children = item.getChildren();
return mRes.getQuantityString(R.plurals.items, children, children);
}
@Override
public int getCount() {
return mItems.size();
}
@Override
public Object getItem(int position) {
return mItems.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
public void updateItems(List<FileDirItem> newItems) {
mItems.clear();
mItems.addAll(newItems);
notifyDataSetChanged();
}
static class ViewHolder {
@BindView(R.id.item_name) TextView name;
@BindView(R.id.item_icon) ImageView icon;
@BindView(R.id.item_details) TextView details;
public ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
}

View File

@ -0,0 +1,78 @@
package com.simplemobiletools.filemanager.adapters
import android.content.Context
import android.content.res.Resources
import android.graphics.Bitmap
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.TextView
import com.simplemobiletools.filemanager.R
import com.simplemobiletools.filemanager.Utils
import com.simplemobiletools.filemanager.models.FileDirItem
import kotlinx.android.synthetic.main.list_item.view.*
class ItemsAdapter(context: Context, private val mItems: List<FileDirItem>) : BaseAdapter() {
private val mInflater: LayoutInflater
private val mFileBmp: Bitmap
private val mDirectoryBmp: Bitmap
private val mRes: Resources
init {
mInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
mRes = context.resources
mDirectoryBmp = Utils.getColoredIcon(mRes, R.color.lightGrey, R.mipmap.directory)
mFileBmp = Utils.getColoredIcon(mRes, R.color.lightGrey, R.mipmap.file)
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
var view = convertView
val viewHolder: ViewHolder
if (view == null) {
view = mInflater.inflate(R.layout.list_item, parent, false)
viewHolder = ViewHolder(view)
view!!.tag = viewHolder
} else {
viewHolder = view.tag as ViewHolder
}
val item = mItems[position]
viewHolder.name.text = item.name
if (item.isDirectory) {
viewHolder.icon.setImageBitmap(mDirectoryBmp)
viewHolder.details.text = getChildrenCnt(item)
} else {
viewHolder.icon.setImageBitmap(mFileBmp)
viewHolder.details.text = Utils.getFormattedSize(item)
}
return view
}
private fun getChildrenCnt(item: FileDirItem): String {
val children = item.children
return mRes.getQuantityString(R.plurals.items, children, children)
}
override fun getCount(): Int {
return mItems.size
}
override fun getItem(position: Int): Any {
return mItems[position]
}
override fun getItemId(position: Int): Long {
return 0
}
internal class ViewHolder(view: View) {
val name: TextView = view.item_name
val icon: ImageView = view.item_icon
val details: TextView = view.item_details
}
}