折叠工具栏的setTitle,以其他方法初始化

时间:2018-08-26 11:04:06

标签: java android

在我的片段中,我有一个CollapsingToolbar,它是通过onCreateView()方法创建的:

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

CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) getActivity().findViewById(R.id.toolbar_layout);
AppBarLayout appBarLayout = (AppBarLayout) getActivity().findViewById(R.id.appBar);

return view;
}

由于我的标题可以用其他方法更改,因此我尝试创建一个新方法,该方法将设置CollapsingToolbar的新标题,但是它显示cannot resolve symbol 'collapsingToolbarLayout'。我这样尝试过:

public void setTitle(String passedTitle){
    collapsingToolbarLayout.setTitle(passedTitle);
}

我需要更改什么才能从其他方法重命名标题?

编辑:

这是我使用setTitle()方法的类。我尝试从内部类使用它,我尝试尽可能地减少它:

public class CallsFragment extends Fragment {

String title;
private CollapsingToolbarLayout collapsingToolbarLayout;

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


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_calls, container, false);

    collapsingToolbarLayout = (CollapsingToolbarLayout) getActivity().findViewById(R.id.toolbar_layout);
    AppBarLayout appBarLayout = (AppBarLayout) getActivity().findViewById(R.id.appBar);

    return view;
}


public void setTitle(String passedTitle){
    collapsingToolbarLayout.setTitle(passedTitle);
}


public class GetContacts extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

    @Override
    protected Void doInBackground(Void... arg0) {

    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);

    //here the title string gets filled     
            Log.d("place", title);
            setTitle(title);
}
}

}

和MainActivity:

public class MainActivity extends AppCompatActivity implements Serializable {

private ViewPager viewPager;
BottomNavigationView bottomNavigationView;
ChatFragment chatFragment;
CallsFragment callsFragment;
ContactsFragment contactsFragment;
MenuItem prevMenuItem;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Initializing viewPager
    viewPager = (ViewPager) findViewById(R.id.viewpager);

    //Initializing the bottomNavigationView
    bottomNavigationView = (BottomNavigationView)findViewById(R.id.bottom_navigation);

    bottomNavigationView.setOnNavigationItemSelectedListener(
            new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    switch (item.getItemId()) {
                        case R.id.navigation_umkreis:
                            viewPager.setCurrentItem(0);
                            break;
                        case R.id.navigation_karte:
                            viewPager.setCurrentItem(1);
                            break;
                        case R.id.navigation_einstellungen:
                            viewPager.setCurrentItem(2);
                            break;
                    }
                    return false;
                }
            });


    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            if (prevMenuItem != null) {
                prevMenuItem.setChecked(false);
            }
            else
            {
                bottomNavigationView.getMenu().getItem(0).setChecked(false);
            }
            Log.d("page", "onPageSelected: "+position);
            bottomNavigationView.getMenu().getItem(position).setChecked(true);
            prevMenuItem = bottomNavigationView.getMenu().getItem(position);

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });


    setupViewPager(viewPager);
}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    callsFragment = new CallsFragment();
    chatFragment = new ChatFragment();
    contactsFragment = new ContactsFragment();
    adapter.addFragment(callsFragment);
    adapter.addFragment(chatFragment);
    adapter.addFragment(contactsFragment);
    viewPager.setAdapter(adapter);
    int limit = adapter.getCount();
    viewPager.setOffscreenPageLimit(limit);
}

}

1 个答案:

答案 0 :(得分:1)

根本原因:由于collapsingToolbarLayout变量在onCreate方法中是局部变量,因此setTitle无法访问它的原因。

解决方案:将collapsingToolbarLayout设置为Fragment的变量。

private CollapsingToolbarLayout collapsingToolbarLayout;

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

    collapsingToolbarLayout = (CollapsingToolbarLayout) getActivity().findViewById(R.id.toolbar_layout);
    AppBarLayout appBarLayout = (AppBarLayout) getActivity().findViewById(R.id.appBar);

    return view;
}

public void setTitle(String passedTitle){
    collapsingToolbarLayout.setTitle(passedTitle);
}

更新:我看到了问题,请更改 CallsFragment

中的代码

来自

collapsingToolbarLayout = (CollapsingToolbarLayout) getActivity().findViewById(R.id.toolbar_layout);
AppBarLayout appBarLayout = (AppBarLayout) getActivity().findViewById(R.id.appBar);

收件人

collapsingToolbarLayout = (CollapsingToolbarLayout) view.findViewById(R.id.toolbar_layout);
AppBarLayout appBarLayout = (AppBarLayout) view.findViewById(R.id.appBar);

注意::要从包含当前片段的活动访问视图时,可以使用。

View view = getActivity().findViewById(R.id.this_view_inside_activity);

如果要访问片段内部的视图,请使用。

// Inflate the layout for this fragment
View fragmentRootView = inflater.inflate(R.layout.fragment_calls, container, false);
View view = fragmentRootView.findViewById(R.id.this_view_inside_fragment);