问题一:Fragment中使用MainActivity中的静态对象失败
fragment与Activity之间值的传递不能直接取
用广播和接收器来完成传值的过程,代码如下:
发送方:
//用intent和bundle传值给fragment
Intent intent = new Intent("c20.hnucm.shouye");
Bundle bundle = new Bundle();
bundle.putSerializable("sy",shouyeItem);
intent.putExtras(bundle);
sendBroadcast(intent); //todo 发送广播
接收方:
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
shouyeItem = (ShouyeItem) bundle.getSerializable("sy");
Log.i("MyReceiver","接收信息:"+shouyeItem.getJdm());
imageView = view.findViewById(R.id.imageView11);
Glide.with(view).load(shouyeItem.getImg()).into(imageView);
jdm=view.findViewById(R.id.textView22);
jdm.setText(shouyeItem.getJdm());
}
};
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("c20.hnucm.shouye"); //todo 调频
getActivity().getApplicationContext().registerReceiver(broadcastReceiver,intentFilter);
问题二:RecycleView显示Item间距过大
最开始用的LinearLayoutManager,每页只能显示一个Item控件。后来改成了GridLayoutManager,可自行设置列数,但一页只显示所设列数个Item控件。解决方法:
Item可以把 页面填充满了,但是Item之间过于紧凑,所以写了一个SpaceItemDecoration继承ItemDecoration来控制Item间距。
代码如下:(没画分割线)
public class SpaceItemDecoration extends RecyclerView.ItemDecoration {
private int spacing; //间隔
public SpaceItemDecoration(int space){
this.spacing=space;
}
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
outRect.left=spacing;
outRect.right=spacing;
outRect.bottom=spacing;
}
}
问题三:java.lang.IllegalStateException: ScrollView can host only one direct child
ScrollView里面只能有一个控件,在ScrollView下面定义一个LinearLayout或ConstraintLayout等都可解决这个问题。
问题四:多个Fragment切换时,一个Fragment在再次被调用时,界面被重新加载
因为最开始用的是replace()方法来切换Fragment,该方法只是在上一个Fragment不再需要时采用的简便方法,弊端就是如果需要重复使用该fragment时,需要每次都要重新加载一次。会导致数据丢失。
解决方法:使用show()、hide()、add(),正确的切换方式是add(),切换时hide(),add()另一个Fragment;再次切换时,只需hide()当前,show()另一个就行了。代码如下:
(写在当前Activity的onCreate()方法里)
if (savedInstanceState != null) { // “内存重启”时调用
//从fragmentManager里面找到fragment
shouyeFragment = (ShouyeFragment) getSupportFragmentManager().findFragmentByTag(ShouyeFragment.class.getName());
findFragment = (FindFragment) getSupportFragmentManager().findFragmentByTag(FindFragment.class.getName());
xiaoxiFragment = (XiaoxiFragment) getSupportFragmentManager().findFragmentByTag(XiaoxiFragment.class.getName());
wo = (woFragment) getSupportFragmentManager().findFragmentByTag(woFragment.class.getName());
//解决重叠问题show里面可以指定恢复的页面
getSupportFragmentManager().beginTransaction()
.show(shouyeFragment)
.hide(findFragment)
.hide(xiaoxiFragment)
.hide(wo)
.commit();
//把当前显示的fragment记录下来
currentFragment = shouyeFragment;
}else{ //正常启动时调用
shouyeFragment = new ShouyeFragment();
xiaoxiFragment = new XiaoxiFragment();
findFragment = new FindFragment();
wo = new woFragment();
showFragment(shouyeFragment);
}
第二段代码:
private void showFragment(Fragment fg){
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
//如果之前没有添加过
if(!fg.isAdded()){
transaction
.hide(currentFragment)
.add(R.id.con,fg);
}else{
transaction
.hide(currentFragment)
.show(fg);
}
//全局变量,记录当前显示的fragment
currentFragment = fg;
transaction.commit();
}
问题五:RecycleView放在ScrollView里面,滑动的时候只有recycleview动,其他控件不动
不要把包裹recycleview的父布局高度定死,就可以实现所有控件一起滑动的效果。如下图,为包裹它的约束布局部分代码。(我是这样解决的,可能有点投机取巧了,因为查到的都是要重写一个啥啥的)
更正:应该是不要把recycleview的高度定死,上面的说法也没问题,父布局没定死,里面的只要是match_parent也是没定死的。
问题六:Button改不了颜色
修改themes.xml中的style处:
改前:<style name="Theme.WangZiyi" parent="Theme.MaterialComponents.DayNight.NoActionBar">
改后:<style name="Theme.WangZiyi" parent="Theme.MaterialComponents.DayNight.NoActionBar.Bridge">
问题七:沉浸式状态栏:
两个步骤:1、在图示位置加上两条代码
2、在布局文件中加入图示代码:
注:若只完成第一步,不修改布局文件,整个界面会上移一个状态栏的高度。
问题八:在RecycleView中更换显示内容
就是点击红框中的内容,使其以下页面的内容变化。
解决方法:第一步:获得要填充的数据
第二步:再重写对应的MyAdapter和ViewHolder。如下图:
第三步:在点击事件中重新给recycleview设置一个adapter:````bash
recyclerView.setAdapter(myAdapterXX);
## 问题九:网络请求,放fragment里面总是显示不出来
因为少了一句:`myAdapterXX.notifyDataSetChanged (); //刷新全部可见的Item`
## 问题十:分割线
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210617232009276.png)