From 08449d8abbf176596007b70abfe1bf4347b620b3 Mon Sep 17 00:00:00 2001 From: maxinyu Date: Fri, 29 May 2026 17:04:26 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=BE=E5=A4=87=E5=91=8A=E8=AD=A6=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E5=8A=9F=E8=83=BD=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../adapter/AlarmLogAdapter.kt | 11 +++- .../liveVideo/AlarmLogActivity.kt | 60 +++++++++++++++++-- .../liveVideo/LiveVideoActivity.kt | 27 +++++++-- 3 files changed, 86 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/example/defenseapplication/adapter/AlarmLogAdapter.kt b/app/src/main/java/com/example/defenseapplication/adapter/AlarmLogAdapter.kt index bfc2892..c648e23 100644 --- a/app/src/main/java/com/example/defenseapplication/adapter/AlarmLogAdapter.kt +++ b/app/src/main/java/com/example/defenseapplication/adapter/AlarmLogAdapter.kt @@ -6,7 +6,10 @@ import androidx.recyclerview.widget.RecyclerView import com.example.defenseapplication.bean.DeviceAlarmBean import com.example.defenseapplication.databinding.ItemAlarmLogBinding -class AlarmLogAdapter(private val items: List) : RecyclerView.Adapter() { +class AlarmLogAdapter( + private val items: List, + private val onItemClick: (DeviceAlarmBean) -> Unit +) : RecyclerView.Adapter() { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { val binding = ItemAlarmLogBinding.inflate( @@ -23,10 +26,14 @@ class AlarmLogAdapter(private val items: List) : RecyclerView.A override fun getItemCount(): Int = items.size - class ViewHolder(private val binding: ItemAlarmLogBinding) : RecyclerView.ViewHolder(binding.root) { + inner class ViewHolder(private val binding: ItemAlarmLogBinding) : RecyclerView.ViewHolder(binding.root) { fun bind(item: DeviceAlarmBean) { binding.tvTitle.text = item.categoryName binding.tvTime.text = item.createTime + + binding.root.setOnClickListener { + onItemClick(item) + } } } } diff --git a/app/src/main/java/com/example/defenseapplication/liveVideo/AlarmLogActivity.kt b/app/src/main/java/com/example/defenseapplication/liveVideo/AlarmLogActivity.kt index 7a2f683..9def8d5 100644 --- a/app/src/main/java/com/example/defenseapplication/liveVideo/AlarmLogActivity.kt +++ b/app/src/main/java/com/example/defenseapplication/liveVideo/AlarmLogActivity.kt @@ -42,6 +42,10 @@ class AlarmLogActivity : AppCompatActivity() { private var isLightOn = false private var deviceId: String? = null private var isLoading = false + private var fromLiveVideo = false + private var passedVideoUrl: String? = null + private var passedSelectedId: String? = null + private var passedCreateTime: String? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -53,6 +57,10 @@ class AlarmLogActivity : AppCompatActivity() { .init() deviceId = intent.getStringExtra("device_id") + fromLiveVideo = intent.getBooleanExtra("from_live_video", false) + passedVideoUrl = intent.getStringExtra("video_url") + passedSelectedId = intent.getStringExtra("selected_id") + passedCreateTime = intent.getStringExtra("create_time") calendar = Calendar.getInstance() updateDateDisplay() initVideoPlayer() @@ -77,9 +85,14 @@ class AlarmLogActivity : AppCompatActivity() { private fun initVideoPlayer() { val videoPlayerView = binding.exoPlayContextId + val videoUri = if (fromLiveVideo && !passedVideoUrl.isNullOrEmpty()) { + passedVideoUrl!! + } else { + "https://media.w3.org/2010/05/sintel/trailer.mp4" + } exoPlayerManager = VideoPlayerManager.Builder(VideoPlayerManager.TYPE_PLAY_GESTURE, videoPlayerView) - .setPlayUri("https://media.w3.org/2010/05/sintel/trailer.mp4") + .setPlayUri(videoUri) .setPosition(0) .addVideoInfoListener(object : VideoInfoListener { override fun onPlayStart(p0: Long) { @@ -87,7 +100,6 @@ class AlarmLogActivity : AppCompatActivity() { } override fun onLoadingChanged() { - // 视频加载状态变化 } override fun onPlayerError(p0: com.google.android.exoplayer2.ExoPlaybackException?) { @@ -99,11 +111,9 @@ class AlarmLogActivity : AppCompatActivity() { } override fun isPlaying(p0: Boolean) { - // p0 表示当前是否正在播放 } fun onRepeatModeChanged(p0: Int) { - // 循环模式改变时的回调 } }) .create() @@ -145,10 +155,49 @@ class AlarmLogActivity : AppCompatActivity() { } private fun initRecyclerView() { - adapter = AlarmLogAdapter(alarmList) + adapter = AlarmLogAdapter(alarmList) { item -> + if (!item.video.isNullOrEmpty()) { + playVideo(item.video!!) + } else { + Toast.makeText(this, "该告警记录没有视频", Toast.LENGTH_SHORT).show() + } + } binding.alarmListRecyclerView.layoutManager = LinearLayoutManager(this) binding.alarmListRecyclerView.adapter = adapter } + + private fun playVideo(videoUrl: String) { + exoPlayerManager.onDestroy() + + exoPlayerManager = VideoPlayerManager.Builder(VideoPlayerManager.TYPE_PLAY_GESTURE, binding.exoPlayContextId) + .setPlayUri(videoUrl) + .setPosition(0) + .addVideoInfoListener(object : VideoInfoListener { + override fun onPlayStart(p0: Long) { + Log.d("AlarmLogActivity", "视频开始播放") + } + + override fun onLoadingChanged() { + } + + override fun onPlayerError(p0: com.google.android.exoplayer2.ExoPlaybackException?) { + Toast.makeText(this@AlarmLogActivity, "播放失败", Toast.LENGTH_SHORT).show() + } + + override fun onPlayEnd() { + Log.d("AlarmLogActivity", "播放结束") + } + + override fun isPlaying(p0: Boolean) { + } + + fun onRepeatModeChanged(p0: Int) { + } + }) + .create() + + exoPlayerManager.startPlayer() + } private fun fetchAlarmList() { val currentDeviceId = deviceId ?: return @@ -297,6 +346,7 @@ class AlarmLogActivity : AppCompatActivity() { .setTextColorCenter(Color.parseColor("#09C2BD"))//选中颜色 .setLineSpacingMultiplier(2.5f)//行间距倍数 .setItemVisibleCount(5)//可见项数量 + .setDate(calendar) // 设置默认选中日期为tvDate显示的日期 .build() timePicker.show() } diff --git a/app/src/main/java/com/example/defenseapplication/liveVideo/LiveVideoActivity.kt b/app/src/main/java/com/example/defenseapplication/liveVideo/LiveVideoActivity.kt index 6789a69..efd86d5 100644 --- a/app/src/main/java/com/example/defenseapplication/liveVideo/LiveVideoActivity.kt +++ b/app/src/main/java/com/example/defenseapplication/liveVideo/LiveVideoActivity.kt @@ -45,9 +45,11 @@ class LiveVideoActivity : BaseActivity() { private var alarmList: MutableList = mutableListOf() private var alarmAdapter: AlarmLogHorizontalAdapter? = null private var currentPage = 1 + private var totalPage = 1 private val pageSize = 10 private var hasMore = true private var isLoading = false + private var isFirstLoad = true private val requestPermissionLauncher = registerForActivityResult( ActivityResultContracts.RequestPermission() @@ -130,6 +132,9 @@ class LiveVideoActivity : BaseActivity() { val intent = Intent(this, AlarmLogActivity::class.java) intent.putExtra("device_id", deviceId) intent.putExtra("selected_id", messageBean.id) + intent.putExtra("video_url", messageBean.video) + intent.putExtra("create_time", messageBean.createTime) + intent.putExtra("from_live_video", true) startActivity(intent) } binding.alarmLogRecyclerView.adapter = alarmAdapter @@ -139,8 +144,7 @@ class LiveVideoActivity : BaseActivity() { super.onScrolled(recyclerView, dx, dy) if (dx > 0 && !isLoading && hasMore) { val lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition() - val totalItemCount = layoutManager.itemCount - if (lastVisibleItemPosition >= totalItemCount - 1) { + if (lastVisibleItemPosition == alarmList.size - 1) { loadMoreAlarms() } } @@ -151,6 +155,10 @@ class LiveVideoActivity : BaseActivity() { private fun loadMoreAlarms() { if (!hasMore || isLoading) return currentPage++ + if (currentPage > totalPage) { + hasMore = false + return + } fetchAlarmList(currentPage) } @@ -187,6 +195,7 @@ class LiveVideoActivity : BaseActivity() { binding.xq.setOnClickListener { val intent = Intent(this, AlarmLogActivity::class.java) intent.putExtra("device_id", deviceId) + intent.putExtra("from_live_video", true) startActivity(intent) } } @@ -220,17 +229,25 @@ class LiveVideoActivity : BaseActivity() { } } + totalPage = if (response.total % pageSize == 0) { + response.total / pageSize + } else { + response.total / pageSize + 1 + } + Log.d("LiveVideoActivity", "总页数: $totalPage") + + isFirstLoad = false + if (page == 1) { alarmList.clear() - hasMore = true + hasMore = totalPage > 1 Log.d("LiveVideoActivity", "第一页,已清空列表") } + alarmList.addAll(newData) Log.d("LiveVideoActivity", "已添加 ${newData.size} 条数据,列表总条数: ${alarmList.size}") alarmAdapter?.notifyDataSetChanged() Log.d("LiveVideoActivity", "已通知适配器刷新") - hasMore = newData.size >= pageSize - Log.d("LiveVideoActivity", "是否有更多数据: $hasMore") } catch (e: Exception) { Log.e("LiveVideoActivity", "请求异常: ${e.message}", e)