我试图使用tabView。当我滑动页面时,页面不会与标签一起滑动。无法解决问题。请帮助我。
我的活动代码:
public class Notify extends AppCompatActivity implements ActionBar.TabListener{
String contentText="I'm using android notification text." + "It's working fine, but the problem is that we have to create a big textview";
TabLayout tabLayout;
private String[] frag={"Books","Canvas","Fragments","Needed"};
ViewPager viewPager;
ActionBar a;
DemoCollectionPagerAdapter mDemoCollectionPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notification);
tabLayout = (TabLayout) findViewById(R.id.tab);
viewPager = (ViewPager) findViewById(R.id.viewPager);
a = getSupportActionBar();
a.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// tabLayout.addTab(tabLayout.newTab().setText("Books"));
//tabLayout.addTab(tabLayout.newTab().setText("Canvas"));
//tabLayout.addTab(tabLayout.newTab().setText("Fragments"));
//tabLayout.addTab(tabLayout.newTab().setText("Needed"));
for (String tab_name : frag) {
a.addTab(a.newTab().setText(tab_name)
.setTabListener(this));
}
tabLayout.setSelectedTabIndicatorColor(Color.CYAN);
tabLayout.setupWithViewPager(viewPager);
viewPager.setAdapter(mDemoCollectionPagerAdapter);
mDemoCollectionPagerAdapter = new DemoCollectionPagerAdapter(getSupportFragmentManager());
Log.i("TAG", "message");
// getSupportActionBar().hide();
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
a.setSelectedNavigationItem(position);
mDemoCollectionPagerAdapter.notifyDataSetChanged();
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
public void onClick(View view){
NotificationCompat.Builder builder= new NotificationCompat.Builder(Notify.this);
builder.setContentTitle("Notify me");
builder.setContentText(contentText);
builder.setSmallIcon(R.drawable.images);
builder.setTicker("Hey! You got it");
builder.setAutoCancel(true);
Intent i=new Intent(Notify.this,Newactivity.class);
TaskStackBuilder stackBuilder=TaskStackBuilder.create(this);
stackBuilder.addParentStack(Newactivity.class);
stackBuilder.addNextIntent(i);
PendingIntent pendingIntent=stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
//Settings
Intent j=new Intent(Notify.this,Setting.class);
TaskStackBuilder stackBuilder_settings=TaskStackBuilder.create(this);
stackBuilder_settings.addParentStack(Setting.class);
stackBuilder_settings.addNextIntent(j);
PendingIntent pendingIntent_settings=stackBuilder_settings.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
//help
Intent k=new Intent(Notify.this,Help.class);
TaskStackBuilder stackBuilder_help=TaskStackBuilder.create(this);
stackBuilder_help.addParentStack(Help.class);
stackBuilder_help.addNextIntent(k);
PendingIntent pendingIntent_help=stackBuilder_help.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
builder.addAction(R.drawable.images,"Settings",pendingIntent_settings);
builder.addAction(R.drawable.images,"Help",pendingIntent_help);
Notification notification= builder.build();
NotificationManager manager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
manager.notify(1234, notification);
}
public void bigText(View view) {
NotificationCompat.BigTextStyle style= new NotificationCompat.BigTextStyle();
style.setBigContentTitle("Big Notification");
style.bigText(contentText);
NotificationCompat.Builder builder= new NotificationCompat.Builder(this);
builder.setContentText(contentText);
builder.setContentTitle("Big Notification");
builder.setSmallIcon(R.drawable.images);
builder.setAutoCancel(true);
builder.setStyle(style);
Notification notification= builder.build();
NotificationManager manager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
manager.notify(567, notification);
}
public void bigpicture(View view) {
Bitmap bmp= BitmapFactory.decodeResource(getResources(),R.drawable.one);
NotificationCompat.BigPictureStyle style=new NotificationCompat.BigPictureStyle();
style.setBigContentTitle("Big Picture");
style.setBigContentTitle(contentText);style.bigPicture(bmp);
NotificationCompat.Builder builder= new NotificationCompat.Builder(this);
builder.setContentText(contentText);
builder.setContentTitle("Big Notification");
builder.setSmallIcon(R.drawable.images);
builder.setAutoCancel(true);
builder.setStyle(style);
Notification notification= builder.build();
NotificationManager manager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
manager.notify(111, notification);
}
public void inboxStyle(View view) {
NotificationCompat.InboxStyle style =new NotificationCompat.InboxStyle();
style.setBigContentTitle("Inbox style");
style.addLine("Line one");
style.addLine("Line two");
style.addLine("Line three");
NotificationCompat.Builder builder= new NotificationCompat.Builder(this);
builder.setContentText(contentText);
builder.setContentTitle("Big Notification");
builder.setSmallIcon(R.drawable.images);
builder.setAutoCancel(true);
builder.setStyle(style);
Notification notification= builder.build();
NotificationManager manager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
manager.notify(279, notification);
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
}
我的适配器代码:
public class DemoCollectionPagerAdapter extends FragmentStatePagerAdapter {
public DemoCollectionPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
BooksFragment myFragment = new BooksFragment();
Bundle data = new Bundle();
data.putInt("current_page", position+1);
myFragment.setArguments(data);
return myFragment;
case 1:
return new Fragment1();
case 2:
return new Fragment2();
case 3:
return new Fragment2();
}
return null;
}
@Override
public int getCount() {
return 4;
}
private class BooksFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View root= inflater.inflate(R.layout.splashscreen,container, false);
ImageView imageView= (ImageView) root.findViewById(R.id.imageView);
return root;
}
}
}
答案 0 :(得分:0)
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"/>
我从这里得到了这个。新材料设计http://www.androidhive.info/2015/09/android-material-design-working-with-tabs/希望它可以帮助您