当前位置: 首页 > news >正文

MFC实现子控件focus焦点上下移动父控件ListView和Gridview也跟着向上下移动

项目中要实现mfc功能,然后子控件焦点下移,LIstView和Gridview父控件不会下移,所以就有这个文章。废话不多说直接上代码。

MFCGridView.java

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewParent;
import android.view.ViewTreeObserver;
import android.widget.GridView;import com.baidu.navisdk.ui.util.MFCUtil;public class MFCGridView extends GridView {protected int lastPosition = -1;protected boolean mHasRegister = false;private final ViewTreeObserver.OnGlobalFocusChangeListener mFocusChangeListener =new ViewTreeObserver.OnGlobalFocusChangeListener() {@Overridepublic void onGlobalFocusChanged(View oldFocus, View newFocus) {if (!isInTouchMode()) {refreshListViewScroll(oldFocus, newFocus);}}};protected void refreshListViewScroll(View oldFocus, View newFocus) {if (getVisibility() != VISIBLE) {return;}if (newFocus == null) {return;}ViewParent convertView = getConvertView(newFocus);if (convertView == null) {return;}if (!(convertView instanceof View)) {return;}Object tagView = ((View) convertView).getTag();if (!(tagView instanceof IMFCHolder)) {if (lastPosition!= getAdapter().getCount() - 1) {smoothScrollToPositionFromTop(0, 0);lastPosition = -1;}return;}int focusedPosition = -1;View focusedChild =  getFocusedChild();if (focusedChild != null) {focusedPosition =  getPositionForView(focusedChild);}if (focusedPosition != lastPosition) {smoothScrollToPositionFromTop(focusedPosition, 50);lastPosition = focusedPosition;}}protected ViewParent getConvertView(View newFocus) {ViewParent lastView = null;ViewParent parent = newFocus.getParent();if (parent == this){return (ViewParent) newFocus;}while (parent != null) {if (parent == this) {return lastView;}lastView = parent;parent = parent.getParent();}return null;}public MFCGridView(Context context) {super(context);setFocusableInTouchMode(false);}public MFCGridView(Context context, AttributeSet attrs) {super(context, attrs);setFocusableInTouchMode(false);}public MFCGridView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);setFocusableInTouchMode(false);}@Overrideprotected void onAttachedToWindow() {super.onAttachedToWindow();if (MFCUtil.isMFCEnable()) {if (!mHasRegister) {getViewTreeObserver().addOnGlobalFocusChangeListener(mFocusChangeListener);mHasRegister = true;}}}@Overrideprotected void onDetachedFromWindow() {super.onDetachedFromWindow();if (MFCUtil.isMFCEnable()) {getViewTreeObserver().removeOnGlobalFocusChangeListener(mFocusChangeListener);mHasRegister = false;}clearDisappearingChildren();}
}
MFCGridView使用方法:xml中直接引用即可,无需其他操作

---------------------------------------------------------分割线---------------------------------------------------------

依赖类IMFCHolder.java
public interface IMFCHolder {int getPosition();
}

MFCListView.java

 import com.baidu.navisdk.ui.util.MFCUtil;import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewParent;
import android.view.ViewTreeObserver;
import android.widget.ListView;public class MFCListView extends ListView {protected boolean mHasRegister = false;protected int lastPosition = -1;private final ViewTreeObserver.OnGlobalFocusChangeListener mFocusChangeListener =new ViewTreeObserver.OnGlobalFocusChangeListener() {@Overridepublic void onGlobalFocusChanged(View oldFocus, View newFocus) {if (!isInTouchMode()) {refreshListViewScroll(oldFocus, newFocus);}}};protected void refreshListViewScroll(View oldFocus, View newFocus) {if (getVisibility() != VISIBLE) {return;}if (newFocus == null) {return;}ViewParent convertView = getConvertView(newFocus);if (convertView == null) {return;}if (!(convertView instanceof View)) {return;}Object tagView = ((View) convertView).getTag();if (!(tagView instanceof IMFCHolder)) {if (lastPosition!= getAdapter().getCount() - getHeaderViewsCount() - getFooterViewsCount()- 1) {smoothScrollToPositionFromTop(0, 0);lastPosition = -1;}return;}IMFCHolder imfcHolder = (IMFCHolder) tagView;int position = imfcHolder.getPosition();if (position != lastPosition) {smoothScrollToPositionFromTop(position + getHeaderViewsCount(), 50);lastPosition = position;}}protected ViewParent getConvertView(View newFocus) {ViewParent lastView = null;ViewParent parent = newFocus.getParent();if (parent == this){return (ViewParent) newFocus;}while (parent != null) {if (parent == this) {return lastView;}lastView = parent;parent = parent.getParent();}return null;}public MFCListView(Context context) {super(context);setFocusableInTouchMode(false);}public MFCListView(Context context, AttributeSet attrs) {super(context, attrs);setFocusableInTouchMode(false);}public MFCListView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);setFocusableInTouchMode(false);}public MFCListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {super(context, attrs, defStyleAttr, defStyleRes);setFocusableInTouchMode(false);}@Overridepublic View getFocusedChild() {return null;}@Overrideprotected void onAttachedToWindow() {super.onAttachedToWindow();if (MFCUtil.isMFCEnable()) {if (!mHasRegister) {getViewTreeObserver().addOnGlobalFocusChangeListener(mFocusChangeListener);mHasRegister = true;}}}@Overrideprotected void onDetachedFromWindow() {super.onDetachedFromWindow();if (MFCUtil.isMFCEnable()) {getViewTreeObserver().removeOnGlobalFocusChangeListener(mFocusChangeListener);mHasRegister = false;}clearDisappearingChildren();}
}

依赖类MFCUtil.java

package com.baidu.navisdk.ui.util;import android.app.Activity;import com.baidu.naviauto.appcommon.AppLog;import java.util.ArrayList;
import java.util.List;public class MFCUtil {private static final String TAG = "MFCUtil";public static final List<String> REQUEST_CHECK_LIST_STRING = new ArrayList<>();public static boolean isMFCEnable() {return true;}/***  返回false 不消费 调用者可以request*  返回true   消费  调用者不可以request* @param activity* @param classname* @return*/public static boolean requestCheck(Activity activity, String classname) {if (activity == null) {return true;}if (!isMFCEnable()) {return true;}if (activity.getWindow().getDecorView().isInTouchMode()){return true;}checkRequestCheckList(activity);if (REQUEST_CHECK_LIST_STRING == null) {AppLog.e(TAG, "checkRequestCheckList  ==  " + classname);return false;}for (int i = 0; i < REQUEST_CHECK_LIST_STRING.size(); i++) {if (REQUEST_CHECK_LIST_STRING.get(i).equals(classname)) {AppLog.e(TAG, "false  ==  " + classname);return false;}}AppLog.e(TAG, "false finish  ==  " + classname);return false;}public static void checkRequestCheckList(Activity activity) {if (REQUEST_CHECK_LIST_STRING != null && REQUEST_CHECK_LIST_STRING.size() == 0) {REQUEST_CHECK_LIST_STRING.add("PowerNotification");REQUEST_CHECK_LIST_STRING.add("RestrictionTipsView");REQUEST_CHECK_LIST_STRING.add("RecommendTripTipsView");REQUEST_CHECK_LIST_STRING.add("PushPoiNaviNotificationView");REQUEST_CHECK_LIST_STRING.add("PushPoiNaviNotificationDialog");REQUEST_CHECK_LIST_STRING.add("NaviAutoActivity");}}public static void onDestory(){if (REQUEST_CHECK_LIST_STRING != null){REQUEST_CHECK_LIST_STRING.clear();}} 
}

MFCListView实际使用例子:

1.xml代码中使用MFCListView类代替

2.adapter中,核心代码如下:

@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ViewHolder viewHolder;if (convertView == null) {convertView = LayoutInflater.from(mContext).inflate(R.layout.item_column, null);viewHolder = new ViewHolder();viewHolder.textView = convertView.findViewById(R.id.text);convertView.setTag(viewHolder);} else {viewHolder = (ViewHolder) convertView.getTag();}viewHolder.position = position;viewHolder.textView.setText(mProvinShotNameArr[position]);return convertView;}public static class ViewHolder  implements IMFCHolder {TextView textView;int position;@Overridepublic int getPosition() {return position;}}

实现成功:子控件焦点滑到中间,gridview父控件也跟着下滑了!!!

相关文章:

  • CogVLM2多模态开源大模型部署与使用
  • 聊聊二叉堆、红黑树、时间轮在定时任务中的应用
  • zookeeper节点启动的主要逻辑
  • 4. MySQL 约束
  • 东方博宜1317 - 正多边形每个内角的度数?
  • webpack学习
  • 掌握复选框(Checkbox)的奥秘:全选与反选功能实现
  • uniapp封装picker选择器组件,支持关键字查询
  • react快速开始(四)-之Vite 还是 (Create React App) CRA? 用Vite创建项目
  • Docker搭建ELKF日志分析系统
  • GPT-4o:免费且更快的模型
  • C语言 指针——函数指针的典型应用:计算定积分
  • CAD二次开发(8)-探索实现不重启CAD进行热部署代码
  • 算法-分治策略
  • 如何复制文件描述符
  • Android路由框架AnnoRouter:使用Java接口来定义路由跳转
  • dva中组件的懒加载
  • EventListener原理
  • Hexo+码云+git快速搭建免费的静态Blog
  • Linux编程学习笔记 | Linux IO学习[1] - 文件IO
  • Mybatis初体验
  • SegmentFault 技术周刊 Vol.27 - Git 学习宝典:程序员走江湖必备
  • Spring Cloud中负载均衡器概览
  • Spring技术内幕笔记(2):Spring MVC 与 Web
  • vue:响应原理
  • vue总结
  • webgl (原生)基础入门指南【一】
  • 从重复到重用
  • 机器学习 vs. 深度学习
  • 数组大概知多少
  • 做一名精致的JavaScripter 01:JavaScript简介
  • Spark2.4.0源码分析之WorldCount 默认shuffling并行度为200(九) ...
  • 阿里云服务器购买完整流程
  • 仓管云——企业云erp功能有哪些?
  • 整理一些计算机基础知识!
  • #ubuntu# #git# repository git config --global --add safe.directory
  • (2)MFC+openGL单文档框架glFrame
  • (3)Dubbo启动时qos-server can not bind localhost22222错误解决
  • (3)选择元素——(17)练习(Exercises)
  • (BAT向)Java岗常问高频面试汇总:MyBatis 微服务 Spring 分布式 MySQL等(1)
  • (板子)A* astar算法,AcWing第k短路+八数码 带注释
  • (附源码)springboot家庭装修管理系统 毕业设计 613205
  • (附源码)计算机毕业设计ssm-Java网名推荐系统
  • (过滤器)Filter和(监听器)listener
  • (原创) cocos2dx使用Curl连接网络(客户端)
  • (转) SpringBoot:使用spring-boot-devtools进行热部署以及不生效的问题解决
  • (转)c++ std::pair 与 std::make
  • ***微信公众号支付+微信H5支付+微信扫码支付+小程序支付+APP微信支付解决方案总结...
  • .babyk勒索病毒解析:恶意更新如何威胁您的数据安全
  • .Net 4.0并行库实用性演练
  • .NET Core实战项目之CMS 第一章 入门篇-开篇及总体规划
  • .NET Framework与.NET Framework SDK有什么不同?
  • .net 使用ajax控件后如何调用前端脚本
  • .NET/ASP.NETMVC 大型站点架构设计—迁移Model元数据设置项(自定义元数据提供程序)...
  • .NetCore 如何动态路由