这个效果在现在的软件中非常的常见;故在此分享代码如下;需要改进的地方请多多指教;;
写代码前我们先分析一下编程思想;
主要有四个个重要的类;Fragment,FragmentActivity;ViewPager;FragemntPagerAdapter;
首先让每个要显示的activity继承Fragement这个类;(记得不要指导错包咯;是support。v4。。下的jar包哦;);将Fragment的子类放到arrayList集合中;重点就是这些;FragmentActivity还需要注册;注册方式跟activity注册方式一样的;
下面上源代码哈;代码有点多哦;要有耐心;
java;代码;
先是adapter;
package com.transit.tarena.adapter;
import java.util.ArrayList;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class FragmentAdapter extends FragmentPagerAdapter{
//将fragment放到集合中;
private ArrayList<Fragment> fragmentlist;
public FragmentAdapter(ArrayList<Fragment> fragmentlist,FragmentManager fm) {
super(fm);
this.fragmentlist=fragmentlist;
}
@Override
public Fragment getItem(int arg0) {
return fragmentlist.get(arg0);
}
@Override
public int getCount() {
return fragmentlist.size();
}
}
package com.transit.tarena.adapter;
import java.util.ArrayList;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class FragmentAdapter extends FragmentPagerAdapter{
//将fragment放到集合中;
private ArrayList<Fragment> fragmentlist;
public FragmentAdapter(ArrayList<Fragment> fragmentlist,FragmentManager fm) {
super(fm);
this.fragmentlist=fragmentlist;
}
@Override
public Fragment getItem(int arg0) {
return fragmentlist.get(arg0);
}
@Override
public int getCount() {
return fragmentlist.size();
}
}
然后是继承Fragment的子类;子类中加载layout布局文件;
package com.transit.tarena.fragment;
import com.example.view.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class LineFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=null;
view=inflater.inflate(R.layout.line_layout, null);
return view;
}
}
package com.transit.tarena.fragment;
import com.example.view.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class LineFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=null;
view=inflater.inflate(R.layout.line_layout, null);
return view;
}
}
-----------------------
package com.transit.tarena.fragment;
import com.example.view.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MoreFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=null;
view=inflater.inflate(R.layout.activity_set_main, null);
return view;
}
}
--------------------
package com.transit.tarena.fragment;
import com.example.view.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MoreFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=null;
view=inflater.inflate(R.layout.activity_set_main, null);
return view;
}
}
package com.transit.tarena.fragment;
import com.example.view.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class StationFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=null;
view=inflater.inflate(R.layout.activity_station_main, null);
return view;
}
}
package com.transit.tarena.fragment;
import com.example.view.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class TransferFagment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=null;
view=inflater.inflate(R.layout.activity_change_main, null);
return view;
}
}
-------------------------------------
package com.transit.tarena.fragment;
import com.example.view.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class StationFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=null;
view=inflater.inflate(R.layout.activity_station_main, null);
return view;
}
}
package com.transit.tarena.fragment;
import com.example.view.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class TransferFagment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=null;
view=inflater.inflate(R.layout.activity_change_main, null);
return view;
}
}
package com.transit.tarena.view;
import java.util.ArrayList;
import com.example.view.R;
import com.transit.tarena.adapter.FragmentAdapter;
import com.transit.tarena.fragment.LineFragment;
import com.transit.tarena.fragment.MoreFragment;
import com.transit.tarena.fragment.StationFragment;
import com.transit.tarena.fragment.TransferFagment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
public class MainFragmentActivity extends FragmentActivity{
ViewPager pager;
//继承fragment类的子类;
MoreFragment moreFragment;
LineFragment lineFragment;
StationFragment stationFragment;
TransferFagment transferFagment;
//fragment适配器;
FragmentAdapter adapter;
//fragment集合;
ArrayList<Fragment> fragments;
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
try {
setContentView(R.layout.activity_main);
setview();
setlistener();
} catch (Exception e) {
}
}
private void setview() {
pager=(ViewPager)findViewById(R.id.gongjiaoviewpager);
fragments=new ArrayList<Fragment>();
//new fragmetn子类对象;
moreFragment=new MoreFragment();
lineFragment=new LineFragment();
stationFragment=new StationFragment();
transferFagment=new TransferFagment();
//添加到集合中;
fragments.add(moreFragment);
fragments.add(lineFragment);
fragments.add(stationFragment);
fragments.add(transferFagment);
//getSupportFragmentManager();方法必须继承FragmentActivity才ok;
adapter=new FragmentAdapter(fragments, getSupportFragmentManager());
pager.setAdapter(adapter);
}
private void setlistener() {
}
}
---------------------------------
package com.transit.tarena.view;
import java.util.ArrayList;
import com.example.view.R;
import com.transit.tarena.adapter.FragmentAdapter;
import com.transit.tarena.fragment.LineFragment;
import com.transit.tarena.fragment.MoreFragment;
import com.transit.tarena.fragment.StationFragment;
import com.transit.tarena.fragment.TransferFagment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
public class MainFragmentActivity extends FragmentActivity{
ViewPager pager;
//继承fragment类的子类;
MoreFragment moreFragment;
LineFragment lineFragment;
StationFragment stationFragment;
TransferFagment transferFagment;
//fragment适配器;
FragmentAdapter adapter;
//fragment集合;
ArrayList<Fragment> fragments;
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
try {
setContentView(R.layout.activity_main);
setview();
setlistener();
} catch (Exception e) {
}
}
private void setview() {
pager=(ViewPager)findViewById(R.id.gongjiaoviewpager);
fragments=new ArrayList<Fragment>();
//new fragmetn子类对象;
moreFragment=new MoreFragment();
lineFragment=new LineFragment();
stationFragment=new StationFragment();
transferFagment=new TransferFagment();
//添加到集合中;
fragments.add(moreFragment);
fragments.add(lineFragment);
fragments.add(stationFragment);
fragments.add(transferFagment);
//getSupportFragmentManager();方法必须继承FragmentActivity才ok;
adapter=new FragmentAdapter(fragments, getSupportFragmentManager());
pager.setAdapter(adapter);
}
private void setlistener() {
}
}
最后是布局文件;有四个哦;
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".StationMainActivity" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="90px" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="90px"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:scaleType="fitXY"
android:src="@drawable/tobiao" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="站点查询"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@android:color/white"
android:textSize="45px" />
</RelativeLayout>
<EditText
android:id="@+id/ed_station"
android:layout_width="wrap_content"
android:layout_height="80px"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/relativeLayout1"
android:layout_marginTop="15dp"
android:autoText="true"
android:ems="10"
android:hint="请输入公交站点名称"
android:inputType="textPersonName"
android:textStyle="bold" />
<Button
android:id="@+id/but_station"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/ed_station"
android:background="@drawable/gongjiao_imageview_drawble01" />
<ListView
android:id="@+id/station_listview01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/but_station" >
</ListView>
</RelativeLayout>
--------------------------------------
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".StationMainActivity" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="90px" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="90px"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:scaleType="fitXY"
android:src="@drawable/tobiao" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="线路查询"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@android:color/white"
android:textSize="45px" />
</RelativeLayout>
<EditText
android:id="@+id/ed_circuit"
android:layout_width="wrap_content"
android:layout_height="80px"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/relativeLayout1"
android:layout_marginTop="15dp"
android:autoText="true"
android:ems="10"
android:hint="请输入线路名称"
android:inputType="textPersonName"
android:textStyle="bold" />
<Button
android:id="@+id/but_circuit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/ed_circuit"
android:background="@drawable/gongjiao_imageview_drawble01" />
<ListView
android:id="@+id/listview_circuit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/but_circuit" >
</ListView>
</RelativeLayout>
--------------------------------------------------------
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".StationMainActivity" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="90px" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="90px"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:scaleType="fitXY"
android:src="@drawable/tobiao" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="线路查询"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@android:color/white"
android:textSize="45px" />
<TextView
android:id="@+id/city_text01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/textView1"
android:text="深圳"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@android:color/white" />
<Button
android:id="@+id/ditie_but"
android:layout_width="60px"
android:layout_height="60px"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@drawable/icon_arrive_station_car" />
</RelativeLayout>
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/relativeLayout1"
android:src="@drawable/ic_map_start" />
<EditText
android:id="@+id/ed_change02"
android:layout_width="wrap_content"
android:layout_height="80px"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/imageView2"
android:layout_toRightOf="@+id/imageView2"
android:autoText="true"
android:ems="10"
android:hint="请输入出发站点名称"
android:inputType="textPersonName"
android:textStyle="bold" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/ed_change"
android:layout_width="wrap_content"
android:layout_height="80px"
android:layout_alignLeft="@+id/ed_change02"
android:layout_alignParentRight="true"
android:layout_below="@+id/ed_change02"
android:autoText="true"
android:ems="10"
android:hint="请输入到达站点名称"
android:inputType="textPersonName"
android:textStyle="bold" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/ed_change"
android:layout_toLeftOf="@+id/ed_change02"
android:src="@drawable/ic_map_end" />
<Button
android:id="@+id/but_change01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/ed_change"
android:layout_marginTop="14dp"
android:background="@drawable/gongjiao_imageview_drawble01" />
<ListView
android:id="@+id/listview_change"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/but_change01" >
</ListView>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".StationMainActivity" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="90px" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="90px"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:scaleType="fitXY"
android:src="@drawable/tobiao" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="站点查询"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@android:color/white"
android:textSize="45px" />
</RelativeLayout>
<EditText
android:id="@+id/ed_station"
android:layout_width="wrap_content"
android:layout_height="80px"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/relativeLayout1"
android:layout_marginTop="15dp"
android:autoText="true"
android:ems="10"
android:hint="请输入公交站点名称"
android:inputType="textPersonName"
android:textStyle="bold" />
<Button
android:id="@+id/but_station"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/ed_station"
android:background="@drawable/gongjiao_imageview_drawble01" />
<ListView
android:id="@+id/station_listview01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/but_station" >
</ListView>
</RelativeLayout>
--------------------------------------
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".StationMainActivity" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="90px" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="90px"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:scaleType="fitXY"
android:src="@drawable/tobiao" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="线路查询"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@android:color/white"
android:textSize="45px" />
</RelativeLayout>
<EditText
android:id="@+id/ed_circuit"
android:layout_width="wrap_content"
android:layout_height="80px"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/relativeLayout1"
android:layout_marginTop="15dp"
android:autoText="true"
android:ems="10"
android:hint="请输入线路名称"
android:inputType="textPersonName"
android:textStyle="bold" />
<Button
android:id="@+id/but_circuit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/ed_circuit"
android:background="@drawable/gongjiao_imageview_drawble01" />
<ListView
android:id="@+id/listview_circuit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/but_circuit" >
</ListView>
</RelativeLayout>
--------------------------------------------------------
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".StationMainActivity" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="90px" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="90px"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:scaleType="fitXY"
android:src="@drawable/tobiao" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="线路查询"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@android:color/white"
android:textSize="45px" />
<TextView
android:id="@+id/city_text01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/textView1"
android:text="深圳"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@android:color/white" />
<Button
android:id="@+id/ditie_but"
android:layout_width="60px"
android:layout_height="60px"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@drawable/icon_arrive_station_car" />
</RelativeLayout>
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/relativeLayout1"
android:src="@drawable/ic_map_start" />
<EditText
android:id="@+id/ed_change02"
android:layout_width="wrap_content"
android:layout_height="80px"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/imageView2"
android:layout_toRightOf="@+id/imageView2"
android:autoText="true"
android:ems="10"
android:hint="请输入出发站点名称"
android:inputType="textPersonName"
android:textStyle="bold" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/ed_change"
android:layout_width="wrap_content"
android:layout_height="80px"
android:layout_alignLeft="@+id/ed_change02"
android:layout_alignParentRight="true"
android:layout_below="@+id/ed_change02"
android:autoText="true"
android:ems="10"
android:hint="请输入到达站点名称"
android:inputType="textPersonName"
android:textStyle="bold" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/ed_change"
android:layout_toLeftOf="@+id/ed_change02"
android:src="@drawable/ic_map_end" />
<Button
android:id="@+id/but_change01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/ed_change"
android:layout_marginTop="14dp"
android:background="@drawable/gongjiao_imageview_drawble01" />
<ListView
android:id="@+id/listview_change"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/but_change01" >
</ListView>
</RelativeLayout>