我用了slidingMenu做出滑动显示左侧菜单, 在出来的菜单中设置跳转,但是为什么不跳转?
我是要从LiftNewsActivity 跳转到MainActivity类
java 代码 LiftNewsActivity类
public class LiftNewsActivity extends Activity implements OnClickListener {
private RelativeLayout rl = null;
private SlidingMenu slidingMenu = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.slidingmenu_left);
rl= (RelativeLayout) findViewById(R.id.rlivtv1);
rl.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.rlivtv1:
Intent intent = new Intent(LiftNewsActivity.this,MainActivity.class);
startActivity(intent);
break;
}
}
java 代码 MainActivity类
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
timer = new Timer();
findViewById(R.id.bNew).setOnClickListener(this);
findViewById(R.id.bPersonal).setOnClickListener(this);
((TextView) findViewById(R.id.tvTag1)).setOnClickListener(this);
((TextView) findViewById(R.id.tvTag2)).setOnClickListener(this);
((TextView) findViewById(R.id.tvTag3)).setOnClickListener(this);
((TextView) findViewById(R.id.tvTag4)).setOnClickListener(this);
views = new ArrayList<View>();
views.add(LayoutInflater.from(this).inflate(R.layout.head_lines, null));
views.add(LayoutInflater.from(this).inflate(R.layout.entertainment,
null));
views.add(LayoutInflater.from(this).inflate(R.layout.finance, null));
views.add(LayoutInflater.from(this).inflate(R.layout.car, null));
vpViewPager = (ViewPager) findViewById(R.id.vpViewPager1);
vpViewPager.setAdapter(new MyPagerAdapter(views));
// vpViewPager.setOnPageChangeListener(new MyOnPageChangeListener());
// initCursor(views.size());
// 设置抽屉菜单
slidingMenu = new SlidingMenu(this);
slidingMenu.setMode(SlidingMenu.LEFT_RIGHT);
slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN); // 触摸边界拖出菜单
slidingMenu.setMenu(R.layout.slidingmenu_left);
slidingMenu.setSecondaryMenu(R.layout.slidingmenu_right);
slidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
// 将抽屉菜单与主页面关联起来
slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
MyPagerAdapter myPagerAdapter = (MyPagerAdapter) vpViewPager
.getAdapter();
View v1 = myPagerAdapter.getItemAtPosition(0);
View v2 = myPagerAdapter.getItemAtPosition(1);
View v3 = myPagerAdapter.getItemAtPosition(2);
View v4 = myPagerAdapter.getItemAtPosition(3);
ptrlvHeadLineNews = (PullToRefreshListView) v1
.findViewById(R.id.ptrlvHeadLineNews);
ptrlvEntertainmentNews = (PullToRefreshListView) v2
.findViewById(R.id.ptrlvEntertainmentNews);
ptrlvFinanceNews = (PullToRefreshListView) v3
.findViewById(R.id.ptrlvFinanceNews);
ptrlvCarNews = (PullToRefreshListView) v4
.findViewById(R.id.ptrlvCarNews);
newAdapter = new NewListAdapter(this, getSimulationNews(10));
initPullToRefreshListView(ptrlvHeadLineNews, newAdapter);
initPullToRefreshListView(ptrlvEntertainmentNews, newAdapter);
initPullToRefreshListView(ptrlvFinanceNews, newAdapter);
initPullToRefreshListView(ptrlvCarNews, newAdapter);
}
@Override
public void onBackPressed() {
if (isExit) {
finish();
} else {
isExit = true;
Toast.makeText(MainActivity.this, "再按一次退出潍坊新闻", Toast.LENGTH_SHORT)
.show();
timeTask = new TimerTask() {
@Override
public void run() {
isExit = false;
}
};
timer.schedule(timeTask, 2000);
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tvTag1:
vpViewPager.setCurrentItem(0);
break;
case R.id.tvTag2:
vpViewPager.setCurrentItem(1);
break;
case R.id.tvTag3:
vpViewPager.setCurrentItem(2);
break;
case R.id.tvTag4:
vpViewPager.setCurrentItem(3);
break;
case R.id.bNew:
slidingMenu.showMenu();
break;
case R.id.bPersonal:
slidingMenu.showSecondaryMenu();
break;
}
}
------解决思路----------------------
这个页面的跳转不是activity的跳转而是fragment的相互切换,需要用的是replace方法,而你用了intent,这是不可行的。你先了解下fragment再看看代码就明白了。我给你贴个我写的
//左侧导航栏点击事件
public void menuItemClicked(int position) {
menu.showContent();// 点击关闭
switch (position) {
case 0:
Toast.makeText(getApplicationContext(), "遥信检测", 0).show();
fragmentManager.beginTransaction()
.replace(R.id.homefragment, new HomeFragment()).commit();
break;
case 1:
Toast.makeText(getApplicationContext(), "遥控监测", 0).show();
fragmentManager.beginTransaction()
.replace(R.id.homefragment, new YaokongFragment()).commit();
break;
case 2:
Toast.makeText(getApplicationContext(), "载波通信", 0).show();
fragmentManager.beginTransaction()
.replace(R.id.homefragment, new ZaibotongxinFragment())
.commit();
break;
case 3:
Toast.makeText(getApplicationContext(), "信号强度", 0).show();
fragmentManager.beginTransaction()
.replace(R.id.homefragment, new XinhaoqiangduFragment())
.commit();
break;
case 4:
Toast.makeText(getApplicationContext(), "电表抄读", 0).show();
fragmentManager.beginTransaction()
.replace(R.id.homefragment, new DianbiaochaoduFragment())
.commit();
break;
case 5:
Toast.makeText(getApplicationContext(), "脉冲检测", 0).show();
fragmentManager.beginTransaction()
.replace(R.id.homefragment, new MaichongjianceFragment())
.commit();
break;
default:
break;
}