Chinaunix首页 | 论坛 | 博客
  • 博客访问: 208387
  • 博文数量: 102
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1015
  • 用 户 组: 普通用户
  • 注册时间: 2013-06-05 16:45
文章存档

2014年(73)

2013年(29)

我的朋友

分类: Android平台

2014-05-30 14:57:45

四种方案解决ScrollView嵌套ListView问题



activity_main.xml

点击(此处)折叠或打开

  1. <RelativeLayout xmlns:android=""
  2.     xmlns:tools=""
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:paddingBottom="@dimen/activity_vertical_margin"
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"
  7.     android:paddingRight="@dimen/activity_horizontal_margin"
  8.     android:paddingTop="@dimen/activity_vertical_margin"
  9.     tools:context=".MainActivity" >

  10.     <LinearLayout
  11.         android:layout_width="match_parent"
  12.         android:layout_height="match_parent"
  13.         android:orientation="vertical" >

  14.         <TextView
  15.             android:layout_width="match_parent"
  16.             android:layout_height="wrap_content"
  17.             android:text="@string/hello_world" />

  18.         <ScrollView
  19.             android:id="@+id/scrollView"
  20.             android:layout_width="match_parent"
  21.             android:layout_height="wrap_content" >

  22.             <LinearLayout
  23.                 android:layout_width="match_parent"
  24.                 android:layout_height="wrap_content"
  25.                 android:orientation="vertical" >

  26.                 <com.lwb.scroll_listview.ListViewForScrollView
  27.                     android:id="@+id/listView"
  28.                     android:layout_width="match_parent"
  29.                     android:layout_height="wrap_content" >
  30.                 </com.lwb.scroll_listview.ListViewForScrollView>

  31.                 <ImageView
  32.                     android:layout_width="match_parent"
  33.                     android:layout_height="wrap_content"
  34.                     android:src="@drawable/ic_launcher" />
  35.             </LinearLayout>
  36.         </ScrollView>
  37.     </LinearLayout>
  38. </RelativeLayout>

item.xml

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android=""
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent"
  5.     android:orientation="horizontal" >
  6.     <ImageView
  7.         android:id="@+id/img"
  8.         android:layout_width="71dp"
  9.         android:layout_height="71dp"
  10.         android:src="@drawable/ic_launcher" />
  11.   
  12.     <LinearLayout
  13.         android:layout_width="wrap_content"
  14.         android:layout_height="wrap_content"
  15.         android:orientation="vertical" ><TextView
  16.             android:id="@+id/title"
  17.             android:layout_width="242dp"
  18.             android:layout_height="wrap_content"
  19.             android:text="222"
  20.             android:textAppearance="?android:attr/textAppearanceMedium" />

  21.         <TextView
  22.             android:id="@+id/info"
  23.             android:layout_width="match_parent"
  24.             android:layout_height="47dp"
  25.             android:text="11" />
  26. </LinearLayout>
  27.   
  28. </LinearLayout>

MainActivity.java

点击(此处)折叠或打开

  1. package com.lwb.scroll_listview;

  2. import java.util.ArrayList;
  3. import java.util.List;

  4. import android.annotation.SuppressLint;
  5. import android.app.Activity;
  6. import android.content.Context;
  7. import android.os.Bundle;
  8. import android.view.ContextMenu;
  9. import android.view.ContextMenu.ContextMenuInfo;
  10. import android.view.LayoutInflater;
  11. import android.view.MenuItem;
  12. import android.view.View;
  13. import android.view.View.OnCreateContextMenuListener;
  14. import android.view.ViewGroup;
  15. import android.widget.AdapterView;
  16. import android.widget.AdapterView.OnItemClickListener;
  17. import android.widget.BaseAdapter;
  18. import android.widget.ImageView;
  19. import android.widget.ScrollView;
  20. import android.widget.TextView;
  21. import android.widget.Toast;

  22. @SuppressLint("ParserError")
  23. public class MainActivity extends Activity {
  24.     ListViewForScrollView listView; // 声明一个ListView对象
  25.     private List<info> mlistInfo = new ArrayList<info>(); // 声明一个list,动态存储要显示的信息

  26.     private ScrollView scrollView = null;

  27.     @Override
  28.     public void onCreate(Bundle savedInstanceState) {
  29.         super.onCreate(savedInstanceState);
  30.         setContentView(R.layout.activity_main);

  31.         listView = (ListViewForScrollView) this.findViewById(R.id.listView); // 将listView与布局对象关联

  32.         setInfo(); // 给信息赋值函数,用来测试

  33.         listView.setAdapter(new ListViewAdapter(mlistInfo));

  34.         // 处理Item的点击事件
  35.         listView.setOnItemClickListener(new OnItemClickListener() {
  36.             public void onItemClick(AdapterView<?> parent, View view,
  37.                     int position, long id) {
  38.                 info getObject = mlistInfo.get(position); // 通过position获取所点击的对象
  39.                 int infoId = getObject.getId(); // 获取信息id
  40.                 String infoTitle = getObject.getTitle(); // 获取信息标题
  41.                 String infoDetails = getObject.getDetails(); // 获取信息详情

  42.                 // Toast显示测试
  43.                 Toast.makeText(MainActivity.this, "信息ID:" + infoId,
  44.                         Toast.LENGTH_SHORT).show();
  45.             }
  46.         });

  47.         // 长按菜单显示
  48.         listView.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
  49.             public void onCreateContextMenu(ContextMenu conMenu, View view,
  50.                     ContextMenuInfo info) {
  51.                 conMenu.setHeaderTitle("菜单");
  52.                 conMenu.add(0, 0, 0, "条目一");
  53.                 conMenu.add(0, 1, 1, "条目二");
  54.                 conMenu.add(0, 2, 2, "条目三");
  55.             }
  56.         });

  57.         scrollView = (ScrollView) findViewById(R.id.scrollView);
  58.         scrollView.smoothScrollTo(0, 0);
  59.     }

  60.     // 长按菜单处理函数
  61.     public boolean onContextItemSelected(MenuItem aItem) {
  62.         AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) aItem
  63.                 .getMenuInfo();
  64.         switch (aItem.getItemId()) {
  65.         case 0:
  66.             Toast.makeText(MainActivity.this, "你点击了条目一", Toast.LENGTH_SHORT)
  67.                     .show();
  68.             return true;
  69.         case 1:
  70.             Toast.makeText(MainActivity.this, "你点击了条目二", Toast.LENGTH_SHORT)
  71.                     .show();

  72.             return true;
  73.         case 2:
  74.             Toast.makeText(MainActivity.this, "你点击了条目三", Toast.LENGTH_SHORT)
  75.                     .show();
  76.             return true;
  77.         }
  78.         return false;
  79.     }

  80.     public class ListViewAdapter extends BaseAdapter {
  81.         View[] itemViews;

  82.         public ListViewAdapter(List<info> mlistInfo) {
  83.             // TODO Auto-generated constructor stub
  84.             itemViews = new View[mlistInfo.size()];
  85.             for (int i = 0; i < mlistInfo.size(); i++) {
  86.                 info getInfo = (info) mlistInfo.get(i); // 获取第i个对象
  87.                 // 调用makeItemView,实例化一个Item
  88.                 itemViews[i] = makeItemView(getInfo.getTitle(),
  89.                         getInfo.getDetails(), getInfo.getAvatar());
  90.             }
  91.         }

  92.         public int getCount() {
  93.             return itemViews.length;
  94.         }

  95.         public View getItem(int position) {
  96.             return itemViews[position];
  97.         }

  98.         public long getItemId(int position) {
  99.             return position;
  100.         }

  101.         // 绘制Item的函数
  102.         private View makeItemView(String strTitle, String strText, int resId) {
  103.             LayoutInflater inflater = (LayoutInflater) MainActivity.this
  104.                     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

  105.             // 使用View的对象itemView与R.layout.item关联
  106.             View itemView = inflater.inflate(R.layout.item, null);

  107.             // 通过findViewById()方法实例R.layout.item内各组件
  108.             TextView title = (TextView) itemView.findViewById(R.id.title);
  109.             title.setText(strTitle); // 填入相应的值
  110.             TextView text = (TextView) itemView.findViewById(R.id.info);
  111.             text.setText(strText);
  112.             ImageView image = (ImageView) itemView.findViewById(R.id.img);
  113.             image.setImageResource(resId);

  114.             return itemView;
  115.         }

  116.         public View getView(int position, View convertView, ViewGroup parent) {
  117.             if (convertView == null)
  118.                 return itemViews[position];
  119.             return convertView;
  120.         }
  121.     }

  122.     public void setInfo() {
  123.         mlistInfo.clear();
  124.         int i = 0;
  125.         while (i < 10) {
  126.             info information = new info();
  127.             information.setId(1000 + i);
  128.             information.setTitle("标题" + i);
  129.             information.setDetails("详细信息" + i);
  130.             information.setAvatar(R.drawable.ic_launcher);

  131.             mlistInfo.add(information); // 将新的info对象加入到信息列表中
  132.             i++;
  133.         }
  134.     }
  135. }
ListViewForScrollView .java

点击(此处)折叠或打开

  1. package com.lwb.scroll_listview;

  2. import android.content.Context;
  3. import android.util.AttributeSet;
  4. import android.widget.ListView;

  5. public class ListViewForScrollView extends ListView {
  6.     public ListViewForScrollView(Context context) {
  7.         super(context);
  8.     }

  9.     public ListViewForScrollView(Context context, AttributeSet attrs) {
  10.         super(context, attrs);
  11.     }

  12.     public ListViewForScrollView(Context context, AttributeSet attrs,
  13.             int defStyle) {
  14.         super(context, attrs, defStyle);
  15.     }

  16.     @Override
  17.     /**
  18.      * 重写该方法,达到使ListView适应ScrollView的效果
  19.      */
  20.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  21.         int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
  22.                 MeasureSpec.AT_MOST);
  23.         super.onMeasure(widthMeasureSpec, expandSpec);
  24.     }
  25. }





阅读(1221) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~