我想从另一个活动/片段中实现一个3-Tab活动/片段。
我查看了许多链接,例如this ,this one和this one too,但没有一个解决了我的问题。问题出现在代码
中的StudentProfile.java
文件中
fragment = new RandomFragment();
错误是:类型不匹配:无法从RandomFragment转换为Fragment
StudentProfile.java
package com.nikhil.studentlife;
import java.util.ArrayList;
import android.support.v7.app.AppCompatActivity;
import com.nikhil.studentlife.adapter.NavDrawerListAdapter;
import com.nikhil.studentlife.model.NavDrawerItem;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SlidingDrawer.OnDrawerCloseListener;
import android.widget.SlidingDrawer.OnDrawerOpenListener;
import android.widget.Toast;
//import android.app.Fragment;
import android.support.v4.app.Fragment;
//import android.app.FragmentManager;
import android.support.v4.app.FragmentManager;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.os.Bundle;
public class StudentProfile extends AppCompatActivity implements
OnItemClickListener {
private DrawerLayout drawerLayout;
private ListView listView;
private String[] studProfile;
private ActionBarDrawerToggle drawerListener;
/*
* from android hive example:--
*
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
*/
// nav drawer title
private CharSequence mDrawerTitle;
// used to store app title
private CharSequence mTitle;
// slide menu items
private String[] navMenuTitles;
private TypedArray navMenuIcons;
private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student_profile);
mTitle = mDrawerTitle = getTitle();
// load slide menu items
navMenuTitles = getResources().getStringArray(R.array.studProfile);
// nav drawer icons from resources
navMenuIcons = getResources()
.obtainTypedArray(R.array.studProfile_icons);
navDrawerItems = new ArrayList<NavDrawerItem>();
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
studProfile = getResources().getStringArray(R.array.studProfile);
listView = (ListView) findViewById(R.id.drawerList);
listView.setAdapter(new ArrayAdapter(this,
android.R.layout.simple_list_item_1, studProfile));
listView.setOnItemClickListener(this);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
drawerListener = new ActionBarDrawerToggle(this, drawerLayout,
R.string.drawer_open, R.string.drawer_close) {
@Override
public void onDrawerClosed(View drawerView) {
// TODO Auto-generated method stub
Toast.makeText(StudentProfile.this, "Drawer Closed",
Toast.LENGTH_SHORT).show();
}
@Override
public void onDrawerOpened(View drawerView) {
// TODO Auto-generated method stub
Toast.makeText(StudentProfile.this, "Drawer Opened",
Toast.LENGTH_SHORT).show();
}
};
drawerLayout.setDrawerListener(drawerListener);
listView.setOnItemClickListener(new SlideMenuClickListener());
/*
if (savedInstanceState == null) {
// on first time display view for first nav item
displayView(0);
}
*/
// adding nav drawer items to array
// Home
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
// Find People
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
// Photos
navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
// Communities, Will add a counter here
navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1), true, "22"));
// Pages
navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
// What's hot, We will add a counter here
navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1), true, "50+"));
// Recycle the typed array
navMenuIcons.recycle();
// setting the nav drawer list adapter
adapter = new NavDrawerListAdapter(getApplicationContext(),
navDrawerItems);
listView.setAdapter(adapter);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// getActionBar().setHomeButtonEnabled(true);
// Intent intent = getIntent();
// String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if(drawerListener.onOptionsItemSelected(item))
{
return true;
}
return super.onOptionsItemSelected(item);
}
/***
* Called when invalidateOptionsMenu() is triggered
*/
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// if nav drawer is opened, hide the action items
boolean drawerOpen = drawerLayout.isDrawerOpen(listView);
menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
drawerListener.onConfigurationChanged(newConfig);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onPostCreate(savedInstanceState);
drawerListener.syncState();
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(this, studProfile[arg2] + " was selected",
Toast.LENGTH_SHORT).show();
selectItem(arg2);
}
public void selectItem(int arg2) {
// TODO Auto-generated method stub
listView.setItemChecked(arg2, true);
setTitle(studProfile[arg2]);
}
public void setTitle(String title) {
// TODO Auto-generated method stub
getSupportActionBar().setTitle(title);
}
/*
* @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the
* menu; this adds items to the action bar if it is present.
* getMenuInflater().inflate(R.menu.student_profile, 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(); if (id ==
* R.id.action_settings) { return true; } return
* super.onOptionsItemSelected(item); }
*/
/**
* Slide menu item click listener
* */
private class SlideMenuClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// display view for selected nav drawer item
displayView(position);
}
}
/**
* Diplaying fragment view for selected nav drawer list item
* */
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
//fragment = new ProfileFragment();
//fragment = new Fragment();
fragment = new RandomFragment();
break;
case 1:
fragment = new HomeWorkFragment();
break;
case 2:
fragment = new AssignmentsFragment();
break;
case 3:
fragment = new ProjectsFragment();
break;
case 4:
fragment = new GamesFragment();
break;
case 5:
fragment = new ActivitiesFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.mainContent, fragment).commit();
// update selected item and title, then close the drawer
listView.setItemChecked(position, true);
listView.setSelection(position);
setTitle(navMenuTitles[position]);
drawerLayout.closeDrawer(listView);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
}
RandomFragment.java
package com.nikhil.studentlife;
导入android.os.Bundle; import android.support.v4.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBar.Tab; import android.support.v7.app.ActionBarActivity;
public class RandomFragment extends ActionBarActivity实现android.support.v7.app.ActionBar.TabListener {
private ViewPager tabsviewPager;
private ActionBar mActionBar;
private TabsAdapter mTabsAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabsviewPager = (ViewPager) findViewById(R.id.pager);
mTabsAdapter = new TabsAdapter(getSupportFragmentManager());
tabsviewPager.setAdapter(mTabsAdapter);
getSupportActionBar().setHomeButtonEnabled(false);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Tab friendstab = getSupportActionBar().newTab().setText("Friends").setTabListener(this);
Tab publicprofiletab = getSupportActionBar().newTab().setText("Public").setTabListener(this);
Tab communitytab = getSupportActionBar().newTab().setText("Community").setTabListener(this);
getSupportActionBar().addTab(friendstab);
getSupportActionBar().addTab(publicprofiletab);
getSupportActionBar().addTab(communitytab);
//This helps in providing swiping effect for v7 compat library
tabsviewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
getSupportActionBar().setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
@Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab selectedtab, FragmentTransaction arg1) {
// TODO Auto-generated method stub
tabsviewPager.setCurrentItem(selectedtab.getPosition()); //update tab position on tap
}
@Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
}