我想更改选项卡式活动中标签的标题。 目前使用以下代码我可以切换到选项卡,但无法在通知到达时更改选项卡标题。
如何更改标签的标题。
ViewPager viewPager = (ViewPager) AllTabs.mViewPager.findViewById(R.id.container);
int currentTab = viewPager.getCurrentItem();
viewPager.setCurrentItem(1);
以上代码有助于更改或切换到mytab。 请尝试以下代码检查标签的标题
PagerAdapter pagerAdapter = new PagerAdapter() {
@Override
public int getCount() {
return 0;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return false;
}
};
Log.e("ServerManager_Title",pagerAdapter.getPageTitle(1).toString());
pagerAdapter.notifyDataSetChanged();
但由于pagerAdapter与其投掷的任何标签都没有关联 调用任何方法时出现NullPointerException。
12-22 11:27:37.049 20448-20448/com.moodoff E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.moodoff, PID: 20448
java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.String java.lang.CharSequence.toString()' on a null object reference
at com.moodoff.helper.ServerManager$3.run(ServerManager.java:199)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
我的代码:在ServerManager.java文件中
final Activity currActivity = (Activity)context;
NotificationCompat.Builder builder =
new NotificationCompat.Builder(currActivity)
.setSmallIcon(R.drawable.btn_dedicate)
.setColor(001500)
.setContentTitle("MoodOff")
.setContentText(UserDetails.getUserName()+ "!! You got new notifications!!");
Intent notificationIntent;
if(currActivity==null){
notificationIntent = new Intent(currActivity, Start.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
}
else{
currActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
//Toast.makeText(context,"Hi New one",Toast.LENGTH_SHORT).show();
//designNotPanel(1);
ViewPager viewPager = (ViewPager) AllTabs.mViewPager.findViewById(R.id.container);
int currentTab = viewPager.getCurrentItem();
viewPager.
viewPager.getAdapter().notifyDataSetChanged();
}
});
notificationIntent = new Intent(currActivity, NotificationFragment.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
}
PendingIntent contentIntent = PendingIntent.getActivity(currActivity, 0, notificationIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
builder.setContentIntent(contentIntent);
builder.setAutoCancel(true);
}
在AllTabs.java中托管所有选项卡,或者是包含所有选项卡的主要活动 公共类AllTabs扩展AppCompatActivity实现ViewPager.OnPageChangeListener {
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
//private ViewPager mViewPager;
public static ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_all_tabs);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
//Request all the dangerous permissions over here
}
@Override
public void onPageScrollStateChanged(int state) {
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
阿米做正确的方法?在通知到达时,我想在我的第二个标签上显示一个计数(一个数字)(在我选择的三个标签中哪个标签不重要)这个数字是动态的,所以一旦通知出现我需要计算并显示,即根据动态计数将第二个标签的标题更改为新标题。
答案 0 :(得分:0)
1.activity应该实现<div *ngIf="planState==plan1">show plan 1</div>
2.创建<div [hidden]="planState!=plan1">show plan 1</div>
3.您可以使用OnPageChangeListener
4
ViewPager
然后viewPager.setOnPageChangeListener(this)
将不会 null
请参阅here
如果@Override
public abstract void onPageSelected(int position) {
setTitle(getTitleFromPosition(position));
}
由于不推荐使用而无效,请立即使用pagerAdapter.getPageTitle(1).toString()
。
例如,
ViewPager.setOnPageChangeListener
答案 1 :(得分:0)
作为@Charuka,这也是可能的,你也可以这样做。
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
这是设置ViewPager适配器的方法。
private void setupViewPager(ViewPager viewPager)
{
try
{
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new FragmentGeneral(), "GENERAL");
adapter.addFrag(new FragmentInterests(), "INTERESTS");
adapter.addFrag(new FragmentAlert(), "ALERTS");
adapter.addFrag(new FragmentSocial(), "SOCIAL");
viewPager.setAdapter(adapter);
}
catch (Exception e)
{
e.printStackTrace();
}
}
现在创建ViewPagerAdapter类,它处理你的片段和Tab标题
private class ViewPagerAdapter extends FragmentPagerAdapter
{
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
//here you can set your custom title by using position
if (position==1)
return "Title";
else
return mFragmentTitleList.get(position);
}
}