android勾選控制項 Android滑動選擇控制項

2021-10-17 06:33:27 字數 2859 閱讀 5894

前言

最近做了個滑動選擇的小控制項,拿出來給大家分享一下,先上圖

執行效果

執行效果圖

實現步驟

這裡分解為3個動作:down、move、up來進行分析,博主文采不好,大家直接看流程圖吧!!

down事件分析

move事件分析

up事件分析

**分析

talk is cheap, show me code!!!

前置知識

這個地方使用的是recyclerview的**,使用recyclerview只能使用linearlayoutmanager,listview的執行效果稍微要比recyclerview差一些

//這裡使用dispatchtouchevent,因為ontouchevent容易被ontouchlistener擷取

@override

public boolean dispatchtouchevent(motionevent ev) else else else else else else if ((currentposition + mfirstvisibleposition) < (mpreposition + mprefirstvisibleposition) && !mupordown) else if (currentposition < 2 && currenty < mprey) {

//自動向上滑

view child = getchildat(currentposition);

log.d(log_tag, "dispatchtouchevent() move mcount: " + mcount);

//mcount用於降低滑動的頻率,頻率太快容易滑動的看不清楚

if (null != child && 0 == mcount % 5) {

scrolltoposition(mfirstvisibleposition - 1);

mcount++;

mprey = currenty;

mpreposition = currentposition;

mprefirstvisibleposition = mfirstvisibleposition;

return true;

break;

case motionevent.action_up:

if (misneedscrollcheck) {

mcount = 0;

return false;

break;

return super.dispatchtouchevent(ev);

其他的**片段

//改變開始的checkbox狀態

private void setstartcheckboxstate() {

view child = getchildat(mstartposition);

if (null != child) {

viewgroup checkboxcontainer = (viewgroup) child.findviewwithtag("checkbox_layout");

if (null != checkboxcontainer) {

checkbox checkbox = (checkbox) checkboxcontainer.getchildat(0);

if (null != checkbox && checkbox.getvisibility() == visible) {

checkbox.toggle();

//判斷當前item的position,相對位置

private int pointtoposition(int x, int y) {

rect frame = mtouchframe;

if (frame == null) {

mtouchframe = new rect();

frame = mtouchframe;

final int count = getchildcount();

for (int i = count - 1; i >= 0; i--) {

final view child = getchildat(i);

if (child.getvisibility() == view.visible) {

child.gethitrect(frame);

if (frame.contains(x, y)) {

return i;

return -1;

//改變position的選中狀態

public void changecheckboxstate(int position) {

if (position < 0 || position >= getchildcount()) {

return;

view child = getchildat(position);

if (null != child) {

viewgroup checkboxlayout = (viewgroup) child.findviewwithtag("checkbox_layout");

if (null != checkboxlayout && checkboxlayout.getvisibility() == visible) {

checkbox checkbox = (checkbox) checkboxlayout.getchildat(0);

if (null != checkbox) {

log.d(log_tag, "changecheckboxstate() selectcheckbox: " + position);

checkbox.toggle();

原始碼

jQuery 控制勾選和去勾選

input ratio勾選控制 id prop checked true 勾選 id prop checked false 去勾選注意 一定用prop而不是用attr來控制。目前專案使用的jq版本是3.1.1,在這個版本中,類似於 id attr checked checked id removea...

Extjs checkbox Grid勾選時事件

var sm new ext.grid.checkboxselectionmodel sm.handlemousedown ext.emptyfn 不響應mousedown事件 sm.on rowselect function sm rowindex,record this sm.on rowdes...

Ztree勾選節點後取消勾選其父子節點

前言 ztree官方給的api可以設定勾選乙個節點的同時勾選子節點或者父節點,也可以設定不影響父子節點,即將chkboxtype設定為,而有時我們希望實現這樣的功能,點選乙個節點判斷他所有的父節點和子節點,如果有選中的話就取消勾選,這樣的話官方貌似沒有給出有效的解決方案,這篇文章記錄一下我是怎麼解決...