我正在尝试实施像此http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/这样的滑动标签视图。 但是当我尝试在Adapter中实现getItems方法时,它不接受我的自定义Fragment。 这是代码: MyActivity.java
package com.bob.app;
import android.os.Bundle;
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.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.*;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity implements android.support.v7.app.ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private String[] tabs = { getString(R.string.tab_home), getString(R.string.tab_city), getString(R.string.tab_name),getString(R.string.tab_favorites) };
private DrawerLayout drawerLayout;
private ListView listView;
private String[] Drawer_menu_item;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final android.support.v7.app.ActionBar actionbar = getSupportActionBar();
actionbar.setNavigationMode(android.support.v7.app.ActionBar.NAVIGATION_MODE_TABS);
viewPager = (ViewPager) findViewById(R.id.pager);
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionbar.setHomeButtonEnabled(true);
// Adding Tabs
for (String tab_name : tabs) {
actionbar.addTab(actionbar.newTab().setText(tab_name)
.setTabListener((android.support.v7.app.ActionBar.TabListener) this));
Drawer_menu_item= getResources().getStringArray(R.array.menu_item);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
listView = (ListView) findViewById(R.id.left_drawer);
// Set the adapter for the list view
listView.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer, Drawer_menu_item));
/* Set the list's click listener
listView.setOnItemClickListener(new DrawerItemClickListener());*/
}
}
@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_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onTabSelected(android.support.v7.app.ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
}
@Override
public void onTabUnselected(android.support.v7.app.ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
}
@Override
public void onTabReselected(android.support.v7.app.ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
}
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
/* Home fragment
return new Home_fragment();
case 1:
/* City fragment
return new City_fragment();
case 2:
/* Name fragment
return new Name_fragment();
case 3:
/* Favourites fragment
return new Fav_fragment();
}
return null;
}
@Override
public int getCount() {
// get item count - equal to number of tabs
return tabs.length;
}
}
}
Name_fragment.java
package com.bob.sluurpy.app;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by boob on 11/09/15.
*/
public class Name_fragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.name, container, false);
return rootView;
}
}
答案 0 :(得分:0)
请在import android.support.v4.app.Fragment;
中导入import android.app.Fragment;
代替Name_fragment
这可能会解决您的问题!