|  | @@ -9,9 +9,10 @@ import android.view.ViewGroup
 | 
	
		
			
				|  |  |  import android.widget.ImageView
 | 
	
		
			
				|  |  |  import androidx.recyclerview.widget.RecyclerView
 | 
	
		
			
				|  |  |  import androidx.viewpager2.widget.ViewPager2
 | 
	
		
			
				|  |  | +import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
 | 
	
		
			
				|  |  | +import com.cr.common.CrFileManager
 | 
	
		
			
				|  |  |  import com.cr.cruav.R
 | 
	
		
			
				|  |  |  import com.cr.data.CrUtil
 | 
	
		
			
				|  |  | -import okhttp3.internal.notify
 | 
	
		
			
				|  |  |  import java.io.FileInputStream
 | 
	
		
			
				|  |  |  import java.io.FileNotFoundException
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -21,14 +22,20 @@ import java.io.FileNotFoundException
 | 
	
		
			
				|  |  |   * 创建日期:2023/6/16 16:19
 | 
	
		
			
				|  |  |   * 描述:图片浏览器
 | 
	
		
			
				|  |  |   */
 | 
	
		
			
				|  |  | -class CrImageBrowserWidget@JvmOverloads constructor(
 | 
	
		
			
				|  |  | +class CrImageBrowserWidget @JvmOverloads constructor(
 | 
	
		
			
				|  |  |      context: Context,
 | 
	
		
			
				|  |  | -    attrs: AttributeSet?=null,
 | 
	
		
			
				|  |  | +    attrs: AttributeSet? = null,
 | 
	
		
			
				|  |  |      defStyleAttr: Int = 0
 | 
	
		
			
				|  |  | -) :CrConstraintLayoutWidget(context,attrs,defStyleAttr){
 | 
	
		
			
				|  |  | -    private var viewPager:ViewPager2?= null // define: 2023/6/16 页面滑动组件
 | 
	
		
			
				|  |  | -    private var dataList:MutableList<String> = mutableListOf() // define: 2023/6/16 数据集合
 | 
	
		
			
				|  |  | -    private var adapter:BaseAdapter?= null // define: 2023/6/16 适配器
 | 
	
		
			
				|  |  | +) : CrConstraintLayoutWidget(context, attrs, defStyleAttr) {
 | 
	
		
			
				|  |  | +    private var viewPager: ViewPager2? = null // define: 2023/6/16 页面滑动组件
 | 
	
		
			
				|  |  | +    private var dataList: MutableList<String> = mutableListOf() // define: 2023/6/16 数据集合
 | 
	
		
			
				|  |  | +    private var adapter: BaseAdapter? = null // define: 2023/6/16 适配器
 | 
	
		
			
				|  |  | +    private var listener:iChangeListener?=null // define: 2023/6/17 对外接口
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    // todo: 2023/6/17 对外接口
 | 
	
		
			
				|  |  | +    interface iChangeListener{
 | 
	
		
			
				|  |  | +        fun onChange(file:ImageFileModel?)
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      /**
 | 
	
		
			
				|  |  |       * 初始化
 | 
	
	
		
			
				|  | @@ -48,6 +55,21 @@ class CrImageBrowserWidget@JvmOverloads constructor(
 | 
	
		
			
				|  |  |          // todo: 2023/6/16 设置适配器
 | 
	
		
			
				|  |  |          adapter = BaseAdapter(dataList)
 | 
	
		
			
				|  |  |          viewPager?.adapter = adapter
 | 
	
		
			
				|  |  | +        // todo: 2023/6/17 设置监听
 | 
	
		
			
				|  |  | +//        viewPager?.registerOnPageChangeCallback(pageChangeListener)
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 页面滑动监听
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    private var pageChangeListener = object:OnPageChangeCallback(){
 | 
	
		
			
				|  |  | +        // todo: 2023/6/17 选择页面
 | 
	
		
			
				|  |  | +        override fun onPageSelected(position: Int) {
 | 
	
		
			
				|  |  | +            var fileModel = adapter?.crGetSelectItem(position)
 | 
	
		
			
				|  |  | +            if(fileModel != null){
 | 
	
		
			
				|  |  | +                if(listener != null) listener?.onChange(fileModel)
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      /**
 | 
	
	
		
			
				|  | @@ -55,15 +77,17 @@ class CrImageBrowserWidget@JvmOverloads constructor(
 | 
	
		
			
				|  |  |       * @property dataList List<String>?
 | 
	
		
			
				|  |  |       * @constructor
 | 
	
		
			
				|  |  |       */
 | 
	
		
			
				|  |  | -    internal class BaseAdapter(dataList:List<String>):RecyclerView.Adapter<BaseAdapter.BaseViewHolder?>(){
 | 
	
		
			
				|  |  | -        var dataList:List<String>? = null // define: 2023/6/16 数据集合
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | +    internal class BaseAdapter(dataList: MutableList<String>) :
 | 
	
		
			
				|  |  | +        RecyclerView.Adapter<BaseAdapter.BaseViewHolder?>() {
 | 
	
		
			
				|  |  | +        private var dataList: MutableList<String>? = mutableListOf() // define: 2023/6/16 数据集合
 | 
	
		
			
				|  |  | +        private var fileList:MutableList<ImageFileModel>?= mutableListOf()  // define: 2023/6/17 文件集合
 | 
	
		
			
				|  |  |          // todo: 2023/6/16 创建视图
 | 
	
		
			
				|  |  |          override fun onCreateViewHolder(
 | 
	
		
			
				|  |  |              parent: ViewGroup,
 | 
	
		
			
				|  |  |              viewType: Int
 | 
	
		
			
				|  |  |          ): BaseAdapter.BaseViewHolder {
 | 
	
		
			
				|  |  | -            var itemView:View = LayoutInflater.from(parent.context).inflate(R.layout.item_image,parent,false)
 | 
	
		
			
				|  |  | +            var itemView: View =
 | 
	
		
			
				|  |  | +                LayoutInflater.from(parent.context).inflate(R.layout.item_image, parent, false)
 | 
	
		
			
				|  |  |              return BaseViewHolder(itemView)
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -74,7 +98,7 @@ class CrImageBrowserWidget@JvmOverloads constructor(
 | 
	
		
			
				|  |  |                  var fis = FileInputStream(imagePath)
 | 
	
		
			
				|  |  |                  var bitmap = BitmapFactory.decodeStream(fis)
 | 
	
		
			
				|  |  |                  holder.imageView?.setImageBitmap(bitmap)
 | 
	
		
			
				|  |  | -            }catch (ex:FileNotFoundException){
 | 
	
		
			
				|  |  | +            } catch (ex: FileNotFoundException) {
 | 
	
		
			
				|  |  |                  CrUtil.print("加载图片错误:" + ex.message)
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |          }
 | 
	
	
		
			
				|  | @@ -84,8 +108,8 @@ class CrImageBrowserWidget@JvmOverloads constructor(
 | 
	
		
			
				|  |  |              return dataList!!.size
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        inner class BaseViewHolder(itemView: View):RecyclerView.ViewHolder(itemView){
 | 
	
		
			
				|  |  | -            var imageView:ImageView?= null  // define: 2023/6/16 图片展示控件
 | 
	
		
			
				|  |  | +        inner class BaseViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
 | 
	
		
			
				|  |  | +            var imageView: ImageView? = null  // define: 2023/6/16 图片展示控件
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |              /**
 | 
	
		
			
				|  |  |               * 初始化
 | 
	
	
		
			
				|  | @@ -100,24 +124,82 @@ class CrImageBrowserWidget@JvmOverloads constructor(
 | 
	
		
			
				|  |  |           */
 | 
	
		
			
				|  |  |          init {
 | 
	
		
			
				|  |  |              this.dataList = dataList
 | 
	
		
			
				|  |  | +            initFileList()
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        /**
 | 
	
		
			
				|  |  | +         * 文件初始化
 | 
	
		
			
				|  |  | +         */
 | 
	
		
			
				|  |  | +        private fun initFileList(){
 | 
	
		
			
				|  |  | +            this.fileList?.let {
 | 
	
		
			
				|  |  | +                it.clear()
 | 
	
		
			
				|  |  | +                for(filePath in this.dataList!!){
 | 
	
		
			
				|  |  | +                    var model = ImageFileModel(filePath)
 | 
	
		
			
				|  |  | +                    it.add(model)
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          /**
 | 
	
		
			
				|  |  |           * 更新数据
 | 
	
		
			
				|  |  |           * @param dataList List<String>
 | 
	
		
			
				|  |  |           */
 | 
	
		
			
				|  |  | -        fun update(dataList: List<String>){
 | 
	
		
			
				|  |  | +        fun crUpdate(dataList: MutableList<String>) {
 | 
	
		
			
				|  |  |              this.dataList = dataList
 | 
	
		
			
				|  |  | +            initFileList()
 | 
	
		
			
				|  |  |              notifyDataSetChanged()
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        /**
 | 
	
		
			
				|  |  | +         * 返回当前位置的文件模型
 | 
	
		
			
				|  |  | +         * @param position Int 位置
 | 
	
		
			
				|  |  | +         * @return ImageFileModel? 信息
 | 
	
		
			
				|  |  | +         */
 | 
	
		
			
				|  |  | +        fun crGetSelectItem(position:Int):ImageFileModel?{
 | 
	
		
			
				|  |  | +            return this.fileList?.get(position)
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    // todo: 2023/6/17 照片模型
 | 
	
		
			
				|  |  | +    class ImageFileModel {
 | 
	
		
			
				|  |  | +        var fileName: String? = null // define: 2023/6/17 图片名称
 | 
	
		
			
				|  |  | +        var fileSize: Long? = 0 // define: 2023/6/17 图片大小
 | 
	
		
			
				|  |  | +        var fileSizeDescription: String? = null // define: 2023/6/17 图片大小描述
 | 
	
		
			
				|  |  | +        var fileFullPath: String? = null  // define: 2023/6/17 文件路径
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        /**
 | 
	
		
			
				|  |  | +         * 初始化
 | 
	
		
			
				|  |  | +         * @param filePth String 文件路径
 | 
	
		
			
				|  |  | +         * @constructor
 | 
	
		
			
				|  |  | +         */
 | 
	
		
			
				|  |  | +        constructor(filePth: String) {
 | 
	
		
			
				|  |  | +            this.fileFullPath = filePth
 | 
	
		
			
				|  |  | +            this.fileName = CrFileManager.getFileName(filePth)
 | 
	
		
			
				|  |  | +            this.fileSize = CrFileManager.getFileSize(filePth)
 | 
	
		
			
				|  |  | +            this.fileSizeDescription = CrFileManager.fileSizeToString(this.fileSize!!)
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      /**
 | 
	
		
			
				|  |  |       * 添加图片
 | 
	
		
			
				|  |  |       * @param imagePath String 图片路径
 | 
	
		
			
				|  |  |       */
 | 
	
		
			
				|  |  | -    fun crAppendImage(imagePath:String){
 | 
	
		
			
				|  |  | +    fun crAppendImage(imagePath: String) {
 | 
	
		
			
				|  |  |          this.dataList.add(imagePath)
 | 
	
		
			
				|  |  | -        adapter?.update(this.dataList)
 | 
	
		
			
				|  |  | +        adapter?.crUpdate(this.dataList)
 | 
	
		
			
				|  |  | +        // todo: 2023/6/17 追加了一条数据后注册监听
 | 
	
		
			
				|  |  | +        if(this.dataList.size == 1){
 | 
	
		
			
				|  |  | +            viewPager?.registerOnPageChangeCallback(pageChangeListener)
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        viewPager?.currentItem = this.dataList.size-1
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 设置监听
 | 
	
		
			
				|  |  | +     * @param listener iChangeListener 监听
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    fun crSetListener(listener:iChangeListener){
 | 
	
		
			
				|  |  | +        this.listener = listener
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  }
 |