Android实现 ScrollView + ListView无滚动条滚动

来源:互联网 发布:阿里巴巴美工招聘杭州 编辑:程序博客网 时间:2024/06/10 08:50

效果图:


Android实现 ScrollView+ListView无滚动条滚动,即ListView的数据会全部显示完,但Listview无滚动条。

核心代码如下:


1. NoScrollListView.java 

[java] view plaincopy
  1. /*** 
  2.  * 自定义ListView子类,继承ListView 
  3.  * @author Administrator 
  4.  * 
  5.  */  
  6. public class NoScrollListView extends ListView {  
  7.   
  8.     public NoScrollListView(Context context) {  
  9.         super(context);  
  10.     }  
  11.   
  12.     public NoScrollListView(Context context, AttributeSet attrs) {  
  13.         super(context, attrs);  
  14.     }  
  15.   
  16.     public NoScrollListView(Context context, AttributeSet attrs, int defStyle) {  
  17.         super(context, attrs, defStyle);  
  18.     }  
  19.   
  20.     @Override  
  21.     public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  22.         int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);  
  23.         super.onMeasure(widthMeasureSpec, expandSpec);  
  24.     }  
  25.   
  26. }  


2. res > layout > activity_main.xml

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/ScrollView"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:background="#FFF4F4F4"  
  7.     android:scrollbars="vertical" >  
  8.   
  9.     <LinearLayout  
  10.         android:id="@+id/LinearLayout"  
  11.         android:gravity="center_horizontal"  
  12.         android:background="#FFF4F4F4"  
  13.         android:layout_width="fill_parent"  
  14.         android:layout_height="fill_parent"  
  15.         android:orientation="vertical" >  
  16.   
  17.         <TextView  
  18.             android:id="@+id/textView1"  
  19.             android:layout_width="wrap_content"  
  20.             android:layout_height="wrap_content"  
  21.             android:layout_marginLeft="2dp"  
  22.             android:layout_marginTop="20dp"  
  23.             android:text="This's a listView..." />  
  24.           
  25.         <!--原: <ListView> -->  
  26.         <com.example.listviewdemo.NoScrollListView  
  27.             android:id="@+id/listView1"  
  28.             android:layout_width="fill_parent"  
  29.             android:layout_height="fill_parent"   
  30.             android:dividerHeight="0.0dip"  
  31.             android:fadingEdge="none"  
  32.             android:cacheColorHint="#FFF4F4F4"/>  
  33.               
  34.     </LinearLayout>  
  35.   
  36. </ScrollView>  
这里注意:原来的<ListView 改为<com.example.listviewdemo.NoScrollListView 。


3.MainActivity

[java] view plaincopy
  1. private String[] nameList = {"Miley Cyruc","Alice Keys","Jewel","Dublin","Kelly Clarkson",  
  2.                              "Mariah Carey","Sheen","Adele","Avril Lavigne","Taylor Swift"};  
  3.   
  4. @Override  
  5. protected void onCreate(Bundle savedInstanceState) {  
  6.     super.onCreate(savedInstanceState);  
  7.     setContentView(R.layout.activity_main);  
  8.       
  9.     ListView lv = (ListView) findViewById(R.id.listView1);  
  10.       
  11.     ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, nameList);  
  12.     lv.setAdapter(adapter);  
  13.     //lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);//选择效果  
  14.   
  15.     //listView注册一个元素点击事件监听器  
  16.     lv.setOnItemClickListener(new OnItemClickListener() {  
  17.         @Override  
  18.         public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {  
  19.             Log.d(TAG, "//:"+MainActivity.this.nameList[arg2]);  
  20.             Toast.makeText(MainActivity.this"Hey, "+nameList[arg2], Toast.LENGTH_LONG).show();  
  21.         }  
  22.     });   
  23.       
  24. }  



0 0
原创粉丝点击