fragment是什么
fragment它自己的中文意思:碎片;
一个可以将activity拆分成几个完全独立封装的可重用的组件,每个组件有自己的生命周期和ui布局。
用fragment能解决什么问题?
总的来说,Fragment和Activity的生命周期类似。需要注意的是,它相比于Activity,多了onAttach(), onDetch(), onCreateView()和onDestroyView()这几个回调函数;但是,却少了onRestart()。 Fragment的生命周期非常复杂,分为以下几种情况:
如果是通过XML中的标签实例化的,那么第一个收到的回调将是onInflate 如果setRetainInstance(true) 当Activity重建时,Fragment的onDestroy以及Activity重建后Fragment的onCreate回调不会被调用.(无论是否将其添加到了返回栈)
如果当前显示的是Fragment A,然后执行FragmentTransaction.replace(),那么Fragment A会执行onPause()->onStop()->onDestroyView()->onDestroy()->onDetach()
如果执行FragmentTransaction.replace().addToBackStack(),那么Fragment A会执行onPause()->onStop()->onDestroyView() FragmentTransaction.hide(),将不会导致onPause(),而是会触发onHiddenChanged() FragmentTransaction.detach(),会导致onPause()->onStop()->onDestroyView(),注意:onDestroy()和onDetach()不会调用
activity中加载fragment的控件
fragment.getView().findViewById(id)来获取fragment中的控件。
在fragment 中调用activity中的控件
前提条件是这个fragment是装在这个activity中的,还有一种情况是fragment的嵌套的时候也是 可以的。
首先把activity中的控件声明为public
在fragment中这样调用:
//获取上下文,在这里就是只Activity, MainActivity mainActivity = (MainActivity) getActivity(); 使用Context调用控件; mainActivity.mIvAction.setVisibility(View.GONE);
原理就是: fragment是嵌套在Activity的,activity就是桌子,Fragment就是抽屉,可以用父子继承关系看待;
上面第一句就是在获取上下文,在这里就是只Activity,
第二句就是使用Context调用控件;
根本原因是:所有的控件都是继承Context的
WebView加载不出Html5网页的解决方法
wvPrivatePolicy.setWebViewClient(new WebViewClient(){ // 在app内部加载网页 public boolean shouldOverrideUrlLoading(final WebView view, final String url) { view.loadUrl(url); return true; }//重写点击动作,用webview载入 }); wvPrivatePolicy.loadUrl(“www.lyzhongxin.cn/m/view.php?…WebView webView = findViewById(R.id.myWebView); WebSettings webSettings = webView.getSettings(); webSettings.setJavaScriptEnabled(true); webSettings.setUseWideViewPort(true);/设置此属性,可任意比例缩放 webSettings.setLoadWithOverviewMode(true); webSettings.setDomStorageEnabled(true);//不添加可能加载H5的页面时候出现一片空白,对某些标签的不支持webSettings.setBlockNetworkImage(true);//设置显示图片
android studio 拾色器
androidx ViewPager高度无法wrap_content问题
package com.base.emergency_bureau.view; import android.content.Context; import android.util.AttributeSet; import android.view.View; import androidx.viewpager.widget.ViewPager; /** * @ProjectName: an-kefu * @Package: com.base.emergency_bureau.view * @ClassName: WrapContentHeightViewPager * @Description: java类作用描述 * @Author: liys * @CreateDate: 2021/5/31 17:26 * @UpdateUser: 更新者 * @UpdateDate: 2021/5/31 17:26 * @UpdateRemark: 更新说明 * @Version: 1.0 */ public class WrapContentHeightViewPager extends ViewPager { /** * Constructor * * @param context the context */ public WrapContentHeightViewPager(Context context) { super(context); } /** * Constructor * * @param context the context * @param attrs the attribute set */ public WrapContentHeightViewPager(Context context, AttributeSet attrs) { super(context, attrs); } // @Override // protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // super.onMeasure(widthMeasureSpec, heightMeasureSpec); // // // find the first child view // View view = getChildAt(0); // if (view != null) { // // measure the first child view with the specified measure spec // view.measure(widthMeasureSpec, heightMeasureSpec); // } // // setMeasuredDimension(getMeasuredWidth(), measureHeight(heightMeasureSpec, view)); // } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int height = 0; for(int i = 0; i < getChildCount(); i++) { View child = getChildAt(i); child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); int h = child.getMeasuredHeight(); if(h > height) height = h; } heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); super.onMeasure(widthMeasureSpec, heightMeasureSpec); } /** * Determines the height of this view * * @param measureSpec A measureSpec packed into an int * @param view the base view with already measured height * * @return The height of the view, honoring constraints from measureSpec */ private int measureHeight(int measureSpec, View view) { int result = 0; int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec); if (specMode == MeasureSpec.EXACTLY) { result = specSize; } else { // set the height from the base view if available if (view != null) { result = view.getMeasuredHeight(); } if (specMode == MeasureSpec.AT_MOST) { result = Math.min(result, specSize); } } return result; } }
Android viewpage 设定上一页下一页按钮
接口回调回去当前viewpage 页码
package com.base.emergency_bureau.ui.module.question.fragment; /** * @ProjectName: emergency_bureau * @Package: com.base.emergency_bureau.ui.module.question.fragment * @ClassName: getCurrentPage * @Description: java类作用描述 * @Author: liys * @CreateDate: 2021/5/17 14:55 * @UpdateUser: 更新者 * @UpdateDate: 2021/5/17 14:55 * @UpdateRemark: 更新说明 * @Version: 1.0 */ public interface getCurrentPage { int getPage( int page); }
public class AnswerActivity extends FragmentActivity implements getCurrentPage {
@Override public int getPage(int getPage) { currentPage = getPage; switch (getPage) { case 1: tvSubmit.setVisibility(View.GONE); btNext.setVisibility(View.VISIBLE); llQuestionNextLast.setVisibility(View.VISIBLE); btLast.setVisibility(View.GONE); break; case 2: tvSubmit.setVisibility(View.GONE); btNext.setVisibility(View.VISIBLE); btLast.setVisibility(View.VISIBLE); llQuestionNextLast.setVisibility(View.VISIBLE); break; case 3: tvSubmit.setVisibility(View.GONE); btNext.setVisibility(View.VISIBLE); btLast.setVisibility(View.VISIBLE); llQuestionNextLast.setVisibility(View.VISIBLE); break; case 4: tvSubmit.setVisibility(View.GONE); btNext.setVisibility(View.VISIBLE); btLast.setVisibility(View.VISIBLE); llQuestionNextLast.setVisibility(View.VISIBLE); break; case 5: btNext.setVisibility(View.GONE); btLast.setVisibility(View.GONE); llQuestionNextLast.setVisibility(View.GONE); tvSubmit.setVisibility(View.VISIBLE); break; default: break; } return 0; }
在Fragment 中设定事件触发(重写方法:setUserVisibleHint 在当前页面可见情况下)
@Override public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); if(isVisibleToUser){ getCurrentPage.getPage(5); } else { } }