当前位置: 代码迷 >> Android >> Android 怎么在viewpager中添加这样的view
  详细解决方案

Android 怎么在viewpager中添加这样的view

热度:52   发布时间:2016-04-28 04:37:31.0
Android 如何在viewpager中添加这样的view
1.首先,已经实现了viewpager,一共三页,其中第二页是listview。代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <ListView 
        android:id="@+id/page2_list"
        android:layout_height="50dip"
        android:layout_width="fill_parent"
        android:background="#EDEDED"
        ></ListView>
    
</LinearLayout>


2.现在想在该页底部实现固定的textview,请教,如何添加。viewpager的初始化代码如下:
private void InitViewPager() {
viewPager=(ViewPager) findViewById(R.id.vPager);
views=new ArrayList<View>();
LayoutInflater inflater=getLayoutInflater();
view1=inflater.inflate(R.layout.pager1, null);
listView = (ListView) inflater.inflate(R.layout.pager2, null).findViewById(R.id.page2_list);
view3=inflater.inflate(R.layout.pager3, null);

//这一部分以后可以通过json或者xml解析来得到。数据库中新建一张课程表,然后通过servlet查询,返回json格式数据。
ArrayList<HashMap<String, String>> alist = new ArrayList<HashMap<String,String>>();
String[] lessons = new String[]{"瑜伽", "肚皮舞", "爵士"};
String[] time = new String[]{"17:30", "18:20", "19:30"};
for (int i = 0; i < 4; i++) {
for (int j = 0; j < lessons.length; j++) {
HashMap<String , String> map = new HashMap<String, String>();
map.put("lesson", lessons[j]);
map.put("time", time[j]);
alist.add(map);
}
}
SimpleAdapter simpleAdapter = new SimpleAdapter(MainActivity.this, alist, R.layout.pager2_list_item, new String[]{"lesson", "time"}, new int[]{R.id.page2_list_text1, R.id.pager2_list_text2});
listView.setAdapter(simpleAdapter);

views.add(view1);
views.add(listView);
views.add(view3);
viewPager.setAdapter(new MyViewPagerAdapter(views));
viewPager.setCurrentItem(0);
viewPager.setOnPageChangeListener(new MyOnPageChangeListener());
}

初学android,请各位大神指导。谢谢!
------解决方案--------------------
引用:
Quote: 引用:

Quote: 引用:

Quote: 引用:

你textview是要加在listview下面,还是ViewPager 下面?

请教,下面和上面有什么区别?是不是如果在下面,有能够直接使用的api。我主要就是想实现滑动listview的时候,这个textview保持不动。

如果是在listview下加,就使用listView的addFooterView方法,如果是layout下的话,使用<include layout="@layout/footer" />,根据你的需求,应该使用后者。

试了一下貌似还不行。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <ListView 
        android:id="@+id/page2_list"
        android:layout_height="50dip"
        android:layout_width="fill_parent"
        android:background="#EDEDED"
        ></ListView>
    
    <include layout="@layout/pager2_foot"/>
    
</LinearLayout>

将LinearLayout 改为RelativeLayout
  相关解决方案