【发布时间】:2019-02-16 06:32:58
【问题描述】:
这是我的FragmentA
添加图像允许用户捕获图像并保存到手机外部存储中。 newList 是一个List,它从手机存储中检索所有图像并最终设置为适配器。
newList = retrieveCaptureImagePath()
myRecyclerView.setLayoutManager(
LinearLayoutManager(
getActivity(),
LinearLayoutManager.HORIZONTAL, false
)
)
myRecyclerView.setHasFixedSize(true);
myRecyclerView.setAdapter(ImageListAdapter(this, newList))
这是适配器类中的onBindViewHolder
override fun onBindViewHolder(holder: ViewHolder, p1: Int) {
context.longToast("position "+imgPics?.get(p1).toString())
val item = this!!.imgPics?.get(p1)
val bfOptions = BitmapFactory.Options()
bfOptions.inDither = false
bfOptions.inPurgeable =
true
bfOptions.inInputShareable =
true
bfOptions.inTempStorage = ByteArray(32 * 1024)
var fs: FileInputStream? = null
val file = File(imgPics?.get(p1).toString())
if (file.exists()) {
fs = FileInputStream(File(imgPics?.get(p1).toString()))
if (fs != null) {
var bm = BitmapFactory.decodeFileDescriptor(fs.fd, null, bfOptions)
holder.image?.setImageBitmap(bm)
}
}
with(holder.itemView) {
tag = item
}
}
在onBindViewHolder里面,我可以很方便的得到图片路径
context.longToast("position "+imgPics?.get(p1).toString())
但是如何获取片段中的tick action bar被点击时的位置呢?
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val id = item.itemId
if (id == R.id.save) {
for (i in newList?.size.toString()) {
val position = view?.tag
longToast(position.toString())
}
}
return super.onOptionsItemSelected(item)
}
我一直显示空值。
编辑
适配器类声明如下
class ImageListAdapter(
private val context: Fragment,
private val imgPics: MutableList<String>?,
private val mListener: ImageAdapterUIHandler?
) :
RecyclerView.Adapter<ImageListAdapter.ViewHolder>() {
private val mOnClickListener: View.OnClickListener
init {
mOnClickListener = View.OnClickListener { v ->
val item = v.tag as String
mListener?.onWorkRequestClicked(item)
}
}
override fun onCreateViewHolder(p0: ViewGroup, p1: Int): ViewHolder {
val view = LayoutInflater.from(p0.context)
.inflate(R.layout.grid_item, p0, false)
return ViewHolder(view)
}
override fun getItemCount(): Int{
return imgPics?.size!!
}
override fun onBindViewHolder(holder: ViewHolder, p1: Int) {
context.longToast("position "+imgPics?.get(p1).toString())
val item = this!!.imgPics?.get(p1)
val bfOptions = BitmapFactory.Options()
bfOptions.inDither = false //Disable Dithering mode
bfOptions.inPurgeable =
true //Tell to gc that whether it needs free memory, the Bitmap can be cleared
bfOptions.inInputShareable =
true //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
bfOptions.inTempStorage = ByteArray(32 * 1024)
var fs: FileInputStream? = null
val file = File(imgPics?.get(p1).toString())
if (file.exists()) {
fs = FileInputStream(File(imgPics?.get(p1).toString()))
if (fs != null) {
var bm = BitmapFactory.decodeFileDescriptor(fs.fd, null, bfOptions)
holder.image?.setImageBitmap(bm)
}
}
with(holder.itemView) {
tag = item
// setOnClickListener(mOnClickListener)
}
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val image: ImageView = itemView.imageView2
}
interface ImageAdapterUIHandler{
fun onWorkRequestClicked(pos: String)
}
}
片段A
class FragmentA : BaseFragment(true),ImageListAdapter.ImageAdapterUIHandler {
override fun onWorkRequestClicked(pos: String) {
longToast(pos)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_create_work_request, container, false)
return view
}
@SuppressLint("ResourceAsColor")
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
activity.hideBottomNavigation()
........
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.create_request, menu)
super.onCreateOptionsMenu(menu, inflater)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val id = item.itemId
if (id == R.id.save) {
for (i in newList?.size.toString()) {
// get the position from adapter
}
}
return super.onOptionsItemSelected(item)
}
.....
}
【问题讨论】:
-
是否要获取所选图像在片段中的位置?
-
@primo ya......
-
检查我的答案。它可以帮助你
标签: android kotlin android-recyclerview