我有一个问题我做对了但是在我的程序中,标签的滑动无法正常显示任何页面。
主要Activity.java
public class MainActivity extends FragmentActivity implements TabListener {
ViewPager vp;
ActionBar actionbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vp=(ViewPager)findViewById(R.id.pager);
vp.setAdapter(new MyAdapter(getSupportFragmentManager()));
actionbar=getActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tab1=actionbar.newTab();
tab1.setText("MP3");
tab1.setTabListener(this);
ActionBar.Tab tab2=actionbar.newTab();
tab2.setText("MP4");
tab2.setTabListener(this);
actionbar.addTab(tab1);
actionbar.addTab(tab2);
}
@Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
Log.d("Gul", "OntabReselected"+"postion"+arg0.getPosition()+"name"+arg0.getText());
}
@Override
public void onTabSelected(Tab arg0, FragmentTransaction arg1) {
Log.d("Gul", "onTabSelected"+"postion"+arg0.getPosition()+"name"+arg0.getText());
}
@Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
Log.d("Gul", "onTabUnselected"+"postion"+arg0.getPosition()+"name"+arg0.getText());
}
}
class MyAdapter extends FragmentPagerAdapter{
public MyAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
Fragment f=null;
if(arg0==0){
f=new Mp3list();
}
else{
f=new Mp4list();
}
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 2;
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
我知道有一个问题,但我找不到它,当我点击标签时,它们仍无法正常工作? mp3list和mp4list是两个片段类。
答案 0 :(得分:2)
由于Google引入了android支持设计库,所以只需这样做:
将以下依赖项添加到gradle
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:design:22.2.0'
修改MainActivity,如下所示:
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
if (viewPager != null) {
setupViewPager(viewPager);
}
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
// create an adapter
Adapter adapter = new Adapter(getSupportFragmentManager());
// add your fragments to the adapter
adapter.addFragment(new Mp3list() , "MP3");
adapter.addFragment(new Mp4list() , "MP4");
// set the adapter to the ViewPager
viewPager.setAdapter(adapter);
}
// adapter class
class Adapter extends FragmentPagerAdapter {
private final List<Fragment> mFragments = new ArrayList<>();
private final List<String> mFragmentTitles = new ArrayList<>();
public Adapter(FragmentManager fm) {
super(fm);
}
public void addFragment(Fragment fragment, String title) {
mFragments.add(fragment);
mFragmentTitles.add(title);
}
@Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
@Override
public int getCount() {
return mFragments.size();
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitles.get(position);
}
}
}
最后将您的activity_main.xml布局更改为:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<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/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:background="?attr/colorPrimary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable" />
<!-- tabMode can be scrollable or fixedß -->
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>