android 解決ScrollView與ListView的衝突(TableLayout+ScrollView)

來源:互聯網
上載者:User

     首先對於ScrollView和ListView這兩個控制項發生衝突我就不過多解釋了,用過的同學們都知道,

     問題:ListView在ScrollView顯示一行半,解決方案:http://jackxlee.blog.51cto.com/2493058/666475 這裡有解決辦法.不過實現後會發現,效果不是很友好,首先由別的介面跳轉至該介面的時候,listview顯示的不正常,

      第一:顯示順序不對。第二:scrollView顯示不再最上面,而是現在在listview的中間,這點肯定不被認可.解決辦法:scrollTo(0,0);不過這個方法必須要用handler實現:

mHandler.post(new Runnable() {   

@Override   
public void run() {   
((ScrollView)findViewById(R.id.main_sv)).scrollTo(10, 10) ;
}   
});

實現後結果會發現:在listview 初始化完以後,螢幕會一閃,其實就是由listview 的中間再次scrollTo頂部。效果很不友好.

本人正在開發新浪微博一些應用,新浪微博的評論列表顯示的時候根本沒有遇到上述情況,也許是自己比較笨吧,不知道該怎麼實現,不過之前做過簡單的動態Taable的應用,想想就一個一個產生吧,麻煩也好,起碼可以實現.

下面是程式碼片段:

public void tableList(Context context, ArrayList<BlogInfo> blogInfos) {TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(android.widget.TableRow.LayoutParams.FILL_PARENT,android.widget.TableRow.LayoutParams.FILL_PARENT);AsyncImageLoader asyncImageLoader = new AsyncImageLoader();View convertView = null;for (int i = 0; i < blogInfos.size(); i++) {TableRow tableRow = new TableRow(context);final ViewHolder holder = new ViewHolder();convertView = LayoutInflater.from(context).inflate(R.layout.blogdetail_item, null);holder.imageLog = (ImageView) convertView.findViewById(R.id.iv_blogdetail_item);holder.textName = (TextView) convertView.findViewById(R.id.tv_blogtetail_item_name);holder.textComment = (TextView) convertView.findViewById(R.id.tv_blogdetail_item_content);holder.textTime = (TextView) convertView.findViewById(R.id.tv_blogdetail_item_time);holder.textName.setText(blogInfos.get(i).Title);holder.textComment.setText(blogInfos.get(i).Content);holder.textTime.setText(MyUtil.convertTime(blogInfos.get(i).Time, 1));Drawable drawable = asyncImageLoader.loadDrawable(blogInfos.get(i).LogImage, new ImageCallback() {@Overridepublic void imageLoaded(Drawable imageDrawable,String imageUrl) {if (imageDrawable != null) {holder.imageLog.setImageDrawable(imageDrawable);} else {holder.imageLog.setImageResource(R.drawable.u6_normal);}}});if (drawable != null) {holder.imageLog.setImageDrawable(drawable);}tableRow.addView(convertView, layoutParams);tb_blogdetail.addView(tableRow);}}

       其實很簡單,就是建立了一個View作為TableRow,把資料一個一個的填充進去,擷取有些同學會發現這個效率很低,早N年前google就提出來了要建立一個類ViewHolder,用於減少findbyId的使用,可是我一用就報錯,錯誤資訊如下:

The specified child already has a parent. You must call removeView() on the child's parent first.

   這個應該可以提高效能,本人水平有限,知道的朋友麻煩解釋下.

  下面效果

       
  

      Listview 載入中...                  Listview載入完畢.                  下拉Listview.

  TableLayout+ScrollView 很好的解決了Listview和ScrollView之間的衝突.雖說我寫的效能不高,但是顯示這些內容足夠了,效果如同新浪微博一樣,呵呵,就說這麼多了。有什麼不足之處,或更好的解決方案:請您留吉言.




方法二:(動態修改ListView的高度,切記:在使用ScrollView嵌套ListView布局的時候,中間一定要加一個LinearLayout或者什麼的布局,不然效果出不來,原因不明,總之知道如何就OK了.

例如:

    <ScrollView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:scrollbars="none" >      <LinearLayout            android:layout_width="match_parent"            android:layout_height="fill_parent"            android:orientation="vertical"            android:padding="20dp" >            <com.jj.corner.MyListView                android:id="@+id/mylistview_2"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:layout_marginBottom="20dp"                android:layout_marginTop="20dp"                android:background="@drawable/list_default_round"                android:cacheColorHint="#00000000" />        </LinearLayout>    </ScrollView>

動態修改ListView高度方法:

/*** * 動態設定listview的高度 *  * @param listView */public void setListViewHeightBasedOnChildren(ListView listView) {ListAdapter listAdapter = listView.getAdapter();if (listAdapter == null) {return;}int totalHeight = 0;for (int i = 0; i < listAdapter.getCount(); i++) {View listItem = listAdapter.getView(i, null, listView);listItem.measure(0, 0);totalHeight += listItem.getMeasuredHeight();}ViewGroup.LayoutParams params = listView.getLayoutParams();params.height = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));// params.height += 5;// if without this statement,the listview will be// a// little short// listView.getDividerHeight()擷取子項間分隔字元佔用的高度// params.height最後得到整個ListView完整顯示需要的高度listView.setLayoutParams(params);}


      



          

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.