国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

android實現按鈕獲取焦點延遲加載

2019-10-22 18:27:01
字體:
來源:轉載
供稿:網友

看到京東電視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();    }  }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黔西县| 健康| 杭锦后旗| 上思县| 文水县| 得荣县| 年辖:市辖区| 新干县| 大同市| 鄄城县| 调兵山市| 仁寿县| 大同市| 长春市| 西城区| 辉县市| 长沙县| 永济市| 乌鲁木齐县| 侯马市| 玉溪市| 诏安县| 东阿县| 侯马市| 万州区| 洪泽县| 大荔县| 渑池县| 巩留县| 虹口区| 彝良县| 舒兰市| 海兴县| 阿鲁科尔沁旗| 庆城县| 墨玉县| 义马市| 千阳县| 绍兴县| 榆林市| 三门县|