1. 程式人生 > >ScrollView中放ListView,ListView中放GridView衝突問題

ScrollView中放ListView,ListView中放GridView衝突問題

ScrollView 中放ListView是避免不了的,網上有種靠譜的方法,我用過覺得很不錯。

ListView

package com.work.driver.view;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;

/**
 * @project a_driver
 * @ClassName NoScrollListView.java
 * @Description 可以放入ScrollView的ListView
 * @author
xugang * @date 2015-9-23 上午1:06:58 */
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); } @Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }

GridView

package com.work.driver.view;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.GridView;

/**
 * @project a_driver
 * @ClassName NoScrollGridView.java
 * @Description  巢狀在listView 中的GridView
 * @author xugang
 * @date 2015-9-23 下午12:56:43
 */
public class NoScrollGridView extends GridView {
    public NoScrollGridView(Context context) {
        super(context);
    }

    public NoScrollGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public NoScrollGridView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }
}

但是這樣有問題,當資料很多的時候,ListView或者GridView上面的View會滑出螢幕,這裡需要寫三行程式碼,找到上面的任意一個View:

        View view = findViewById(R.id.textView1);// find view
        view.setFocusable(true);  
        view.setFocusableInTouchMode(true);  
        view.requestFocus();