我有一个简单的任务。在recyclerView中,当我点击任何按钮时,我都想启动相机,拍照,然后拍摄这张照片。然而,我找不到任何解决方案。我在RecyclerAdapter.kt:中尝试的内容
inner class ViewHolder(itemView: View):RecyclerView.ViewHolder(itemView) {
var textView1: TextView = itemView.findViewById(R.id.firma_textView1)
init {
textView1.setOnClickListener {
capturePhoto(context, activity)
}
}
}
fun capturePhoto(context: Context, activity: Activity) {
if (getCameraPermission(context)) {
val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivityForResult(activity, cameraIntent, FirstFragment.CAMERA_REQUEST_CODE, null)
} else {
ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.CAMERA), FirstFragment.CAMERA_REQUEST_CODE)
}
}
private fun getCameraPermission (context: Context):Boolean {
return ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED
}
有了这个代码,我可以启动相机,拍照,但在RecyclerAdapter中,没有办法捕捉拍摄的图像。
如何在Fragment中捕获图像的正常方式是:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
var buttonCapturePhoto = view.findViewById<Button>(R.id.button)
buttonCapturePhoto.setOnClickListener {
capturePhoto()
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == CAMERA_REQUEST_CODE) {
print("photo captured")
}
}
private fun capturePhoto() {
if (getCameraPermission(requireContext())) {
val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE)
} else {
ActivityCompat.requestPermissions(requireActivity(), arrayOf(Manifest.permission.CAMERA), CAMERA_REQUEST_CODE)
}
}
private fun getCameraPermission (context: Context):Boolean {
return ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED
}
我还在安卓开发者页面上找到了这篇文章-https://developer.android.com/training/basics/intents/result
他们建议创建类MyLifecycleObserver并在Fragment中使用它,但我无法在RecycleAdapter 中使用此代码
lateinit var observer : MyLifecycleObserver
override fun onCreate(savedInstanceState: Bundle?) {
// ...
observer = MyLifecycleObserver(requireActivity().activityResultRegistry)
lifecycle.addObserver(observer)
}
我在activityResultRegistry
和lifecycle
上出现错误
我还为测试这个git存储库创建了:https://github.com/Katzzer/recyclerViewPhotoCaptureKotlinAndroid
您应该将您的点击事件传递给碎片/活动
- 在适配器中创建自定义
interface
- 将其传递到适配器构造函数
// pass listener into constructor
class RecyclerAdapter(
val list:List<YourItemClass>,
val listener: OnItemClickListener) : ... {
// create a custom listener
interface OnItemClickListener{
fun onItemClick(view:View, position:Int)
}
inner class ViewHolder(itemView: View):RecyclerView.ViewHolder(itemView) {
// create function bind instead of using init block
fun bind(item:YourItemClass){
val textView1: TextView = itemView.findViewById(R.id.firma_textView1)
// if you want to change image in your ImageView , you could also pass
// your ImageView too
val imgView: ImageView = itemView.findViewById(R.id.imgView)
textView1.setOnClickListener { view ->
// this is just an example , but you get the idea
// listen click event and pass view and position
listener.onItemClick(view, adapterPosition)
// or
listener.onItemClick(imgView, adapterPosition)
}
}
}
...
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = list[position]
// bind here
holder.bind(item)
}
...
}
- 已在
Fragment/Activity
中初始化适配器 - 收听
Fragment/Activity
中的点击事件
...
adapter = RecyclerAdapter(list, object : RecyclerAdapter.OnItemClickListener{
override fun onItemClick(view:View, position:Int){
// Listen your click event here
capturePhoto().also { result ->
// do something
// dont forget to call notifyItem if you want to update an item in
// RecyclerView
adapter.notifyItemChanged(position)
}
}
}
recyclerView.adapter = adapter
...
- 在
FragmentActivity
中创建capturePhoto
函数