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

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

核心代码如下:

1. NoScrollListView.java 

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

2. res > layout > activity_main.xml

  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. <LinearLayout
  9. android:id="@+id/LinearLayout"
  10. android:gravity="center_horizontal"
  11. android:background="#FFF4F4F4"
  12. android:layout_width="fill_parent"
  13. android:layout_height="fill_parent"
  14. android:orientation="vertical" >
  15. <TextView
  16. android:id="@+id/textView1"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:layout_marginLeft="2dp"
  20. android:layout_marginTop="20dp"
  21. android:text="This's a listView..." />
  22. <!--原: <ListView> -->
  23. <com.example.listviewdemo.NoScrollListView
  24. android:id="@+id/listView1"
  25. android:layout_width="fill_parent"
  26. android:layout_height="fill_parent"
  27. android:dividerHeight="0.0dip"
  28. android:fadingEdge="none"
  29. android:cacheColorHint="#FFF4F4F4"/>
  30. </LinearLayout>
  31. </ScrollView>

这里注意:原来的<ListView 改为<com.example.listviewdemo.NoScrollListView 。

3.MainActivity

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

效果图:

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

上一篇:eclipse+Maven插件报错:-Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME environment variable and mvn script match.


下一篇:Lambda为什么又称为匿名函数