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);
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值