change the files listview to recyclerview

This commit is contained in:
tibbi
2016-11-20 16:55:46 +01:00
parent 9d9b987abf
commit 978b74adc1
7 changed files with 105 additions and 17 deletions

View File

@ -1,23 +1,59 @@
package com.simplemobiletools.filemanager.adapters
import android.content.Context
import android.content.res.Resources
import android.support.v7.widget.RecyclerView
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.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.simplemobiletools.filemanager.R
import com.simplemobiletools.filemanager.extensions.formatSize
import com.simplemobiletools.filepicker.extensions.formatSize
import com.simplemobiletools.filepicker.extensions.isGif
import com.simplemobiletools.filepicker.models.FileDirItem
import kotlinx.android.synthetic.main.list_item.view.*
import java.io.File
class ItemsAdapter(context: Context, private val mItems: List<FileDirItem>) : BaseAdapter() {
class ItemsAdapter(val context: Context, private val mItems: List<FileDirItem>, val itemClick: (FileDirItem) -> Unit) :
RecyclerView.Adapter<ItemsAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent?.context).inflate(R.layout.list_item, parent, false)
return ViewHolder(context, view, itemClick)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bindView(mItems[position])
}
override fun getItemCount() = mItems.size
class ViewHolder(val context: Context, view: View, val itemClick: (FileDirItem) -> (Unit)) : RecyclerView.ViewHolder(view) {
fun bindView(fileDirItem: FileDirItem) {
itemView.item_name.text = fileDirItem.name
if (fileDirItem.isDirectory) {
Glide.with(context).load(R.mipmap.directory).diskCacheStrategy(getCacheStrategy(fileDirItem)).centerCrop().crossFade().into(itemView.item_icon)
itemView.item_details.text = getChildrenCnt(fileDirItem)
} else {
Glide.with(context).load(fileDirItem.path).diskCacheStrategy(getCacheStrategy(fileDirItem)).error(R.mipmap.file).centerCrop().crossFade().into(itemView.item_icon)
itemView.item_details.text = fileDirItem.size.formatSize()
}
itemView.setOnClickListener { itemClick(fileDirItem) }
}
private fun getCacheStrategy(item: FileDirItem) = if (File(item.path).isGif()) DiskCacheStrategy.NONE else DiskCacheStrategy.RESULT
private fun getChildrenCnt(item: FileDirItem): String {
val children = item.children
return context.resources.getQuantityString(R.plurals.items, children, children)
}
}
}
/*class ItemsAdapter(context: Context, private val mItems: List<FileDirItem>) : BaseAdapter() {
private val mInflater: LayoutInflater
private val mRes: Resources
private val mContext: Context
@ -78,3 +114,4 @@ class ItemsAdapter(context: Context, private val mItems: List<FileDirItem>) : Ba
val details: TextView = view.item_details
}
}
*/

View File

@ -23,6 +23,7 @@ import com.simplemobiletools.filemanager.dialogs.RenameItemDialog
import com.simplemobiletools.filepicker.asynctasks.CopyMoveTask
import com.simplemobiletools.filepicker.extensions.*
import com.simplemobiletools.filepicker.models.FileDirItem
import com.simplemobiletools.filepicker.views.RecyclerViewDivider
import com.simplemobiletools.fileproperties.dialogs.PropertiesDialog
import kotlinx.android.synthetic.main.items_fragment.*
import java.io.File
@ -78,9 +79,11 @@ class ItemsFragment : android.support.v4.app.Fragment(), AdapterView.OnItemClick
mItems = newItems
val adapter = ItemsAdapter(context, mItems)
val adapter = ItemsAdapter(context, mItems) {
}
items_list.adapter = adapter
items_list.onItemClickListener = this
items_list.addItemDecoration(RecyclerViewDivider(context))
items_list.setOnTouchListener(this)
}
@ -315,17 +318,18 @@ class ItemsFragment : android.support.v4.app.Fragment(), AdapterView.OnItemClick
}
private fun getSelectedItemIndexes(): List<Int> {
val items = items_list.checkedItemPositions
/*val items = items_list.checkedItemPositions
val cnt = items.size()
val selectedItems = (0..cnt - 1)
.filter { items.valueAt(it) }
.map { items.keyAt(it) }
return selectedItems
return selectedItems*/
return ArrayList()
}
private fun prepareForDeleting() {
mToBeDeleted.clear()
val items = items_list.checkedItemPositions
/*val items = items_list.checkedItemPositions
val cnt = items.size()
var deletedCnt = 0
for (i in 0..cnt - 1) {
@ -337,7 +341,7 @@ class ItemsFragment : android.support.v4.app.Fragment(), AdapterView.OnItemClick
}
}
notifyDeletion(deletedCnt)
notifyDeletion(deletedCnt)*/
}
private fun notifyDeletion(cnt: Int) {

View File

@ -0,0 +1,33 @@
package com.simplemobiletools.filemanager.views
import android.content.Context
import android.graphics.Canvas
import android.graphics.drawable.Drawable
import android.support.v7.widget.RecyclerView
import com.simplemobiletools.filemanager.R
class RecyclerViewDivider(context: Context) : RecyclerView.ItemDecoration() {
private val mDivider: Drawable
init {
mDivider = context.resources.getDrawable(R.drawable.divider)
}
override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
val left = parent.paddingLeft
val right = parent.width - parent.paddingRight
val childCount = parent.childCount
for (i in 0..childCount - 1) {
val child = parent.getChildAt(i)
val params = child.layoutParams as RecyclerView.LayoutParams
val top = child.bottom + params.bottomMargin
val bottom = top + mDivider.intrinsicHeight
mDivider.setBounds(left, top, right, bottom)
mDivider.draw(c)
}
}
}