【问题标题】:Navigation drawer does not automatically start the recyclerview fragment导航抽屉不会自动启动 recyclerview 片段
【发布时间】:2019-06-17 06:26:45
【问题描述】:

我有一个功能齐全的回收器视图,当应用打开时,它会作为导航抽屉的片段打开。我更改了一些代码并无意中放错了一些代码,现在当导航抽屉打开时,片段是空白的,直到我单击片段以在抽屉中再次打开它是否显示 recyclerview 及其内容。我可以得到一些帮助来确定丢失的代码吗?

我有一段时间没有接触这段代码了,因为我认为它运行良好,所以我无法再将代码可视化,但我浏览了用于导航抽屉和回收器视图的教程以确保他们将单独工作,看起来我拥有一切。我检查了一些关于堆栈溢出的问题,但它们似乎只对应于 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;
    }

所有代码都可以运行,但第一次调用时打不开,我需要手动调用它,这是以前从未发生过的。

【问题讨论】:

    标签: android android-recyclerview navigation-drawer


    【解决方案1】:

    在for循环之后试试这个

    for(String club : clubs){}
    vAdapter = new VenueAdapter(vIL);
    vRV.setLayoutManager(vLM);
    vRV.setAdapter(vAdapter);
    

    【讨论】:

    • 那行不通,因为每次迭代都需要访问 recyclerview,因为它应该是 google api 的最终版本。 (我可能弄错了原因,但适配器的代码需要在那里。)我想通了,你的帮助。我过于热心地清理了一些代码,我删除了几行指示在启动时从哪个片段开始
    【解决方案2】:

    我找到了我删除的代码。如果没有可用的 Fragment,这段代码指示启动时启动哪个 Fragment

    if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new VenueList()).commit();
            nV.setCheckedItem(R.id.list_venue);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-16
      • 2016-01-03
      相关资源
      最近更新 更多