我有这个片段。
public class USATodayFragment extends Fragment implements LoaderManager.LoaderCallbacks<Object> {
private Boolean isStarted = false;
private Boolean isVisible = false;
private static final String LOG_TAG = USATodayFragment.class.getName();
private static final int NEWS_LOADER_ID = 0;
private String newsUrl;
private RecyclerView mRecyclerView;
private NewsAdapter adapter;
private TextView errorTextView;
private static final String ARRAY_LIST = "list_view";
private ArrayList<News> newsArrayList;
private static final String LAYOUT_STATE = "USATodayFragment.recycler.layout";
String newsTitlesToJson;
public USATodayFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.common_fragment_layout, container, false);
if(savedInstanceState == null) {
newsArrayList = new ArrayList<>();
mRecyclerView = v.findViewById(R.id.news_recycler_view);
errorTextView = v.findViewById(R.id.errorTextView);
LinearLayoutManager manager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(manager);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.addItemDecoration(new SimpleDividerItemDecoration(getActivity()));
adapter = new NewsAdapter(getActivity(), newsArrayList);
mRecyclerView.setAdapter(adapter);
getLoaderManager().initLoader(NEWS_LOADER_ID, null, this);
UpdateNewsWidgetService.startBakingService(getContext(), (ArrayList<News>) newsArrayList);
}else{
newsArrayList = savedInstanceState.getParcelableArrayList(ARRAY_LIST);
errorTextView = v.findViewById(R.id.errorTextView);
mRecyclerView = v.findViewById(R.id.news_recycler_view);
LinearLayoutManager manager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(manager);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.addItemDecoration(new SimpleDividerItemDecoration(getActivity()));
adapter = new NewsAdapter(getActivity(), newsArrayList);
mRecyclerView.setAdapter(adapter);
UpdateNewsWidgetService.startBakingService(getContext(), (ArrayList<News>) newsArrayList);
}
return v;
}
@Override
public Loader onCreateLoader(int id, Bundle args) {
if(id == NEWS_LOADER_ID){
newsUrl = "https://newsapi.org/v2/top-headlines?sources=usa-today&apiKey=98b995b151264acdb35e751ff6d22a3c";
}
return new USATodayLoader(getActivity(),newsUrl);
}
@Override
public void onLoaderReset(Loader<Object> loader) {
}
@Override
public void onLoadFinished(Loader<Object> loader, Object data) {
int id = loader.getId();
if(id == NEWS_LOADER_ID){
newsArrayList = (ArrayList<News>)data;
if(newsArrayList !=null && !newsArrayList.isEmpty()){
adapter.clear();
adapter.setNewsData(newsArrayList);
newsTitlesToJson = new Gson().toJson(newsArrayList);
PreferenceManager.getDefaultSharedPreferences(getActivity())
.edit().putString("news",newsTitlesToJson)
.apply();
Log.v(LOG_TAG, newsTitlesToJson);
}else{
showError();
}
}
}
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelableArrayList(ARRAY_LIST,newsArrayList);
//outState.putParcelable(LAYOUT_STATE, mRecyclerView.getLayoutManager().onSaveInstanceState());
}
@Override
public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState);
if(savedInstanceState != null)
{
Parcelable savedRecyclerLayoutState = savedInstanceState.getParcelable(LAYOUT_STATE);
mRecyclerView.getLayoutManager().onRestoreInstanceState(savedRecyclerLayoutState);
}
}
public void showError(){
mRecyclerView.setVisibility(View.GONE);
errorTextView.setVisibility(View.VISIBLE);
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
}
}
基本上,它会在 onCreateLoader 中加载一些数据,并将其显示在 onLoadFinished 中。我想要做的就是如果Fragment可见,请求就完成了。但是当我使用 setUserVisibleHint()
时,我很困惑对此有何帮助?
谢谢,
西奥。
答案 0 :(得分:4)
在Fragment setUserVisibleHint()
中将调用before oncreate方法。
您可以声明全局变量并在片段
中调用onCreateView()
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
//inflate view layout
view =inflater.inflate(R.layout.your_fragment, container, false);
// return view
return view;
}
然后你可以像那样打电话给setUserVisibleHint()
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
// check condition
if (isResumed() && isVisibleToUser) {
//do stuff
}
}
我希望这会对你有所帮助!
答案 1 :(得分:-1)
% gcc strncpy.c -Wall -Wextra
% ./a.out
buf1 val: >AB <
buf1 addr: 0x7ffd7f6aecc5
buf1 val: >XYZ<