Android瀑布流的實現

/**
 * Created by Venn on 2016/4/18.
 */
1 WaterFallStream
 public class WaterFallStream extends ScrollView {

    private Context mContext;
    private LinearLayout llWaterPool;

    private int columns;
    private List<LinearLayout> subChildList;
    private int childCounts;

    private onReachBottomListener reachBottomListener;

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

    public WaterFallStream(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        TypedArray ta = mContext.obtainStyledAttributes(attrs, R.styleable.WaterFallStream);
        columns = ta.getInt(R.styleable.WaterFallStream_columns, 1);
        init();
    }

    public void setReachBottomListener(onReachBottomListener reachBottomListener) {
        this.reachBottomListener = reachBottomListener;
    }

    public int getColumns() {
        return columns;
    }

    private void init() {
        subChildList = new ArrayList<>();
        llWaterPool = new LinearLayout(mContext);
        llWaterPool.setOrientation(LinearLayout.HORIZONTAL);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams
                .MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        llWaterPool.setLayoutParams(params);
        this.addView(llWaterPool);
        for (int i = 0; i < columns; i++) {
            LinearLayout llChild = new LinearLayout(mContext);
            LinearLayout.LayoutParams childParams = new LinearLayout.LayoutParams(0, ViewGroup
                    .LayoutParams.WRAP_CONTENT, 1.0f);
            llChild.setOrientation(LinearLayout.VERTICAL);
            llChild.setLayoutParams(childParams);
            llWaterPool.addView(llChild);
            subChildList.add(llChild);
        }
    }

    public void addSubView(View view) {
        if (!subChildList.isEmpty()) {
            int childIndex = childCounts % columns;
            subChildList.get(childIndex).addView(view);
            childCounts++;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                int height = getHeight();
                int scrollY = getScrollY();
                int mHeight = height + scrollY;
                int contentHeight = llWaterPool.getHeight();
                if (mHeight >= contentHeight) {
                    if (reachBottomListener != null) {
                        reachBottomListener.addMore(this);
                    }
                }
                break;
            default:
                break;
        }
        return super.onTouchEvent(ev);
    }

    public interface onReachBottomListener {
        void addMore(WaterFallStream stream);
    }
}



2 Style
<declare-styleable name="WaterFallStream">
    <attr name="columns" format="integer"></attr>
</declare-styleable>



3 Activity
public class MainActivity extends Activity implements WaterFallStream.onReachBottomListener {

    private WaterFallStream waterFallStream;
    private int columns;

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

        waterFallStream = (WaterFallStream) this.findViewById(R.id.water_stream);
        waterFallStream.setReachBottomListener(this);
        columns = waterFallStream.getColumns();
    }

    @Override
    public void addMore(WaterFallStream stream) {
        for (int i = 0; i < columns; i++) {
            ImageView imageView = new ImageView(this);
            switch (i % columns) {
                case 0:
                    imageView.setImageResource(R.mipmap.image);
                    break;
                case 1:
                    imageView.setImageResource(R.mipmap.head);
                    break;
                case 2:
                    imageView.setImageResource(R.mipmap.image2);
                    break;
                default:
                    break;
            }
            stream.addSubView(imageView);
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章