用一个listview达到ScrollView的效果

在工作中,有用到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"
     >
     这里我就不写布局了   你们可以根据你们的要求下上布局
</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);

 


    }

 

}

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值