Android实现 ScrollView + ListView无滚动条滚动 解决2者间的冲突

正常来说,在ScrollView添加一个ListView后在真机上只会显示ListView的一行多一点,我也不理解为什么会这样,后来我把 ListView的layout_height改成400dip,而不是用match_parent和wrap_content,我发现这样的话 ListView就显示的多了很多。但这样的话就没什么意义了。现在解决 ScrollView+ListView无滚动条滚动,即ListView的数据会全部显示完,但Listview无滚动条。



/*** 
 * 自定义ListView子类,继承ListView 
 * @author Administrator 
 * 
 */  
public class NoScrollListView extends ListView {  
  
    public NoScrollListView(Context context) {  
        super(context);  
    }  
  
    public NoScrollListView(Context context, AttributeSet attrs) {  
        super(context, attrs);  
    }  
  
    public NoScrollListView(Context context, AttributeSet attrs, int defStyle) {  
        super(context, attrs, defStyle);  
    }  
  
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);  
        super.onMeasure(widthMeasureSpec, expandSpec);  
    }  
  
}  



2. res > layout > activity_main.xml


    <?xml version="1.0" encoding="utf-8"?>  
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"  
        android:id="@+id/ScrollView"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"  
        android:background="#FFF4F4F4"  
        android:scrollbars="vertical" >  
      
        <LinearLayout  
            android:id="@+id/LinearLayout"  
            android:gravity="center_horizontal"  
            android:background="#FFF4F4F4"  
            android:layout_width="fill_parent"  
            android:layout_height="fill_parent"  
            android:orientation="vertical" >  
      
            <TextView  
                android:id="@+id/textView1"  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:layout_marginLeft="2dp"  
                android:layout_marginTop="20dp"  
                android:text="This's a listView..." />  
              
            <!--原: <ListView> -->  
            <com.example.listviewdemo.NoScrollListView  
                android:id="@+id/listView1"  
                android:layout_width="fill_parent"  
                android:layout_height="fill_parent"   
                android:dividerHeight="0.0dip"  
                android:fadingEdge="none"  
                android:cacheColorHint="#FFF4F4F4"/>  
                  
        </LinearLayout>  
      
    </ScrollView>  



3.MainActivity

public class MainActivity extends Activity {


protected static final String TAG = "MainActivity";
private String[] nameList = { "Miley Cyruc", "Alice Keys", "Jewel",
"Dublin", "Kelly Clarkson", "Mariah Carey", "Sheen", "Adele",
"Avril Lavigne", "Taylor Swift","aaaaaa","aaaaa","aaaaaa" };


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


ListView lv = (ListView) findViewById(R.id.listView1);


ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, nameList);
lv.setAdapter(adapter);
// lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);//选择效果


// listView注册一个元素点击事件监听器
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Log.d(TAG, "//:" + MainActivity.this.nameList[arg2]);
Toast.makeText(MainActivity.this, "Hey, " + nameList[arg2],
Toast.LENGTH_LONG).show();
}
});


}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值