您的位置:首页 > 移动开发 > Android开发

用一个listview达到ScrollView的效果

2015-11-20 09:44 543 查看
在工作中,有用到ScrollView包裹listView实现页面滑动的效果,但是用的时候ScrollView和ListVew有冲突!所以在最近学到一种新的做法,就是用一个ListView就可搞定了,也能达到ScrollView的效果。下面请具体看代码:





 

首先是主xml文件:

<?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"

    android:background="@color/common_bg"

     >

     <ListView

        android:id="@+id/lv_list"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:background="#33ffffff"

        />

</LinearLayout>

在算上两个从的xml文件也就是说是一个头部,一个尾部

header.xml文件

<?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"

    android:background="@color/common_bg"

     >

     这里我就不写布局了   你们可以根据你们的要求下上布局

</LinearLayout>

footer.xml文件

<?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"

    android:background="@color/common_bg"

     >

     这里我就不写布
4000
局了   你们可以根据你们的要求下上布局

</LinearLayout>

 

 

上面是布局文件 ,接下来是java文件:

代码如下:

public class ListViewActivity extends Activity{

 @Override

    protected void onCreate(Bundle savedInstanceState) {

        setContentView(R.layout.activity_main);

        super.onCreate(savedInstanceState);

      ListView lv_serving_people_list = (ListView) findViewById(R.id.lv_list);

       View header =  LayoutInflater.from(context).inflate(R.layout.header, null);

      View footer=  LayoutInflater.from(context).inflate(R.layout.footer, null);

       //里面的id你们可以根据这两个header,footer去点findViewById

         

   

      List<Object> annoList = new ArrayList<Object>();

        annoList.add("赵日天");

        annoList.add("叶良辰");

        annoList.add("福尔康");

        annoList.add("龙傲天");

//这是适配器,适配器的代码我就不贴出来了

        StaffDetailAdapter adapter = new StaffDetailAdapter(context, annoList);

 

        //这两个  一定要放在适配器的前面,不然运行后会报错

        lv_serving_people_list.addHeaderView(header);

         lv_serving_people_list.addFooterView(footer);

       

        lv_serving_people_list.setAdapter(adapter);

 

    }

 

}

 

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  listview Android