android 在fragment里的怎样实现滑动fragment
1个回答
2016-03-28 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
按照一般的思路,我们或许会这么做:首先,使用getActionBar()方法获得操作栏,然后我们将操作栏的导航模式设置为Tab,并添加一些Tab,然后实现TabListener接口;其次,我们将多个布局通过Inflater()方法变成View,然后放到ViewPager里面,并实现OnPageChangeListener接口就可以了。由此我们可以写出下面的代码:
package com.Android.AnyViewUI;
import java.util.ArrayList;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
public class MainActivity extends FragmentActivity implements TabListener,OnPageChangeListener {
private ActionBar mActionBar;
private ViewPager mViewPager;
private TabPagerAdapter mAdapter;
private ArrayList<View> mViews;
private ArrayList<ActionBar.Tab> mTabs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);
//取得ActionBar
mActionBar=getActionBar();
//以Tab方式导航
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//禁用ActionBar标题
mActionBar.setDisplayShowTitleEnabled(false);
//禁用ActionBar图标
mActionBar.setDisplayUseLogoEnabled(false);
//禁用ActionBar返回键
mActionBar.setDisplayShowHomeEnabled(false);
//添加Tabs
mTabs=new ArrayList<ActionBar.Tab>();
ActionBar.Tab tab0=mActionBar.newTab();
tab0.setText("界面一");
tab0.setTabListener(this);
mTabs.add(tab0);
mActionBar.addTab(tab0);
ActionBar.Tab tab1=mActionBar.newTab();
tab1.setText("界面二");
tab1.setTabListener(this);
mTabs.add(tab1);
mActionBar.addTab(tab1);
ActionBar.Tab tab2=mActionBar.newTab();
tab2.setText("界面三");
tab2.setTabListener(this);
mTabs.add(tab2);
mActionBar.addTab(tab2);
//获取ViewPager
mViewPager=(ViewPager)findViewById(R.id.ViewPager);
//初始化mViews
mViews=new ArrayList<View>();
mViews.add(LayoutInflater.from(this).inflate(R.layout.layout_0, null));
mViews.add(LayoutInflater.from(this).inflate(R.layout.layout_1, null));
mViews.add(LayoutInflater.from(this).inflate(R.layout.layout_2, null));
//初始化mAdapter
mAdapter=new TabPagerAdapter(mViews);
mViewPager.setAdapter(mAdapter);
mViewPager.setOnPageChangeListener(this);
//默认显示第二项
mViewPager.setCurrentItem(2);
}
@Override
public void onTabReselected(Tab mTab, FragmentTransaction arg1)
{
}
@Override
public void onTabSelected(Tab mTab, FragmentTransaction arg1)
{
if(mViewPager!=null)
{
mViewPager.setCurrentItem(mTab.getPosition());
}
}
@Override
public void onTabUnselected(Tab mTab, FragmentTransaction arg1)
{
}
@Override
public void onPageScrollStateChanged(int arg0)
{
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2)
{
}
@Override
public void onPageSelected(int Index)
{
//设置当前要显示的View
mViewPager.setCurrentItem(Index);
//选中对应的Tab
mActionBar.selectTab(mTabs.get(Index));
}
}
其中,TabPagerAdapter是一个继承自PagerAdapter的适配器类:
package com.Android.AnyViewUI;
import java.util.ArrayList;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
public class TabPagerAdapter extends PagerAdapter
{
private ArrayList<View> mViews;
public TabPagerAdapter(ArrayList<View> mViews)
{
this.mViews=mViews;
}
@Override
public void destroyItem(View container, int position, Object object)
{
((ViewPager)container).removeView(mViews.get(position));
}
@Override
public Object instantiateItem(View container, int position)
{
((ViewPager)container).addView(mViews.get(position), 0);
return mViews.get(position);
}
@Override
public int getCount()
{
return mViews.size();
}
@Override
public boolean isViewFromObject(View mView, Object mObject)
{
return (mView==mObject);
}
}
我们的代码从逻辑上来讲是没有什么问题的,但是当我们试图运行这段代码的时候,我们发现这段代码出了问题,而问题就出在OnTabSelected()上。但是我们冷静下来想了想,没有错啊,那么问题到底出在哪里呢?看到网上的朋友说,这里这个适配器应该继承自FragmentPagerAdapter:
package com.Android.AnyViewUI;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class ViewPagerAdapter extends FragmentPagerAdapter {
//定义三个Fragment的索引
public static final int Fragment_Index_0=0;
public static final int Fragment_Index_1=1;
public static final int Fragment_Index_2=2;
public ViewPagerAdapter(FragmentManager fragmentManager)
{
super(fragmentManager);
}
@Override
public Fragment getItem(int Index)
{
Fragment mFragemnt=null;
switch(Index)
{
case Fragment_Index_0:
mFragemnt=new Fragment_0();
break;
case Fragment_Index_1:
mFragemnt=new Fragment_1();
break;
case Fragment_Index_2:
mFragemnt=new Fragment_2();
break;
}
return mFragemnt;
}
@Override
public int getCount()
{
return 3;
}
}
其中,Fragment_0、Fragment_1、Fragment_2是继承自Fragment的类,由于三个布局基本一样,这里只给出Fragment_0的代码:
package com.Android.AnyViewUI;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment_0 extends Fragment
{
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
View mView=inflater.inflate(R.layout.layout_0, container, false);
return mView;
}
}
package com.Android.AnyViewUI;
import java.util.ArrayList;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
public class MainActivity extends FragmentActivity implements TabListener,OnPageChangeListener {
private ActionBar mActionBar;
private ViewPager mViewPager;
private TabPagerAdapter mAdapter;
private ArrayList<View> mViews;
private ArrayList<ActionBar.Tab> mTabs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);
//取得ActionBar
mActionBar=getActionBar();
//以Tab方式导航
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//禁用ActionBar标题
mActionBar.setDisplayShowTitleEnabled(false);
//禁用ActionBar图标
mActionBar.setDisplayUseLogoEnabled(false);
//禁用ActionBar返回键
mActionBar.setDisplayShowHomeEnabled(false);
//添加Tabs
mTabs=new ArrayList<ActionBar.Tab>();
ActionBar.Tab tab0=mActionBar.newTab();
tab0.setText("界面一");
tab0.setTabListener(this);
mTabs.add(tab0);
mActionBar.addTab(tab0);
ActionBar.Tab tab1=mActionBar.newTab();
tab1.setText("界面二");
tab1.setTabListener(this);
mTabs.add(tab1);
mActionBar.addTab(tab1);
ActionBar.Tab tab2=mActionBar.newTab();
tab2.setText("界面三");
tab2.setTabListener(this);
mTabs.add(tab2);
mActionBar.addTab(tab2);
//获取ViewPager
mViewPager=(ViewPager)findViewById(R.id.ViewPager);
//初始化mViews
mViews=new ArrayList<View>();
mViews.add(LayoutInflater.from(this).inflate(R.layout.layout_0, null));
mViews.add(LayoutInflater.from(this).inflate(R.layout.layout_1, null));
mViews.add(LayoutInflater.from(this).inflate(R.layout.layout_2, null));
//初始化mAdapter
mAdapter=new TabPagerAdapter(mViews);
mViewPager.setAdapter(mAdapter);
mViewPager.setOnPageChangeListener(this);
//默认显示第二项
mViewPager.setCurrentItem(2);
}
@Override
public void onTabReselected(Tab mTab, FragmentTransaction arg1)
{
}
@Override
public void onTabSelected(Tab mTab, FragmentTransaction arg1)
{
if(mViewPager!=null)
{
mViewPager.setCurrentItem(mTab.getPosition());
}
}
@Override
public void onTabUnselected(Tab mTab, FragmentTransaction arg1)
{
}
@Override
public void onPageScrollStateChanged(int arg0)
{
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2)
{
}
@Override
public void onPageSelected(int Index)
{
//设置当前要显示的View
mViewPager.setCurrentItem(Index);
//选中对应的Tab
mActionBar.selectTab(mTabs.get(Index));
}
}
其中,TabPagerAdapter是一个继承自PagerAdapter的适配器类:
package com.Android.AnyViewUI;
import java.util.ArrayList;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
public class TabPagerAdapter extends PagerAdapter
{
private ArrayList<View> mViews;
public TabPagerAdapter(ArrayList<View> mViews)
{
this.mViews=mViews;
}
@Override
public void destroyItem(View container, int position, Object object)
{
((ViewPager)container).removeView(mViews.get(position));
}
@Override
public Object instantiateItem(View container, int position)
{
((ViewPager)container).addView(mViews.get(position), 0);
return mViews.get(position);
}
@Override
public int getCount()
{
return mViews.size();
}
@Override
public boolean isViewFromObject(View mView, Object mObject)
{
return (mView==mObject);
}
}
我们的代码从逻辑上来讲是没有什么问题的,但是当我们试图运行这段代码的时候,我们发现这段代码出了问题,而问题就出在OnTabSelected()上。但是我们冷静下来想了想,没有错啊,那么问题到底出在哪里呢?看到网上的朋友说,这里这个适配器应该继承自FragmentPagerAdapter:
package com.Android.AnyViewUI;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class ViewPagerAdapter extends FragmentPagerAdapter {
//定义三个Fragment的索引
public static final int Fragment_Index_0=0;
public static final int Fragment_Index_1=1;
public static final int Fragment_Index_2=2;
public ViewPagerAdapter(FragmentManager fragmentManager)
{
super(fragmentManager);
}
@Override
public Fragment getItem(int Index)
{
Fragment mFragemnt=null;
switch(Index)
{
case Fragment_Index_0:
mFragemnt=new Fragment_0();
break;
case Fragment_Index_1:
mFragemnt=new Fragment_1();
break;
case Fragment_Index_2:
mFragemnt=new Fragment_2();
break;
}
return mFragemnt;
}
@Override
public int getCount()
{
return 3;
}
}
其中,Fragment_0、Fragment_1、Fragment_2是继承自Fragment的类,由于三个布局基本一样,这里只给出Fragment_0的代码:
package com.Android.AnyViewUI;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment_0 extends Fragment
{
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
View mView=inflater.inflate(R.layout.layout_0, container, false);
return mView;
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |