android实现按钮获取焦点延迟加载
程序员文章站
2023-12-14 22:24:10
看到京东电视app的按钮获取焦点后才加载数据,之前网上没找到好的说法,所以自己实现了记录一下以便后续学习。
主要是按钮获取焦点以后,初始化一个定时器timer延迟500m...
看到京东电视app的按钮获取焦点后才加载数据,之前网上没找到好的说法,所以自己实现了记录一下以便后续学习。
主要是按钮获取焦点以后,初始化一个定时器timer延迟500ms加载数据,如果失去焦点,取消这个定时器就ok了。其实原理很简单,下面是我实现的一个效果。
package com.longmaster.iptv.health.doctorp2p.mode; import android.content.context; import android.support.v7.widget.linearlayoutmanager; import android.support.v7.widget.recyclerview; import android.view.keyevent; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.linearlayout; import android.widget.relativelayout; import com.longmaster.iptv.health.r; import com.longmaster.iptv.health.common.constants; import com.longmaster.iptv.health.widget.magictextview.magictextview; import org.videowebinterface.departmentparam; import java.util.arraylist; import java.util.list; import java.util.timer; import java.util.timertask; import cn.longmaster.lmkit.event.messageproxy; import cn.longmaster.lmkit.utils.applogger; /** * created by yzq on 2017/5/8 */ public class buttonadapter extends recyclerview.adapter<buttonadapter.myviewholder> { private list<departmentparam> mdepartmentlist = new arraylist<>(); private layoutinflater minflater; private context mcontext; private boolean mislostfocus = false; private linearlayoutmanager mlinearlayoutmanager; private recyclerview mrecyclerview; private timertask mtimertask; private timer mtimer; private boolean isloaddata = true; public buttonadapter(context context) { super(); this.mcontext = context; minflater = layoutinflater.from(context); } public void setlayoutmanager(linearlayoutmanager linearlayoutmanager, recyclerview re) { mlinearlayoutmanager = linearlayoutmanager; mrecyclerview = re; } public void setdepartmentlist(list list) { mdepartmentlist = list; } public void setfocus(boolean isfocus) { mislostfocus = isfocus; } public void setisloaddata(boolean isloaddata) { this.isloaddata = isloaddata; } public void clear() { mdepartmentlist.clear(); } @override public int getitemcount() { return mdepartmentlist.size(); } @override public void onbindviewholder(final myviewholder holder, final int position) { holder.tv.settext(mdepartmentlist.get(position).getdeptname()); holder.tv.setfocusable(true); } @override public myviewholder oncreateviewholder(viewgroup parent, int viewtype) { view view = minflater.inflate(r.layout.list_class_item, parent, false); myviewholder holder = new myviewholder(view); return holder; } class myviewholder extends recyclerview.viewholder { magictextview tv; relativelayout mouterbgrl; public myviewholder(final view view) { super(view); recyclerview.layoutparams param = (recyclerview.layoutparams) itemview.getlayoutparams(); param.height = linearlayout.layoutparams.wrap_content; param.width = linearlayout.layoutparams.match_parent; itemview.setvisibility(view.visible); tv = (magictextview) view.findviewbyid(r.id.class_name); mouterbgrl = (relativelayout) view.findviewbyid(r.id.rl_class_outer_bg); tv.setonfocuschangelistener(new view.onfocuschangelistener() { @override public void onfocuschange(view v, boolean hasfocus) { if (hasfocus) { mouterbgrl.setbackgroundresource(r.drawable.p2p_select); if (isloaddata) { mtimertask = new timertask() { @override public void run() { int iposition = getadapterposition(); departmentparam departmentparam = mdepartmentlist.get(iposition); messageproxy.sendmessage(constants.message.p2p_selection_doctor_class, iposition, departmentparam); } }; mtimer = new timer(); mtimer.schedule(mtimertask, 500); } } else { cleartimer(); if (mislostfocus) { mouterbgrl.setbackgroundresource(r.drawable.p2p_unselect); } } } }); tv.setonkeylistener(new view.onkeylistener() { @override public boolean onkey(view v, int keycode, keyevent event) { if (event.getaction() == keyevent.action_down) { int mcurrentselectposition = getadapterposition(); int mdepartmentcount = mdepartmentlist.size(); applogger.e("mcurrentselectposition>>>>>>>>", mcurrentselectposition + "----" + mdepartmentcount); switch (keycode) { case keyevent.keycode_dpad_left: setisloaddata(true); setfocus(true); if (mcurrentselectposition == (mlinearlayoutmanager.findfirstvisibleitemposition())) { mrecyclerview.smoothscrollby(-400, 0); } view view1 = mlinearlayoutmanager.findviewbyposition(mcurrentselectposition - 1); if (view1 != null) { setviewfocus(view1.findviewbyid(r.id.class_name)); } return true; case keyevent.keycode_dpad_right: if (mcurrentselectposition == mdepartmentcount - 1) { return true; } setisloaddata(true); setfocus(true); if (mcurrentselectposition == (mlinearlayoutmanager.findlastvisibleitemposition())) { mrecyclerview.smoothscrollby(400, 0); } view view = mlinearlayoutmanager.findviewbyposition(mcurrentselectposition + 1); if (view != null) { setviewfocus(view.findviewbyid(r.id.class_name)); } return true; case keyevent.keycode_back: break; case keyevent.keycode_dpad_up: break; case keyevent.keycode_dpad_down: setfocus(false); mouterbgrl.setbackgroundresource(r.drawable.p2p_key_down); break; } } return false; } }); } } public void setviewfocus(view view) { view.setfocusable(true); view.setfocusableintouchmode(true); view.requestfocus(); view.requestfocusfromtouch(); } private void cleartimer() { if (mtimer != null) { mtimer.cancel(); } if (mtimertask != null) { mtimertask.cancel(); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。