1.解决二级界面从左往右快速切换页面时,快按光标异常徘徊问题

This commit is contained in:
2026-03-13 20:12:52 +08:00
parent 84883cd7a8
commit 682c87e27d
7 changed files with 124 additions and 3 deletions

View File

@@ -1,6 +1,7 @@
package com.ik.mboxlauncher.ui.fragment;
import android.content.Intent;
import android.os.Build;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewTreeObserver;
@@ -24,6 +25,7 @@ import java.util.List;
public class AppsFragment extends CategoryFragment {
public static final String ACTION="com.ik.mboxlauncher.ui.fragment.AppsFragment";
private MyAppInfoAdapter mMyAppInfoAdapter = null;
private final int SCROLLPOSITION=17;
@Override
protected void initView(View view) {
super.initView(view);
@@ -59,6 +61,20 @@ public class AppsFragment extends CategoryFragment {
LogUtils.loge("hasFocus===>"+hasFocus);
}
});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
gv_category_apps.setOnScrollChangeListener(new View.OnScrollChangeListener() {
@Override
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
lastFocusPositon=gv_category_apps.getmCurrentFocusPosition();
LogUtils.loge("onScrollChange()AppsFragment pos="+lastFocusPositon);
}
});
}
// h3翻页后gv_category_apps失去焦点处理
if(Build.VERSION.SDK_INT<29&&disableFreshData){
gv_category_apps.requestFocus();
}
LogUtils.loge("AppsFragment initView==>"+disableFreshData);
}
@Override
@@ -72,7 +88,9 @@ public class AppsFragment extends CategoryFragment {
runUiThread(new Runnable() {
@Override
public void run() {
if(disableFreshData) return;
if (data != null) {
disableFreshData=true;
List<AppBean> appBeanList = (List<AppBean>) data;
// 创建过滤后的列表
List<AppBean> filteredList = new ArrayList<>();
@@ -119,6 +137,7 @@ public class AppsFragment extends CategoryFragment {
@Override
public void onResumeFragment(MessageEvent event) {
super.onResumeFragment(event);
disableFreshData=false;
loadAppInfoByCategory(AppManager.CATEGORY_MYAPPS);//重新加载数据
LogUtils.loge("onResumeFragment===>AppsFragment");
@@ -144,11 +163,17 @@ public class AppsFragment extends CategoryFragment {
@Override
protected void toNextPage() {
doneEvents(new MessageEvent(MusicFragment.ACTION));
if(lastFocusPositon>SCROLLPOSITION){
disableFreshData=false;
}
}
@Override
protected void toPreviousPage() {
doneEvents(new MessageEvent(RecommendFragment.ACTION));
if(lastFocusPositon>SCROLLPOSITION){
disableFreshData=false;
}
}
}

View File

@@ -44,7 +44,8 @@ public abstract class CategoryFragment extends BaseFragment implements AppnetCal
protected CustomAppAdapter mCustomAppAdapter=null;
protected int mCategory=-1;
protected boolean disableFreshData;//禁止再次刷新数据
protected int lastFocusPositon;//记录焦点位置
protected static final int VIDEO_CATEGORY=0;
protected static final int RECOMMEND_CATEGORY=1;
@@ -248,6 +249,7 @@ public abstract class CategoryFragment extends BaseFragment implements AppnetCal
private void dismissCustomApp(){
cuttentModel = MODEL_NORMAL;
disableFreshData=false;
LogUtils.loge("coustom_view.getHeight():"+coustom_view.getLayoutParams().height);
TranslateAnimation exitTransAnim = new TranslateAnimation(0.0f, 0.0f, 0,(float)(0 - coustom_view.getLayoutParams().height));

View File

@@ -1,6 +1,7 @@
package com.ik.mboxlauncher.ui.fragment;
import android.content.Intent;
import android.os.Build;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewTreeObserver;
@@ -25,6 +26,8 @@ public class LocalFragment extends CategoryFragment {
public static final String ACTION="com.ik.mboxlauncher.ui.fragment.LocalFragment";
private LocalAppAdapter mLocalAppAdapter =null;
private final int SCROLLPOSITION=17;
@Override
protected void initView(View view) {
mCategory=AppManager.CATEGORY_LOCAL;
@@ -53,6 +56,20 @@ public class LocalFragment extends CategoryFragment {
GridLayoutManager customLayoutManager = new GridLayoutManager(view.getContext(), 6);
gv_category_apps.setLayoutManager(customLayoutManager);
gv_category_apps.setAdapter(mLocalAppAdapter);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
gv_category_apps.setOnScrollChangeListener(new View.OnScrollChangeListener() {
@Override
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
lastFocusPositon=gv_category_apps.getmCurrentFocusPosition();
LogUtils.loge("onScrollChange()LocalFragment pos="+lastFocusPositon);
}
});
}
// h3翻页后gv_category_apps失去焦点处理
if(Build.VERSION.SDK_INT<29&&disableFreshData){
gv_category_apps.requestFocus();
}
LogUtils.loge("LocalFragment initView==>"+disableFreshData);
}
@@ -60,7 +77,9 @@ public class LocalFragment extends CategoryFragment {
@Override
public void onResult(Object data) {
if(disableFreshData) return;
if(data!=null){
disableFreshData=true;//禁用再次刷新数据
collectAppMap.clear();
List<LocalAppBean> appBeanList = (List<LocalAppBean>) data;
@@ -127,13 +146,16 @@ public class LocalFragment extends CategoryFragment {
@Override
protected void toPreviousPage() {
doneEvents(new MessageEvent(MusicFragment.ACTION));
if(lastFocusPositon>SCROLLPOSITION){
disableFreshData=false;
}
}
@Override
public void onResumeFragment(MessageEvent event) {
super.onResumeFragment(event);
disableFreshData=false;
loadAppInfoByCategory(AppManager.CATEGORY_LOCAL);//重新加载数据
}

View File

@@ -1,6 +1,7 @@
package com.ik.mboxlauncher.ui.fragment;
import android.content.Intent;
import android.os.Build;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewTreeObserver;
@@ -25,6 +26,7 @@ public class MusicFragment extends CategoryFragment {
public static final String ACTION="com.ik.mboxlauncher.ui.fragment.MusicFragment";
private MusicAppAdapter mMusicAppAdapter;
private final int SCROLLPOSITION=17;
@Override
protected void initView(View view) {
super.initView(view);
@@ -53,6 +55,20 @@ public class MusicFragment extends CategoryFragment {
GridLayoutManager customLayoutManager = new GridLayoutManager(view.getContext(), 6);
gv_category_apps.setLayoutManager(customLayoutManager);
gv_category_apps.setAdapter(mMusicAppAdapter);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
gv_category_apps.setOnScrollChangeListener(new View.OnScrollChangeListener() {
@Override
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
lastFocusPositon=gv_category_apps.getmCurrentFocusPosition();
LogUtils.loge("onScrollChange()MusicFragment pos="+lastFocusPositon);
}
});
}
// h3翻页后gv_category_apps失去焦点处理
if(Build.VERSION.SDK_INT<29&&disableFreshData){
gv_category_apps.requestFocus();
}
LogUtils.loge("MusicFragment initView==>"+disableFreshData);
}
@@ -127,18 +143,24 @@ public class MusicFragment extends CategoryFragment {
@Override
protected void toNextPage() {
doneEvents(new MessageEvent(LocalFragment.ACTION));
if(lastFocusPositon>SCROLLPOSITION){
disableFreshData=false;
}
}
@Override
protected void toPreviousPage() {
doneEvents(new MessageEvent(AppsFragment.ACTION));
if(lastFocusPositon>SCROLLPOSITION){
disableFreshData=false;
}
}
@Override
public void onResumeFragment(MessageEvent event) {
super.onResumeFragment(event);
disableFreshData=false;
loadAppInfoByCategory(AppManager.CATEGORY_MUSIC);//重新加载数据
}

View File

@@ -1,6 +1,7 @@
package com.ik.mboxlauncher.ui.fragment;
import android.content.Intent;
import android.os.Build;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewTreeObserver;
@@ -25,6 +26,7 @@ public class RecommendFragment extends CategoryFragment {
public static final String ACTION="com.ik.mboxlauncher.ui.fragment.RecommendFragment";
private RecommendAppAdapter mRecommendAppAdapter;
private final int SCROLLPOSITION=17;
@Override
protected void initView(View view) {
mCategory=AppManager.CATEGORY_RECOMMEND;
@@ -54,12 +56,28 @@ public class RecommendFragment extends CategoryFragment {
GridLayoutManager customLayoutManager = new GridLayoutManager(view.getContext(), 6);
gv_category_apps.setLayoutManager(customLayoutManager);
gv_category_apps.setAdapter(mRecommendAppAdapter);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
gv_category_apps.setOnScrollChangeListener(new View.OnScrollChangeListener() {
@Override
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
lastFocusPositon=gv_category_apps.getmCurrentFocusPosition();
LogUtils.loge("onScrollChange()RecommendFragment pos="+lastFocusPositon);
}
});
}
// h3翻页后gv_category_apps失去焦点处理
if(Build.VERSION.SDK_INT<29&&disableFreshData){
gv_category_apps.requestFocus();
}
LogUtils.loge("RecommendFragment initView==>"+disableFreshData);
}
@Override
public void onResult(Object data) {
if(disableFreshData) return;
if(data!=null){
disableFreshData=true;
collectAppMap.clear();
List<RecommendAppBean> appBeanList = (List<RecommendAppBean>) data;
for (RecommendAppBean recommendAppBean:appBeanList){
@@ -126,11 +144,17 @@ public class RecommendFragment extends CategoryFragment {
@Override
protected void toNextPage() {
doneEvents(new MessageEvent(AppsFragment.ACTION));
if(lastFocusPositon>SCROLLPOSITION){
disableFreshData=false;
}
}
@Override
protected void toPreviousPage() {
doneEvents(new MessageEvent(VideoFragment.ACTION));
if(lastFocusPositon>SCROLLPOSITION){
disableFreshData=false;
}
}

View File

@@ -1,6 +1,7 @@
package com.ik.mboxlauncher.ui.fragment;
import android.content.Intent;
import android.os.Build;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewTreeObserver;
@@ -24,6 +25,7 @@ public class VideoFragment extends CategoryFragment {
public static final String ACTION="com.ik.mboxlauncher.ui.fragment.VideoFragment";
private VideoAppAdapter mVideoAppAdapter = null;
private final int SCROLLPOSITION=17;
@Override
protected void initView(View view) {
mCategory=AppManager.CATEGORY_VIDEO;
@@ -53,12 +55,28 @@ public class VideoFragment extends CategoryFragment {
GridLayoutManager layoutManager = new GridLayoutManager(view.getContext(), 6);
gv_category_apps.setLayoutManager(layoutManager);
gv_category_apps.setAdapter(mVideoAppAdapter);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
gv_category_apps.setOnScrollChangeListener(new View.OnScrollChangeListener() {
@Override
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
lastFocusPositon=gv_category_apps.getmCurrentFocusPosition();
LogUtils.loge("onScrollChange()VideoFragment pos="+lastFocusPositon);
}
});
}
// h3翻页后gv_category_apps失去焦点处理
if(Build.VERSION.SDK_INT<29&&disableFreshData){
gv_category_apps.requestFocus();
}
LogUtils.loge("VideoFragment initView==>"+disableFreshData);
}
@Override
public void onResult(Object data) {
if(disableFreshData) return;
if(data!=null){
disableFreshData=true;
collectAppMap.clear();
List<VideoAppBean> appBeanList = (List<VideoAppBean>) data;
for (VideoAppBean videoAppBean:appBeanList){
@@ -128,6 +146,9 @@ public class VideoFragment extends CategoryFragment {
@Override
protected void toNextPage() {
doneEvents(new MessageEvent(RecommendFragment.ACTION));
if(lastFocusPositon>SCROLLPOSITION){
disableFreshData=false;
}
}
@Override
@@ -139,6 +160,7 @@ public class VideoFragment extends CategoryFragment {
@Override
public void onResumeFragment(MessageEvent event) {
super.onResumeFragment(event);
disableFreshData=false;
loadAppInfoByCategory(AppManager.CATEGORY_VIDEO);//重新加载数据
}
}

View File

@@ -166,6 +166,10 @@ public class CustomRecyclerViewer extends RecyclerView {
super.requestChildFocus(child, focused);//执行过super.requestChildFocus之后hasFocus会变成true
mCurrentFocusPosition = getChildViewHolder(child).getAdapterPosition();
}
//获取当前焦点位置
public int getmCurrentFocusPosition() {
return mCurrentFocusPosition;
}
//实现焦点记忆的关键代码
@Override