点击自动扩张列表高度

2020-01-08  本文已影响0人  恨水东逝
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

/**
 * 点击自动扩张列表高度
 * @author zhaocheng
 */
public class MarkListView extends ListView implements AdapterView.OnItemClickListener {

    private final String TAG = "MarkListView";

    private boolean isWrapHeight = false;

    /**
     * 列表变化的高度
     */
    private int HEIGHT;

    public MarkListView(Context context) {
        this(context, null);
    }

    public MarkListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOnItemClickListener(this);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return super.dispatchTouchEvent(ev);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (HEIGHT == 0) {
            HEIGHT = h * 2;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (isWrapHeight) {
            wrapHeight(false);
        } else {
            wrapHeight(true);
        }
    }

    /**
     * 扩展列表高度
     * @param wrap
     */
    public void wrapHeight(boolean wrap) {
        if (isWrapHeight != wrap) {
            ViewGroup.LayoutParams lp = getLayoutParams();
            if (wrap) {
                lp.height += HEIGHT;
            } else {
                lp.height -= HEIGHT;
            }
            setLayoutParams(lp);
            isWrapHeight = wrap;
        }
    }
}

上一篇下一篇

猜你喜欢

热点阅读