ScrollView和Listview结合使用

拖动前
拖动后
- 自定义布局

public class MyListview extends ListView{

    public MyListview(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public boolean dispatchTouchEvent(MotionEvent ev) {  

         getParent().requestDisallowInterceptTouchEvent(true);  
         boolean b = super.dispatchTouchEvent(ev);
        return b;    
    }  
}
  • 布局中载入
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context="com.yang.sl.MainActivity" >

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >

        <com.yang.sl.MyListview
            android:id="@+id/main_mylistview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:layout_marginLeft="10dp"
        android:text="黑" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:layout_marginLeft="20dp"
        android:text="黑" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:layout_marginLeft="30dp"
        android:text="黑" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:layout_marginLeft="40dp"
        android:text="黑" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:layout_marginLeft="50dp"
        android:text="黑" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:layout_marginLeft="60dp"
        android:text="黑" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:layout_marginLeft="10dp"
        android:text="红" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:layout_marginLeft="20dp"
        android:text="红" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:layout_marginLeft="30dp"
        android:text="红" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:layout_marginLeft="40dp"
        android:text="红" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:layout_marginLeft="50dp"
        android:text="红" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:layout_marginLeft="60dp"
        android:text="红" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:layout_marginLeft="70dp"
        android:text="红" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:layout_marginLeft="80dp"
        android:text="红" />
    </LinearLayout>

</ScrollView>
  • 效果实现
public class MainActivity extends Activity {

    private String item[]={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","E","Y","Z"};
    private MyListview lv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();
        setOnSL(lv);
    }

    private void init() {
        lv = (MyListview) findViewById(R.id.main_mylistview);

        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_expandable_list_item_1, item);
        lv.setAdapter(adapter);
    }
    @SuppressWarnings("deprecation")
    private void setOnSL(MyListview listview) {
        // TODO Auto-generated method stub
        @SuppressWarnings("rawtypes")
        ArrayAdapter listAdapter = (ArrayAdapter) listview.getAdapter();

        if(listAdapter == null){

            return;
        }
        int toTalHeight = 0;

        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listview);

            if(listItem != null){
                listItem.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

                listItem.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);

                toTalHeight += listItem.getMeasuredHeight();
            }
        }
        LayoutParams params = listview.getLayoutParams();
        params.height = toTalHeight + (listview.getDividerHeight() * (listview.getCount() - 1))+listview.getPaddingTop()+listview.getPaddingBottom();

        int h=getWindowManager().getDefaultDisplay().getHeight();

        if(params.height>h / 2){
            params.height = h / 2;
        }
        listview.setLayoutParams(params);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值