Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unresolved class '@string/appbar_scrolling_view_behavior' in fragment results into behaviour OnCreate not fired

Please refer to my below fragment .XML

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/consulting_frame"
    tools:context=".fragments.ConsultingFragment">

    <!-- TODO: Update blank fragment layout -->
    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.design.widget.TabLayout
                android:id="@+id/consulting_tabs"
                app:tabSelectedTextColor="@color/colorPrimaryDark"
                app:tabIndicatorColor="@color/colorPrimary"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:tabMode="scrollable"/>
        </android.support.design.widget.AppBarLayout>

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            **app:layout_behavior="@string/appbar_scrolling_view_behavior"/>**

    </android.support.design.widget.CoordinatorLayout>

in the above file line

app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

displays error "Unresolved reference" in the editor, however it doesn't generate any compilation error.

I have searched net and stackoverflow to find solution, it has mentioned that

implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support:support-v4:28.0.0'

must be included in the app gradle file. I have checked it, and both files are there in my app gradle file.

I am not able to resolve the error. Please help with this error. Further to above error, I am also facing other erratic behaviour in my app, which is as explained below:

When my above viewpager (Containing error) is initialized, I am instantiating four tabs and where four fragments are opened. My fragment file is as below:

package com.mobiapp.hospmgmt.fragments;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.mobiapp.hospmgmt.R;
import com.mobiapp.hospmgmt.SessionHandler;
import com.mobiapp.hospmgmt.User;
import com.mobiapp.hospmgmt.helper.HospMgmtConstants;

import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class ConsultingFragment extends Fragment {

    private SessionHandler session;
    User user;

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

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

        getActivity().setTitle(HospMgmtConstants.TAG_CONSULTING);

        session = new SessionHandler(getContext());
        user = session.getUserDetails();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View view = inflater.inflate(R.layout.fragment_consulting, container, false);
        // Setting ViewPager for each Tabs
        ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager);
        setupViewPager(viewPager);
        // Set Tabs inside Toolbar
        TabLayout tabs = (TabLayout) view.findViewById(R.id.consulting_tabs);
        tabs.setupWithViewPager(viewPager);


        return view;

    }

    private void setupViewPager(ViewPager viewPager) {

        Adapter adapter = new Adapter(getChildFragmentManager());
        adapter.addFragment(new PatientConsultingFragment(), "Patient");
        adapter.addFragment(new DiagnosisConsultingFragment(), "Diagnosis");
        adapter.addFragment(new LabtestsConsultingFragment(), "Lab Tests");
        adapter.addFragment(new PrescribeConsultingFragment(), "Prescribe");
        viewPager.setAdapter(adapter);

    }

    static class Adapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public Adapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFragment(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }
}

Now, issue is along with first fragment, second fragment is also getting instantiated, and onCreate method is fired for both. However, after that even if, I click on tab-2 (Diagnosis), onCreate is not fired. However, for other two fragments (TAB-3 (Lab Tests) and TAB-4 (Prescribe)) every time onCreate is getting fired whenever those tabs are clicked.

I am failed to understand this erratic behaviour. Is this due to above Unresolved reference? I have tried to remove the line app:layout_behavior="@string/appbar_scrolling_view_behavior", but above erratic behaviour remains as it is.

So, please help me in resolving above mentioned unresolved reference error and also above erratic behaviour of fragment initialization.

I have even added <string name="appbar_scrolling_view_behavior" translatable="false">android.support.design.widget.AppBarLayout$ScrollingViewBehavior</string> in the strings.xml, but it has not resolved unresolved class error.

like image 844
falcon Avatar asked Apr 19 '19 08:04

falcon


1 Answers

For the app:layout_behavior, try:

app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"

(https://issuetracker.google.com/issues/131403371)

like image 114
user3951320 Avatar answered Sep 24 '22 13:09

user3951320