[Android] ViewPager 화면에 보일 때 호출 (setUserVisibleHint함수, mIsVisibleToUser변수)
2021. 4. 1. 08:18ㆍ모바일/Android_Java
public class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
private PageViewModel pageViewModel;
protected boolean mIsVisibleToUser;
public static PlaceholderFragment newInstance(int index) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle bundle = new Bundle();
bundle.putInt(ARG_SECTION_NUMBER, index);
fragment.setArguments(bundle);
return fragment;
}
int index = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pageViewModel = new ViewModelProvider(this).get(PageViewModel.class);
if (getArguments() != null) {
index = getArguments().getInt(ARG_SECTION_NUMBER);
}
pageViewModel.setIndex(index);
Log.d("jhy", "oncreate" + index);
}
@Override
public View onCreateView(
@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_main, container, false);
final TextView textView = root.findViewById(R.id.section_label);
pageViewModel.getText().observe(this, s -> textView.setText(s));
return root;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public void onStart() {
super.onStart();
if (mIsVisibleToUser) {
//화면에 보이는 page foreground 진입시 실행
Log.d("jhy", "onStart VISIBLE " + index);
} else {
//화면에 보이지 않는 page도 호출
Log.d("jhy", "onStart " + index);
}
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
mIsVisibleToUser = isVisibleToUser;
if (isResumed()) {
//Fragment 생성
if (mIsVisibleToUser) {
//화면에 보이고 싶을 때 로딩하고 싶다면 여기서
Log.d("jhy", "setUserVisibleHint VISIBLE " + index);
} else {
//pager의 index가 바뀔 때 실행
Log.d("jhy", "setUserVisibleHint INVISIBLE " + index);
}
}
}
@Override
public void onStop() {
super.onStop();
if (mIsVisibleToUser) {
//화면에 보이는 page background로 내리면 실행
Log.d("jhy", "onStop mIsVisibleToUser true " + index);
} else {
//현재 보이지 않는 page도 호출
Log.d("jhy", "onStop mIsVisibleToUser false " + index);
}
}
@Override
public void onResume() {
super.onResume();
Log.d("jhy", "onResume " + index);
}
@Override
public void onPause() {
super.onPause();
Log.d("jhy", "onPause " + index);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("jhy", "onDestroy " + index);
}
}
'모바일 > Android_Java' 카테고리의 다른 글
[Android] pdf open with instantly(immediatly) close error + Failed to find configured root that contains /data~ (0) | 2021.04.14 |
---|---|
[Android] 스크롤 하면서 라디오버튼 체크 변경, 체크 클릭시 하면 해당 스크롤로 가기 (0) | 2021.04.13 |
[java] sns 형태 시간 (몇분전)으로 변경하기 (0) | 2021.03.26 |
curl to java code (HttpURLConnection, URL, JSONparser,JSONObject) (0) | 2021.03.10 |
[Android] onNewIntent, onSaveInstanceState (0) | 2021.03.10 |