参考网址:
(https://www.cnblogs.com/naishan/p/5650470.html)
(http://blog.csdn.net/newfar1991/article/details/8087220)
因为使用
SimpleCursorAdapter
来适配ListView,不懂修改adapter,所以我使用的是这种方法:
1、手动设置ListView高度 经过测试发现,在xml中直接指定ListView的高度,是可以解决这个问题的,但是ListView中的数据是可变的,实际高度还需要实际测量。于是手动代码设置ListView高度的方法就诞生了。
但是这个方法有个两个细节需要注意: 一是Adapter中getView方法返回的View的必须由LinearLayout组成,因为只有LinearLayout才有measure()方法,如果使用其他的布局如RelativeLayout,在调用listItem.measure(0, 0);时就会抛异常,因为除LinearLayout外的其他布局的这个方法就是直接抛异常的,没理由…。我最初使用的就是这个方法,但是因为子控件的顶层布局是RelativeLayout,所以一直报错,不得不放弃这个方法。 二是需要手动把ScrollView滚动至最顶端,因为使用这个方法的话,默认在ScrollView顶端的项是ListView,具体原因不了解,求大神解答…可以在Activity中设置: |
部分代码如下:
中一个ListView添加adapter的时候的代码: private void queryData(String tempName){ Log.d("TAG","Search:queryData开始"); Log.d("TAG","Search:输入的字符为:"+tempName); sqLiteDatabase=dbHelper.getReadableDatabase(); String sql="select id as _id,name from searchSugTab where name like '%"+tempName+"%' order by id desc"; Cursor cursor=sqLiteDatabase.rawQuery(sql,null); startManagingCursor(cursor); Log.d("TAG","Search:Cursor"); SimpleCursorAdapter adapter=new SimpleCursorAdapter(this,R.layout.search_suggection_layout,cursor, new String[]{"name"},new int[]{R.id.suggectionTxt}, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); Log.d("TAG","Search:成功获取adapter"); mSearchSuggestion.setAdapter(adapter); Log.d("TAG","Search:成功设置mSearchSuggestion.adapter"); // 设置ListView的长度 setListViewHeightBasedOnChildren(mSearchSuggestion); adapter.notifyDataSetChanged(); Log.d("TAG","Search:notifyDataSetChanged成功"); // 当输入框为空的时候执行 Log.d("TAG","Search:cursor.getCount():"+cursor.getCount()); if (tempName.equals("")&& cursor.getCount() != 0){ mtv_clear.setVisibility(View.GONE); // mSearchSuggestion.setVisibility(View.GONE); // mSearchHistory.setVisibility(View.VISIBLE); } else { // 当输入框不为空的时候执行 // mSearchHistory.setVisibility(View.VISIBLE); // mSearchSuggestion.setVisibility(View.VISIBLE); mtv_clear.setVisibility(View.VISIBLE); } sqLiteDatabase.close(); } 另一个ListView添加的adapter时候的代码: private void searchHisListV(String sql){ Log.d("TAG","Search:searchHisListV()开始"); sqLiteDatabase=dbHelper.getReadableDatabase(); // String sql="select id as _id,name from searchHisTab order by id DESC"; Cursor cursor=sqLiteDatabase.rawQuery(sql,null); Log.d("TAG","Search:searchHisListV()获取cursor完成"); startManagingCursor(cursor); Log.d("TAG","Search:startManagingCursor()管理"); SimpleCursorAdapter adapter=new SimpleCursorAdapter(this,R.layout.search_history_item,cursor, new String[]{"name"},new int[]{R.id.historyTxt}, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); Log.d("TAG","Search:searchHisListV()成功获取adapter"); mSearchHistory.setAdapter(adapter); // 设置LIstView高度 setListViewHeightBasedOnChildren(mSearchHistory); adapter.notifyDataSetChanged(); Log.d("TAG","Search:searchHisListV()成功设置adapter"); sqLiteDatabase.close(); } .XML的布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.administrator.madioplayer2.Search"> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:orientation="vertical" android:layout_height="wrap_content"> <ListView android:id="@+id/search_history" android:layout_width="match_parent" android:layout_height="200dp" /> <ListView android:id="@+id/search_suggestion" android:layout_width="match_parent" android:layout_height="200dp" /> <TextView android:id="@+id/tv_clear" android:layout_width="match_parent" android:layout_height="40dp" android:background="#F6F6F6" android:gravity="center" android:text="@string/lv_clear" /> </LinearLayout> </ScrollView> </LinearLayout> 其中一个ListView的item的布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageButton android:id="@+id/suggectionImg" android:layout_margin="20dp" android:layout_width="30dp" android:layout_height="30dp" android:background="@drawable/search_icon" /> <TextView android:id="@+id/suggectionTxt" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@color/deeppink" android:layout_margin="20dp" android:textSize="20sp" /> </LinearLayout> 注意:这里的 </LinearLayout> 不能是其他的布局文件,只能是LinearLayout(据说是,我没有去实践过) |