我有一个功能齐全的回收站视图,当应用程序打开时,该视图作为导航抽屉的一部分打开。我更改了一些代码,并且无意中放错了一些代码,现在打开导航抽屉时,该片段为空白,直到我单击该片段以再次在抽屉中打开它是否显示了recyclerview及其内容。我可以得到一些帮助来确定缺少的代码段吗?
我有一段时间没接触过这段代码了,因为我认为它运行良好,所以我无法再看到代码了,但是我仔细阅读了我用于导航抽屉和recyclerview的教程,以确保他们将单独工作,而且看起来我已经掌握了一切。我检查了一些有关Stack溢出的问题,但它们似乎仅对应于Firebase recyclerview,并没有真正回答我的特定问题。
此页面是初始化抽屉的位置,应调用recyclerview。
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.front_page);
android.support.v7.widget.Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer_front_page);
NavigationView nV = findViewById(R.id.drawer_view);
View header = nV.getHeaderView(0);
nV.setNavigationItemSelectedListener(this);
callbackManager = CallbackManager.Factory.create();
connMgr = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
drawer.openDrawer(Gravity.START);
toggle.syncState();
nV.setCheckedItem(R.id.list_venue);
/*ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if(!networkInfo.isConnected()){
}*/
//TODO: Create network status check with animation and Dialog
sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
isLoggedOut = sharedPreferences.getBoolean(LOGINSTATUS,false);
drawerProfilePicture = header.findViewById(R.id.drawer_pic_profile);
drawerUserName = header.findViewById(R.id.drawer_profile_username);
drawerUserEmail = header.findViewById(R.id.drawer_profile_email);
if (AccessToken.getCurrentAccessToken() != null && !AccessToken.getCurrentAccessToken().isExpired()) {
facebookLogin();
} else {
inHouseSignUp();
}
}
这是recyclerview
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.menu_venue, container, false);
if (!Places.isInitialized()) {
Places.initialize(contextForPlaces, "AIzaSyCKGd3fqmtsDklRGMhnkuIy1GS-j6gRBh8");
}
RecyclerView vRV = view.findViewById(R.id.view_venue);
vRV.setHasFixedSize(true);
RecyclerView.LayoutManager vLM = new LinearLayoutManager(this.getActivity());
vAdapter = new VenueAdapter(vIL);
vAdapter.setOnItemClickListener(this::shareTheView);
placesClient = Places.createClient(contextForPlaces);
//TODO: Make App mention when there is no service
//TODO Figure out animation for scrolling
//TODO: add code to make sure that the VenueList is complete before displaying it
//look for transfering keys for the information instead of the information itself
//TODO: E/RecyclerView: No adapter attached; skipping layout
for (String club : clubs) {
FetchPlaceRequest request = FetchPlaceRequest.newInstance(club, placeFields);
placesClient.fetchPlace(request).addOnSuccessListener((response) -> {
Place place = response.getPlace();
PhotoMetadata photoMetadata = Objects.requireNonNull(place.getPhotoMetadatas()).get(0);
//String attributions = photoMetadata.getAttributions();
FetchPhotoRequest photoRequest = FetchPhotoRequest.builder(photoMetadata).setMaxHeight(400).setMaxWidth(400).build();
placesClient.fetchPhoto(photoRequest).addOnSuccessListener((fetchPhotoResponse) -> {
Bitmap bitmap = fetchPhotoResponse.getBitmap();
vIL.add(new VenueItem( /*Photo*/bitmap, place.getName()));
//Log.i(TAG, "Photo Should Be Up");
vRV.setLayoutManager(vLM);
vRV.setAdapter(vAdapter);
}).addOnFailureListener((exception) -> {
if (exception instanceof ApiException) {
ApiException apiException = (ApiException) exception;
int statusCode = apiException.getStatusCode();
// Handle error with given status code.
Log.e(TAG, "Place not found: " + exception.getMessage());
}
});
Log.i(TAG, "Place found: " + place.getName());
}).addOnFailureListener((exception) -> {
if (exception instanceof ApiException) {
ApiException apiException = (ApiException) exception;
int statusCode = apiException.getStatusCode();
// Handle error with given status code.
Log.e(TAG, "Place not found: " + exception.getMessage());
}
});
}
vAdapter.notifyDataSetChanged();
return view;
}
所有代码都可以使用,但是在首次调用时无法打开,而我需要手动调用它,以前从未发生过。
答案 0 :(得分:0)
在for循环之后尝试一下
for(String club : clubs){}
vAdapter = new VenueAdapter(vIL);
vRV.setLayoutManager(vLM);
vRV.setAdapter(vAdapter);
答案 1 :(得分:0)
我找到了要擦除的代码。这段代码指示了如果没有可用的片段,它将在启动时启动
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new VenueList()).commit();
nV.setCheckedItem(R.id.list_venue);
}