GeezerVison is showing github.com — 仿新浪投票控件 · loosaSH/AndroidCustomView@d96d599
File treeExpand file treeCollapse file tree Open diff view settings java/com/allen/androidcustomview
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +44Lines changed: 44 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package com.allen.androidcustomview.activity | | 2 | + | | 3 | +import android.os.Bundle | | 4 | +import android.support.v7.app.AppCompatActivity | | 5 | +import com.allen.androidcustomview.R | | 6 | +import com.allen.androidcustomview.bean.VoteBean | | 7 | +import com.allen.androidcustomview.bean.VoteOption | | 8 | +import com.allen.androidcustomview.data.getMockData | | 9 | +import com.allen.androidcustomview.widget.vote.VoteLayoutAdapter | | 10 | +import kotlinx.android.synthetic.main.activity_sina_vote.* | | 11 | + | | 12 | +/** | | 13 | + * <pre> | | 14 | + * @author : Allen | | 15 | + | | 16 | + * date : 2019/08/06 | | 17 | + * desc : | | 18 | + * </pre> | | 19 | + */ | | 20 | +class SinaVoteActivity : AppCompatActivity(), VoteLayoutAdapter.OnVoteClickListener { | | 21 | + | | 22 | + var voteLayoutAdapter: VoteLayoutAdapter? = null | | 23 | + | | 24 | + override fun onCreate(savedInstanceState: Bundle?) { | | 25 | + super.onCreate(savedInstanceState) | | 26 | + setContentView(R.layout.activity_sina_vote) | | 27 | + voteLayoutAdapter = VoteLayoutAdapter(vote_ll) | | 28 | + voteLayoutAdapter?.setData(getMockData()) | | 29 | + voteLayoutAdapter?.onVoteClickListener = this | | 30 | + } | | 31 | + | | 32 | + override fun onDestroy() { | | 33 | + super.onDestroy() | | 34 | + voteLayoutAdapter?.onDestroy() | | 35 | + } | | 36 | + | | 37 | + override fun onVoteCommitBtnClick(mainVote: VoteBean?, optionIds: ArrayList<Int>, position: Int) { | | 38 | + voteLayoutAdapter?.refreshDataAfterVotedSuccess(position) | | 39 | + } | | 40 | + | | 41 | + override fun onVoteItemClick(mainVote: VoteBean?, voteOption: VoteOption?, position: Int) { | | 42 | + } | | 43 | + | | 44 | +} |
Collapse file Copy file name to clipboard +26Lines changed: 26 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package com.allen.androidcustomview.bean | | 2 | + | | 3 | + | | 4 | +/** | | 5 | + * <pre> | | 6 | + * @author : Allen | | 7 | + | | 8 | + * date : 2019/08/01 | | 9 | + * desc : | | 10 | + * </pre> | | 11 | + */ | | 12 | + | | 13 | +class VoteBean(val id: Int = 0, | | 14 | + val title: String?, | | 15 | + val choiceType: String?, | | 16 | + val maxSelect: Int?, | | 17 | + var voted: Boolean?, | | 18 | + val sumVoteCount: Int?, | | 19 | + val options: ArrayList<VoteOption>? | | 20 | +) | | 21 | + | | 22 | +data class VoteOption(var id: Int?, | | 23 | + var content: String?, | | 24 | + var voteId: Int?, | | 25 | + var showCount: Int?, | | 26 | + var voted: Boolean?) |
Collapse file Copy file name to clipboard +49Lines changed: 49 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package com.allen.androidcustomview.data | | 2 | + | | 3 | +import com.allen.androidcustomview.bean.VoteBean | | 4 | +import com.allen.androidcustomview.bean.VoteOption | | 5 | +import kotlin.random.Random | | 6 | + | | 7 | +/** | | 8 | + * <pre> | | 9 | + * @author : Allen | | 10 | + * date : 2019/08/06 | | 11 | + * desc : | | 12 | + * </pre> | | 13 | + */ | | 14 | + | | 15 | +fun getMockData(): ArrayList<VoteBean> { | | 16 | + val list: ArrayList<VoteBean> = arrayListOf() | | 17 | + for (i in 0..1) | | 18 | + list.add(getVoteBeanData(i)) | | 19 | + return list | | 20 | +} | | 21 | + | | 22 | +private fun getVoteBeanData(index: Int): VoteBean { | | 23 | + val voteTitle: String = when (index) { | | 24 | + 0 -> "哪吒票房能否突破30亿(多选)" | | 25 | + 1 -> "你觉得谁最火呢?(单选)" | | 26 | + else -> "" | | 27 | + } | | 28 | + return VoteBean(11, voteTitle, if (index == 0) "multiple" else "single", 2, false, Random.nextInt(10000, 20000), getVoteOptionsDatas(index)) | | 29 | +} | | 30 | + | | 31 | +private fun getVoteOptionsDatas(index: Int): java.util.ArrayList<VoteOption>? { | | 32 | + var list: ArrayList<VoteOption> = arrayListOf() | | 33 | + for (i in 0..3) | | 34 | + list.add(getVoteOptionData(index, i)) | | 35 | + return list | | 36 | + | | 37 | +} | | 38 | + | | 39 | +private fun getVoteOptionData(index: Int, i: Int): VoteOption { | | 40 | + | | 41 | + val voteContent: String = when (i) { | | 42 | + 0 -> if (index == 0) "当然可以" else "蔡徐坤" | | 43 | + 1 -> if (index == 0) "估计不能" else "肖战" | | 44 | + 2 -> if (index == 0) "拭目以待" else "李现" | | 45 | + 3 -> if (index == 0) "保持中立" else "邓伦" | | 46 | + else -> "" | | 47 | + } | | 48 | + return VoteOption(i, voteContent, i, Random.nextInt(6666,8888), false) | | 49 | +} |
Collapse file Copy file name to clipboard +88Lines changed: 88 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package com.allen.androidcustomview.widget | | 2 | + | | 3 | +import android.content.Context | | 4 | +import android.graphics.* | | 5 | +import android.util.AttributeSet | | 6 | +import android.view.View | | 7 | +import com.allen.androidcustomview.R | | 8 | + | | 9 | +/** | | 10 | + * <pre> | | 11 | + * @author : Allen | | 12 | + | | 13 | + * date : 2019/07/07 | | 14 | + * desc : | | 15 | + * </pre> | | 16 | + */ | | 17 | +class ArcView : View { | | 18 | + private var mWidth = 0 | | 19 | + private var mHeight = 0 | | 20 | + /** | | 21 | + * 弧形高度 | | 22 | + */ | | 23 | + private var mArcHeight = 0 | | 24 | + /** | | 25 | + * 背景颜色 | | 26 | + */ | | 27 | + private var mBgColor = Color.WHITE | | 28 | + private var mPaint = Paint() | | 29 | + private var mContext: Context? = null | | 30 | + | | 31 | + private var mRect = Rect() | | 32 | + private var mPath = Path() | | 33 | + | | 34 | + constructor(context: Context) : this(context, null) | | 35 | + constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0) | | 36 | + constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) { | | 37 | + initView(context, attrs, defStyleAttr) | | 38 | + } | | 39 | + | | 40 | + private fun initView(context: Context, attrs: AttributeSet?, defStyleAttr: Int) { | | 41 | + val typedArray = context.obtainStyledAttributes(attrs, R.styleable.ArcView) | | 42 | + mArcHeight = typedArray.getDimensionPixelSize(R.styleable.ArcView_arcHeight, 0) | | 43 | + mBgColor = typedArray.getColor(R.styleable.ArcView_bgColor, Color.WHITE) | | 44 | + typedArray.recycle() | | 45 | + | | 46 | + mContext = context | | 47 | + mPaint.style = Paint.Style.FILL | | 48 | + mPaint.color = mBgColor | | 49 | + mPaint.isAntiAlias = true | | 50 | + } | | 51 | + | | 52 | + override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) { | | 53 | + super.onSizeChanged(w, h, oldw, oldh) | | 54 | + mWidth = w | | 55 | + mHeight = h | | 56 | + mRect.set(0, 0, mWidth, mHeight - mArcHeight) | | 57 | + resetPath() | | 58 | + } | | 59 | + | | 60 | + private fun resetPath() { | | 61 | + mPath.reset() | | 62 | + mPath.moveTo(0f, (mHeight - mArcHeight).toFloat()) | | 63 | + mPath.quadTo((mWidth / 2).toFloat(), (mHeight + mArcHeight).toFloat(), mWidth.toFloat(), (mHeight - mArcHeight).toFloat()) | | 64 | + mPath.close() | | 65 | + } | | 66 | + | | 67 | + override fun onDraw(canvas: Canvas?) { | | 68 | + super.onDraw(canvas) | | 69 | + canvas?.drawRect(mRect, mPaint) | | 70 | + canvas?.drawPath(mPath, mPaint) | | 71 | + } | | 72 | + | | 73 | + fun setArcViewBgColor(color: Int) { | | 74 | + mBgColor = color | | 75 | + mPaint.color = color | | 76 | + invalidate() | | 77 | + } | | 78 | + | | 79 | + fun getArcViewBgColor() = mBgColor | | 80 | + | | 81 | + fun setArcViewHeight(height: Int) { | | 82 | + if (height == mHeight) return | | 83 | + mHeight = height | | 84 | + mRect.set(0, 0, mWidth, mHeight - mArcHeight) | | 85 | + resetPath() | | 86 | + invalidate() | | 87 | + } | | 88 | +} |
|