我需要为自定义标签布局提供水平滑动导航。我认为此代码段可以帮助您解决问题。 viewpageradapter附加了3个片段。我已经声明了Viewpager类的viewpager对象,但问题仍然存在。
public class DashboardActivity extends AppCompatActivity {
// private SectionsPagerAdapter mSectionsPagerAdapter;
// private ViewPager mViewPager;
ViewPagerAdapter viewPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Toolbar");
setSupportActionBar(toolbar);
TabLayout tabLayout;
ViewPager viewPager;
tabLayout=(TabLayout) findViewById(R.id.tabs);
viewPager=(ViewPager)findViewById(R.id.ViewPager);
viewPager= new ViewPagerAdapter(getSupportFragmentManager());
viewPagerAdapter.addFragments(new HomeFragment(),"HOME");
viewPagerAdapter.addFragments(new TopPaidFragment(),"TOP PAID");
viewPagerAdapter.addFragments(new TopFreeFragment(),"TOP FREE");
viewPager = (ViewPager)findViewById(R.id.ViewPager);
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_dashboard, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
return true;
case R.id.add_pic:
return true;
case R.id.Add_Story:
return true;
case R.id.delete:
return true;
case R.id.home:
NavUtils.navigateUpFromSameTask(this);
default:`enter code here`
// If we got here, the user's action was not recognized.
// Invoke the superclass to handle it.
return super.onOptionsItemSelected(item);
}
}
}
`enter code here`layout_xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
android:id="@+id/appBarLayout">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"
/>
<android.support.v4.view.ViewPager
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/ViewPager"/>
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="42dp"
android:layout_marginEnd="33dp"
android:layout_marginRight="33dp"
android:clickable="true"
app:backgroundTint="@android:color/primary_text_dark"
app:fabSize="mini"
app:srcCompat="@drawable/ic_plus_circle"
android:hint="@string/add_story"/>
</RelativeLayout>
答案 0 :(得分:1)
您正在尝试将适配器对象设置为viewpager
更改此行
viewPager= new ViewPagerAdapter(getSupportFragmentManager());
到
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());