gpt4 book ai didi

android - onCreate中使用getActivity时,如何让onCreate在onCreateView之前加载?

转载 作者:行者123 更新时间:2023-11-29 23:25:22 27 4
gpt4 key购买 nike

我有一个使用 GooglePlayAPI 获取位置的代码。在那段代码中,我使用了 getAcvitity,因为这是一个 fragment ,我把它放在我的 OnCreate 中。因为我希望在 recyclerview 中显示这些值,所以我将那部分代码放在了 onCreateView 中。不幸的是,位置代码导致我的 onCreate 在我的 onCreateView 之后加载,因此我有一个没有值的 recyclerview。我试图将我的位置代码放在我的 onCreateView 中,但这不起作用。有谁知道我的问题的解决方案?这是我的代码。

public class TestFragment extends Fragment {
private double latitude;
private static double longitude;

private FusedLocationProviderClient mFusedLocationClient;


RecyclerView mRecyclerView;
PlanetRecyclerViewAdapter adapter;
private OnFragmentInteractionListener mListener;

public TestFragment() {
// Required empty public constructor
}


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);

requestPermission();

mFusedLocationClient = LocationServices.getFusedLocationProviderClient(getActivity());

if (ActivityCompat.checkSelfPermission(getActivity(), ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getActivity(), "Please provide Location Permission to continue ", Toast.LENGTH_LONG).show();
return;
}
mFusedLocationClient.getLastLocation().addOnSuccessListener(getActivity(), new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {

if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();

System.out.println("This is latitude:" + latitude);



}

}
});


if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);



}


}


private void requestPermission(){
ActivityCompat.requestPermissions(getActivity(),new String[]{ACCESS_FINE_LOCATION}, 1);
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_test, container, false);



mRecyclerView = (RecyclerView)view.findViewById(R.id.planet_recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mRecyclerView.addItemDecoration(new DividerItemDecoration(mRecyclerView.getContext(),DividerItemDecoration.VERTICAL));

adapter = new PlanetRecyclerViewAdapter(items, mRecyclerView.getContext());
mRecyclerView.setAdapter(adapter);




return view;
}




@Override
public void onAttach(Context context) {
super.onAttach(context);

if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}

@Override
public void onDetach() {
super.onDetach();
mListener = null;
}

public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}

最佳答案

getLastLocation() 是异步代码,因此它发生在 onCreateView 之后。尝试像这样将 getLastLocation() 放在 onCreateView 中:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_test, container, false);



mRecyclerView = (RecyclerView)view.findViewById(R.id.planet_recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mRecyclerView.addItemDecoration(new DividerItemDecoration(mRecyclerView.getContext(),DividerItemDecoration.VERTICAL));

mFusedLocationClient.getLastLocation().addOnSuccessListener(getActivity(), new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {

if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();

System.out.println("This is latitude:" + latitude);
adapter = new PlanetRecyclerViewAdapter(items, mRecyclerView.getContext());
mRecyclerView.setAdapter(adapter);


}

}
});




return view;
}

只有当您收到位置时,您才会设置适配器并将其附加到 recyclerview。我没有在您的代码中找到您为适配器声明项目的位置。

关于android - onCreate中使用getActivity时,如何让onCreate在onCreateView之前加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53688736/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com