[英]setTitle of collapsing toolbar in other method where it is initialized
In my Fragment I have a CollapsingToolbar and it is created it in the onCreateView()
method:在我的片段中,我有一个 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;
}
Since my title can change in other methods, I tried to create a new method that shall set a new Title of the CollapsingToolbar, but it says cannot resolve symbol 'collapsingToolbarLayout'
.由于我的标题可以在其他方法中更改,我尝试创建一个新方法来设置 CollapsingToolbar 的新标题,但它说
cannot resolve symbol 'collapsingToolbarLayout'
。 I tried it like this:我是这样试的:
public void setTitle(String passedTitle){
collapsingToolbarLayout.setTitle(passedTitle);
}
What do I have to change so I can rename the title from other methods?我必须更改什么才能从其他方法重命名标题?
Edit:编辑:
This is the class where I use the setTitle()
method.这是我使用
setTitle()
方法的类。 I try to use it from an inner Class, I tried to cut it as well as possible:我尝试从内部类使用它,我试图尽可能地削减它:
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);
}
}
} }
And the 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);
}
} }
Root cause: Because the collapsingToolbarLayout
variable is local in onCreate
method, that why setTitle
cannot access it.根本原因:因为
collapsingToolbarLayout
变量在onCreate
方法中是本地的,所以setTitle
无法访问它。
Solution: Make collapsingToolbarLayout
as a variable of your Fragment.解决方案:将
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);
}
Update: I see the problem, change the code in CallsFragment更新:我看到了问题,更改CallsFragment 中的代码
From从
collapsingToolbarLayout = (CollapsingToolbarLayout) getActivity().findViewById(R.id.toolbar_layout);
AppBarLayout appBarLayout = (AppBarLayout) getActivity().findViewById(R.id.appBar);
To到
collapsingToolbarLayout = (CollapsingToolbarLayout) view.findViewById(R.id.toolbar_layout);
AppBarLayout appBarLayout = (AppBarLayout) view.findViewById(R.id.appBar);
Note: When you want to access a view from activity that contains current fragment you can use.注意:当您想从包含当前片段的活动访问视图时,您可以使用。
View view = getActivity().findViewById(R.id.this_view_inside_activity);
If you want to access a view inside the fragment then use.如果要访问片段内的视图,请使用。
// 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.