修复我的界面重构闪退

This commit is contained in:
muqing 2023-12-28 10:39:35 +08:00
parent 188a1d4a06
commit 3e0d363f3d
7 changed files with 64 additions and 128 deletions

View File

@ -18,7 +18,6 @@ import android.widget.LinearLayout;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import com.google.gson.Gson; import com.google.gson.Gson;
@ -76,22 +75,22 @@ public class bfq extends AppCompatActivity {
}); });
} }
} }
@SuppressLint("ClickableViewAccessibility")
@Override @Override
protected void onCreate(@Nullable Bundle savedInstanceState) { protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
binding = ActivityBfqBinding.inflate(getLayoutInflater()); binding = ActivityBfqBinding.inflate(getLayoutInflater());
view = binding.getRoot();
setLrc(); setLrc();
new Media(binding); new Media(binding);
view = binding.getRoot();
TypedValue typedValue = new TypedValue(); TypedValue typedValue = new TypedValue();
home.appCompatActivity.getTheme().resolveAttribute(android.R.attr.windowBackground, typedValue, true); home.appCompatActivity.getTheme().resolveAttribute(android.R.attr.windowBackground, typedValue, true);
// 设置背景颜色 // 设置背景颜色
view.setBackgroundColor(typedValue.data); view.setBackgroundColor(typedValue.data);
setContentView(view); setContentView(view);
Toolbar toolbar = binding.toolbar; binding.toolbar.setNavigationOnClickListener(view1 -> finish());
toolbar.setNavigationOnClickListener(view1 -> finish()); binding.toolbar.setOnMenuItemClickListener(item -> {
toolbar.setOnMenuItemClickListener(item -> {
if (item.getItemId() == R.id.fx && bfqkz.xm != null) { if (item.getItemId() == R.id.fx && bfqkz.xm != null) {
com.muqingbfq.mq.gj.fx(this, com.muqingbfq.mq.gj.fx(this,
"音乐名称:" + bfqkz.xm.name + "音乐名称:" + bfqkz.xm.name +
@ -110,7 +109,9 @@ public class bfq extends AppCompatActivity {
if (bfqkz.mt != null && bfqkz.mt.isPlaying()) { if (bfqkz.mt != null && bfqkz.mt.isPlaying()) {
binding.kg.setImageResource(R.drawable.bf); binding.kg.setImageResource(R.drawable.bf);
} }
text(); binding.toolbar.setOnTouchListener(new Touch());
view.setOnTouchListener(new Touch());
binding.like.setOnClickListener(view1 -> { binding.like.setOnClickListener(view1 -> {
try { try {
Gson gson = new Gson(); Gson gson = new Gson();
@ -208,11 +209,11 @@ public class bfq extends AppCompatActivity {
super.onConfigurationChanged(newConfig); super.onConfigurationChanged(newConfig);
} }
public class Touch implements View.OnTouchListener {
private float downY, moveY; private float downY, moveY;
@SuppressLint("ClickableViewAccessibility") @Override
public void text() { public boolean onTouch(View view, MotionEvent motionEvent) {LinearLayout root = binding.getRoot();
binding.toolbar.setOnTouchListener((view, motionEvent) -> {
LinearLayout root = binding.getRoot();
switch (motionEvent.getAction()) { switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_DOWN:
downY = motionEvent.getRawY(); downY = motionEvent.getRawY();
@ -248,7 +249,7 @@ public class bfq extends AppCompatActivity {
break; break;
} }
return true; return true;
}); }
} }
public static void setlike(boolean bool) { public static void setlike(boolean bool) {
if (binding == null) { if (binding == null) {

View File

@ -5,7 +5,6 @@ import android.app.PendingIntent;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.IBinder; import android.os.IBinder;
import android.support.v4.media.MediaBrowserCompat; import android.support.v4.media.MediaBrowserCompat;
@ -56,12 +55,11 @@ public class bfqkz extends MediaBrowserServiceCompat {
try { try {
if (TextUtils.isEmpty(id)) { if (TextUtils.isEmpty(id)) {
//针对错误进行相应的处理 //针对错误进行相应的处理
/* if (bfqkz.list.size() < 1) { if (bfqkz.list.size() < 1) {
return; return;
} }
bfqkz.list.remove(bfqkz.xm); bfqkz.list.remove(bfqkz.xm);
bfqkz.xm = bfqkz.list.get(bfqkz.getmti(bfqkz.ms)); bfq_an.xyq();
new mp3(id);*/
return; return;
} }
if (TextUtils.isEmpty(xm.picurl)) { if (TextUtils.isEmpty(xm.picurl)) {
@ -199,15 +197,6 @@ public class bfqkz extends MediaBrowserServiceCompat {
public void onLoadChildren(@NonNull String parentId, @NonNull Result<List<MediaBrowserCompat.MediaItem>> result) { public void onLoadChildren(@NonNull String parentId, @NonNull Result<List<MediaBrowserCompat.MediaItem>> result) {
} }
/*
public class MyBinder extends Binder {
bfqkz getService() {
return bfqkz.this;
}
}
*/
public static void updateNotification() { public static void updateNotification() {
try { try {
// 更新通知栏的播放状态 // 更新通知栏的播放状态

View File

@ -14,6 +14,7 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.muqingbfq.R; import com.muqingbfq.R;
import com.muqingbfq.databinding.CleanBinding; import com.muqingbfq.databinding.CleanBinding;
import com.muqingbfq.mq.FragmentActivity; import com.muqingbfq.mq.FragmentActivity;
@ -41,6 +42,9 @@ public class fragment_clean extends FragmentActivity {
list.add(new String[]{"下载的音乐", wj.mp3}); list.add(new String[]{"下载的音乐", wj.mp3});
list.add(new String[]{"下载的歌单",wj.gd}); list.add(new String[]{"下载的歌单",wj.gd});
list.add(new String[]{"缓存的音乐",wj.filesdri+"hc"}); list.add(new String[]{"缓存的音乐",wj.filesdri+"hc"});
list.add(new String[]{"内部缓存", getCacheDir().toString()});
String s = Glide.getPhotoCacheDir(this).toString();
list.add(new String[]{"Glide缓存", s});
binding.toolbar.setTitle("储存清理"); binding.toolbar.setTitle("储存清理");
binding.recyclerview.setAdapter(adapter); binding.recyclerview.setAdapter(adapter);
} }
@ -70,62 +74,15 @@ public class fragment_clean extends FragmentActivity {
} else { } else {
holder.checkBox.setEnabled(false); holder.checkBox.setEnabled(false);
} }
/* GestureDetector gestureDetector=new GestureDetector(fragment_clean.this,
new GestureDetector.OnGestureListener() {
@Override
public boolean onDown(@NonNull MotionEvent motionEvent) {
return true;
}
@Override
public void onShowPress(@NonNull MotionEvent motionEvent) {
}
@Override
public boolean onSingleTapUp(@NonNull MotionEvent motionEvent) {
return false;
}
@Override
public boolean onScroll(@Nullable MotionEvent motionEvent, @NonNull MotionEvent motionEvent1, float v, float v1) {
return false;
}
@Override
public void onLongPress(@NonNull MotionEvent motionEvent) {
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
// 计算水平方向滑动的距离
float deltaX = e2.getX() - e1.getX();
holder.checkBox.setX(deltaX);
// 判断是否是从左向右滑动并且滑动距离超过一定阈值
if (deltaX > 0 && Math.abs(deltaX) > 100) {
// 执行删除操作
gj.sc("sc");
return true;
}
return true;
}
});*/
String s1 = Formatter.formatFileSize(fragment_clean.this, leng); String s1 = Formatter.formatFileSize(fragment_clean.this, leng);
holder.checkBox.setText(s[0] + ":" + s1 + " 共计:" + size+" 个文件"); holder.checkBox.setText(s[0] + ":" + s1 + " 共计:" + size+" 个文件");
/* holder.checkBox.setOnTouchListener((view, motionEvent) ->
gestureDetector.onTouchEvent(motionEvent));*/
holder.checkBox.setOnCheckedChangeListener((compoundButton, b) -> { holder.checkBox.setOnCheckedChangeListener((compoundButton, b) -> {
if (b) { if (b) {
list_box.add(file.toString()); list_box.add(file.toString());
}else { }else {
list_box.remove(file.toString()); list_box.remove(file.toString());
} }
if (list_box.size() > 0) { menu_deleat.setVisible(list_box.size() > 0);
menu_deleat.setVisible(true);
} else {
menu_deleat.setVisible(false);
}
}); });
} }

View File

@ -1,14 +1,11 @@
package com.muqingbfq.fragment; package com.muqingbfq.fragment;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import android.widget.LinearLayout;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@ -16,11 +13,7 @@ import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import com.muqingbfq.R;
import com.muqingbfq.api.resource; import com.muqingbfq.api.resource;
import com.muqingbfq.databinding.FragmentWdBinding;
import com.muqingbfq.login.user_editing;
import com.muqingbfq.login.user_logs;
import com.muqingbfq.main; import com.muqingbfq.main;
import com.muqingbfq.xm; import com.muqingbfq.xm;
@ -53,9 +46,8 @@ public class gd_adapter extends Fragment {
return layout; return layout;
} }
private class sx implements Runnable { private class sx implements Runnable {
gd.baseadapter baseadapter; RecyclerView.Adapter baseadapter;
public sx(RecyclerView.Adapter baseadapter) {
public sx(gd.baseadapter baseadapter) {
this.baseadapter = baseadapter; this.baseadapter = baseadapter;
} }
@SuppressLint("NotifyDataSetChanged") @SuppressLint("NotifyDataSetChanged")

View File

@ -39,15 +39,23 @@ public class wode extends Fragment {
public static TextView name, jieshao; public static TextView name, jieshao;
@SuppressLint("StaticFieldLeak") @SuppressLint("StaticFieldLeak")
public static ImageView imageView; public static ImageView imageView;
FragmentWdBinding binding; FragmentWdBinding binding;
private final Object[][] lista = {
{R.drawable.bf, "最近播放", "mp3_hc.json"},
{R.drawable.download, "下载音乐", "mp3_xz.json"},
{R.drawable.like, "喜欢音乐", "mp3_like.json"},
{R.drawable.icon, "本地搜索", ""},
{R.drawable.icon, "我的歌单", ""},
{R.drawable.icon, "导入歌单", ""},
{R.drawable.paihangbang, "排行榜", "排行榜"},
{R.drawable.icon, "开发中", ""}
};
private final List<com.muqingbfq.xm> list = new ArrayList<>(); private final List<com.muqingbfq.xm> list = new ArrayList<>();
@Nullable @Nullable
@Override @Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
binding = FragmentWdBinding.inflate(inflater, container, false); binding = FragmentWdBinding.inflate(inflater, container, false);
View view = binding.getRoot();
name = binding.text1; name = binding.text1;
jieshao = binding.text2; jieshao = binding.text2;
imageView = binding.imageView; imageView = binding.imageView;
@ -58,26 +66,16 @@ public class wode extends Fragment {
startActivity(new Intent(getContext(), user_editing.class)); startActivity(new Intent(getContext(), user_editing.class));
} }
}); });
new user_message(); new user_message();
// int k = (int) (main.k / getResources().getDisplayMetrics().density + 0.5f); // int k = (int) (main.k / getResources().getDisplayMetrics().density + 0.5f);
GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 4){ GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 4) {
@Override @Override
public boolean canScrollVertically() { public boolean canScrollVertically() {
return false;//禁止滑动 return false;//禁止滑动
} }
}; };
binding.recyclerview1.setLayoutManager(gridLayoutManager); binding.recyclerview1.setLayoutManager(gridLayoutManager);
final Object[][] lista = {
{R.drawable.bf, "最近播放", "mp3_hc.json"},
{R.drawable.download, "下载音乐", "mp3_xz.json"},
{R.drawable.like, "喜欢音乐", "mp3_like.json"},
{R.drawable.icon, "本地搜索", ""},
{R.drawable.icon, "我的歌单", ""},
{R.drawable.icon, "导入歌单", ""},
{R.drawable.paihangbang, "排行榜", "排行榜"},
{R.drawable.icon, "开发中", ""}
};
binding.recyclerview1.setFocusable(false); binding.recyclerview1.setFocusable(false);
binding.recyclerview1.setAdapter(new RecyclerView.Adapter<VH>() { binding.recyclerview1.setAdapter(new RecyclerView.Adapter<VH>() {
@NonNull @NonNull
@ -120,7 +118,6 @@ public class wode extends Fragment {
return lista.length; return lista.length;
} }
}); });
sx();
binding.recyclerview2.setLayoutManager(new LinearLayoutManager(getContext()) { binding.recyclerview2.setLayoutManager(new LinearLayoutManager(getContext()) {
@Override @Override
public boolean canScrollVertically() { public boolean canScrollVertically() {
@ -129,7 +126,8 @@ public class wode extends Fragment {
}); });
binding.recyclerview2.setFocusable(false); binding.recyclerview2.setFocusable(false);
binding.recyclerview2.setAdapter(new gd.baseadapter(getContext(), list, true)); binding.recyclerview2.setAdapter(new gd.baseadapter(getContext(), list, true));
return view; sx();
return binding.getRoot();
} }
class VH extends RecyclerView.ViewHolder { class VH extends RecyclerView.ViewHolder {
@ -146,7 +144,6 @@ public class wode extends Fragment {
@SuppressLint("NotifyDataSetChanged") @SuppressLint("NotifyDataSetChanged")
public void sx() { public void sx() {
try { try {
List<xm> a = new ArrayList<>(list);
list.clear(); list.clear();
JSONObject date = new JSONObject(wj.dqwb(wj.gd_xz)); JSONObject date = new JSONObject(wj.dqwb(wj.gd_xz));
for (Iterator<String> it = date.keys(); it.hasNext(); ) { for (Iterator<String> it = date.keys(); it.hasNext(); ) {
@ -157,9 +154,6 @@ public class wode extends Fragment {
String picUrl = jsonObject.getString("picUrl"); String picUrl = jsonObject.getString("picUrl");
list.add(new xm(id, name, picUrl, cz)); list.add(new xm(id, name, picUrl, cz));
} }
if (list.equals(a)) {
return;
}
main.handler.post(() -> binding.recyclerview2.getAdapter().notifyDataSetChanged()); main.handler.post(() -> binding.recyclerview2.getAdapter().notifyDataSetChanged());
} catch (Exception e) { } catch (Exception e) {
gj.sc(e); gj.sc(e);

View File

@ -35,6 +35,7 @@ public class home extends AppCompatActivity {
ActivityHomeBinding binding; ActivityHomeBinding binding;
public MediaBrowserCompat mBrowser; public MediaBrowserCompat mBrowser;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
appCompatActivity = this; appCompatActivity = this;
@ -61,14 +62,16 @@ public class home extends AppCompatActivity {
//初始化播放器组件 //初始化播放器组件
// 启动Service // 启动Service
if (componentName == null) {
componentName = new ComponentName(this, bfqkz.class); componentName = new ComponentName(this, bfqkz.class);
mBrowser = new MediaBrowserCompat( mBrowser = new MediaBrowserCompat(
this,componentName this, componentName
,//绑定服务端 ,//绑定服务端
browserConnectionCallback,//设置连接回调 browserConnectionCallback,//设置连接回调
null null
); );
mBrowser.connect(); mBrowser.connect();
}
//检测更新 //检测更新
new gj.jianchagengxin(this); new gj.jianchagengxin(this);
binding.editView.setOnClickListener(view -> binding.editView.setOnClickListener(view ->
@ -80,9 +83,11 @@ public class home extends AppCompatActivity {
} }
public static ComponentName componentName; public static ComponentName componentName;
private final List<Fragment> list = new ArrayList<>(); private Adaper adapter;
private class adaper extends FragmentStateAdapter {
public adaper(@NonNull FragmentActivity fragmentActivity) { private class Adaper extends FragmentStateAdapter {
List<Fragment> list = new ArrayList<>();
public Adaper(@NonNull FragmentActivity fragmentActivity) {
super(fragmentActivity); super(fragmentActivity);
list.add(new gd_adapter()); list.add(new gd_adapter());
list.add(new wode()); list.add(new wode());
@ -97,9 +102,10 @@ public class home extends AppCompatActivity {
return list.size(); return list.size();
} }
} }
public void UI() { public void UI() {
binding.viewPager.setAdapter(new adaper(this)); adapter = new Adaper(this);
binding.viewPager.setAdapter(adapter);
binding.viewPager.setSaveEnabled(false);
// ViewPager2 绑定到 TabLayout // ViewPager2 绑定到 TabLayout
binding.tablayout.setOnItemSelectedListener(item -> { binding.tablayout.setOnItemSelectedListener(item -> {
int itemId = item.getItemId(); int itemId = item.getItemId();
@ -128,7 +134,7 @@ public class home extends AppCompatActivity {
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.toString(); e.toString();
} }
wode fragment = (wode) list.get(position); wode fragment = (wode) adapter.createFragment(position);
fragment.sx(); fragment.sx();
} }
}.start(); }.start();
@ -137,6 +143,7 @@ public class home extends AppCompatActivity {
} }
}); });
} }
@Override @Override
protected void onPause() { protected void onPause() {
super.onPause(); super.onPause();
@ -156,6 +163,7 @@ public class home extends AppCompatActivity {
.replace(R.id.bfq_db, new bfq_db()) .replace(R.id.bfq_db, new bfq_db())
.commit(); .commit();
} }
private long time; private long time;
@Override @Override
@ -190,12 +198,11 @@ public class home extends AppCompatActivity {
} }
/** /**
* 连接状态的回调接口连接成功时会调用onConnected()方法 * 连接状态的回调接口连接成功时会调用onConnected()方法
*/ */
private MediaBrowserCompat.ConnectionCallback browserConnectionCallback= private final MediaBrowserCompat.ConnectionCallback browserConnectionCallback =
new MediaBrowserCompat.ConnectionCallback(){ new MediaBrowserCompat.ConnectionCallback() {
@Override @Override
public void onConnected() { public void onConnected() {
//必须在确保连接成功的前提下执行订阅的操作 //必须在确保连接成功的前提下执行订阅的操作
@ -233,7 +240,7 @@ public class home extends AppCompatActivity {
* 向媒体服务器(MediaBrowserService)发起数据订阅请求的回调接口 * 向媒体服务器(MediaBrowserService)发起数据订阅请求的回调接口
*/ */
private final MediaBrowserCompat.SubscriptionCallback browserSubscriptionCallback = private final MediaBrowserCompat.SubscriptionCallback browserSubscriptionCallback =
new MediaBrowserCompat.SubscriptionCallback(){ new MediaBrowserCompat.SubscriptionCallback() {
@Override @Override
public void onChildrenLoaded(@NonNull String parentId, public void onChildrenLoaded(@NonNull String parentId,
@NonNull List<MediaBrowserCompat.MediaItem> children) { @NonNull List<MediaBrowserCompat.MediaItem> children) {

View File

@ -5,17 +5,15 @@ import android.annotation.SuppressLint;
import android.app.NotificationChannel; import android.app.NotificationChannel;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.app.Service;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.graphics.BitmapFactory;
import android.os.Build; import android.os.Build;
import android.support.v4.media.MediaMetadataCompat;
import androidx.core.app.ActivityCompat; import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import androidx.media.MediaBrowserServiceCompat;
import com.muqingbfq.MyButtonClickReceiver; import com.muqingbfq.MyButtonClickReceiver;
import com.muqingbfq.R; import com.muqingbfq.R;
@ -26,12 +24,12 @@ import com.muqingbfq.home;
import com.muqingbfq.yc; import com.muqingbfq.yc;
public class NotificationManagerCompat { public class NotificationManagerCompat {
Service context; MediaBrowserServiceCompat context;
public NotificationCompat.Builder notificationBuilder; public NotificationCompat.Builder notificationBuilder;
public androidx.core.app.NotificationManagerCompat notificationManager; public androidx.core.app.NotificationManagerCompat notificationManager;
private String name, zz; private String name, zz;
public NotificationManagerCompat(Service context) { public NotificationManagerCompat(MediaBrowserServiceCompat context) {
this.context = context; this.context = context;
name = context.getString(R.string.app_name); name = context.getString(R.string.app_name);
zz = context.getString(R.string.zz); zz = context.getString(R.string.zz);
@ -75,7 +73,6 @@ public class NotificationManagerCompat {
.setContentIntent(pendingIntent) .setContentIntent(pendingIntent)
.setStyle(style); .setStyle(style);
tzl(); tzl();
// context.startForeground(1, notificationBuilder.build());
} catch (Exception e) { } catch (Exception e) {
yc.start(context, e); yc.start(context, e);
} }
@ -83,7 +80,6 @@ public class NotificationManagerCompat {
PendingIntent pendingIntent; PendingIntent pendingIntent;
androidx.media.app.NotificationCompat.MediaStyle style; androidx.media.app.NotificationCompat.MediaStyle style;
@SuppressLint("RestrictedApi") @SuppressLint("RestrictedApi")
public void tzl() { public void tzl() {
if (bfqkz.xm != null) { if (bfqkz.xm != null) {