看到京东电视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(); } }}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
以上是内存溢出为你收集整理的android实现按钮获取焦点延迟加载全部内容,希望文章能够帮你解决android实现按钮获取焦点延迟加载所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)