我的主布局
index.xml
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:layout_width="fill_parent"
android:layout_height="110dp"
android:background="#fff64a"
android:orientation="vertical"
android:paddingBottom="10dp" >
android:layout_width="fill_parent"
android:layout_height="60dp"
android:gravity="center"
android:orientation="horizontal" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/location" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="定位中..."
android:textColor="#555"
android:textSize="20dp" />
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/corners"
android:gravity="center"
android:padding="10dp" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/search" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="请输入食物名称"
android:textColor="#888"
android:textSize="15dp" />
android:id="@+id/vpFgmt"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#888" >
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/border_top"
android:orientation="horizontal"
android:paddingBottom="3dp"
android:paddingTop="5dp" >
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="3dp"
android:drawableTop="@drawable/home"
android:text="首页"
android:textColor="#333"
android:textSize="10dp" />
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="3dp"
android:drawableTop="@drawable/orders"
android:text="订单"
android:textColor="#333"
android:textSize="10dp" />
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="3dp"
android:drawableTop="@drawable/settings"
android:text="设置"
android:textColor="#333"
android:textSize="10dp" />
然后有3个fragment home.xml,order.xml,settings.xml,后面2个布局比较简单就一个textview
home.xml
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#dfded4"
android:orientation="vertical" >
android:layout_width="fill_parent"
android:layout_height="120dp" >
android:id="@+id/vpBanner"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#888" >
android:id="@+id/llBanner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@android:color/transparent"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:paddingBottom="10dp" >
android:id="@+id/bannerDot1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/dot_select" />
android:id="@+id/bannerDot2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="3dp"
android:src="@drawable/dot" />
android:id="@+id/bannerDot3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="3dp"
android:src="@drawable/dot" />
android:id="@+id/bannerDot4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="3dp"
android:src="@drawable/dot" />
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:background="#fff"
android:orientation="vertical" >
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/border_bottom"
android:drawableLeft="@drawable/radio"
android:drawablePadding="5dp"
android:padding="5dp"
android:text="最新消息"
android:textColor="#333" />
android:id="@+id/index_orders"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#dfded4"
android:dividerHeight="1px" >
MainActivity.java
public class MainActivity extends FragmentActivity {
private ViewPager vpFgmt;
private List fragmentViews;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.index);
initFragmentViews();
}
private void initFragmentViews() {
fragmentViews = new ArrayList();
fragmentViews.add(new HomeFgmt());
fragmentViews.add(new OrderFgmt());
fragmentViews.add(new SettingsFgmt());
vpFgmt = (ViewPager) findViewById(R.id.vpFgmt);
FragmentManager fm = getSupportFragmentManager();
vpFgmt.setAdapter(new ViewPagerFgmtAdapter(fm,fragmentViews));
vpFgmt.addOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
}
ViewPagerFgmtAdapter.java
public class ViewPagerFgmtAdapter extends FragmentStatePagerAdapter {
private List views;
public ViewPagerFgmtAdapter(FragmentManager fm,List views)
{
super(fm);
this.views = views;
}
@Override
public Fragment getItem(int position) {
return views.get(position);
}
@Override
public int getCount() {
return views.size() ;
}
@Override
public void finishUpdate(ViewGroup container) {
super.finishUpdate(container);
}
}
HomeFgmt.java
public class HomeFgmt extends Fragment {
private ViewPager vpBanner;
private ViewPagerAdapter vpAdapter;
private List bannderViews;
private ImageView[] dots;
private int[] bannerIds = { R.drawable.index_banner1,
R.drawable.index_banner2, R.drawable.index_banner3,
R.drawable.index_banner4 };
private int[] ids = { R.id.bannerDot1, R.id.bannerDot2, R.id.bannerDot3,
R.id.bannerDot4 };
private ListView orderlist;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.home, container, false);
initBannerViews(root);
initDots(root);
return inflater.inflate(R.layout.home, container, false);
}
private void initBannerViews(View root) {
bannderViews = new ArrayList();
for (int i = 0; i < bannerIds.length; i++) {
ImageView iv = new ImageView(root.getContext());
iv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
iv.setBackgroundResource(bannerIds[i]);
bannderViews.add(iv);
}
vpAdapter = new ViewPagerAdapter(bannderViews, root.getContext());
vpBanner = (ViewPager) root.findViewById(R.id.vpBanner);
vpBanner.setAdapter(vpAdapter);
vpBanner.addOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
for (int i = 0; i < ids.length; i++) {
if (arg0 == i) {
dots[i].setImageResource(R.drawable.dot_select);
} else {
dots[i].setImageResource(R.drawable.dot);
}
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
orderlist = (ListView) root.findViewById(R.id.index_orders);
List list = DBHelper.getOrders(JsonUrl.GET_ORDERS_URL);
orderlist.setAdapter(new IndexListViewAdapter(getContext(), list));
}
private void initDots(View root) {
dots = new ImageView[bannderViews.size()];
for (int i = 0; i < bannderViews.size(); i++) {
dots[i] = (ImageView) root.findViewById(ids[i]);
}
}
}
之前没写底部滑动的时候homeFgmt的页面list 和 广告轮播的数据都有的,这样改了之后页面就是空的了
这是怎么回事呢