Created
February 1, 2012 19:03
-
-
Save shisashi/1718672 to your computer and use it in GitHub Desktop.
AndroidのViewに、長押ししたらクリック処理をリピートする処理を付加するアダプタ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.shisashi.android.widget; | |
import android.os.Handler; | |
import android.view.MotionEvent; | |
import android.view.View; | |
import android.view.View.OnLongClickListener; | |
import android.view.View.OnTouchListener; | |
public class LongClickRepeatAdapter { | |
/** | |
* 連続してボタンを押す間隔のデフォルト値 (ms) | |
*/ | |
private static final int REPEAT_INTERVAL = 100; | |
/** | |
* Viewに長押し時のリピート処理を付加する。 リピート間隔は100ms。 | |
* | |
* @param view | |
* 付加対象のView | |
*/ | |
public static void bless(View view) { | |
bless(REPEAT_INTERVAL, view); | |
} | |
/** | |
* リピート間隔を指定して、Viewに長押しリピート処理を付加する | |
* | |
* @param repeatInterval | |
* 連続してボタンを押す間隔(ms) | |
* @param view | |
* 付加対象のView | |
*/ | |
public static void bless(final int repeatInterval, final View view) { | |
final Handler handler = new Handler(); | |
final BooleanWrapper isContinue = new BooleanWrapper(false); | |
final Runnable repeatRunnable = new Runnable() { | |
@Override | |
public void run() { | |
// 連打フラグをみて処理を続けるか判断する | |
if (!isContinue.value) { | |
return; | |
} | |
// クリック処理を実行する | |
view.performClick(); | |
// 連打間隔を過ぎた後に、再び自分を呼び出す | |
handler.postDelayed(this, repeatInterval); | |
} | |
}; | |
view.setOnLongClickListener(new OnLongClickListener() { | |
@Override | |
public boolean onLongClick(View v) { | |
isContinue.value = true; | |
// 長押しをきっかけに連打を開始する | |
handler.post(repeatRunnable); | |
return true; | |
} | |
}); | |
// タッチイベントを乗っ取る | |
view.setOnTouchListener(new OnTouchListener() { | |
@Override | |
public boolean onTouch(View v, MotionEvent event) { | |
// キーから指が離されたら連打をオフにする | |
if (event.getAction() == MotionEvent.ACTION_UP) { | |
isContinue.value = false; | |
} | |
return false; | |
} | |
}); | |
} | |
private static class BooleanWrapper { | |
public boolean value; | |
public BooleanWrapper(boolean value) { | |
this.value = value; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:orientation="vertical" | |
android:gravity="center_vertical|center_horizontal" | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent"> | |
<Button | |
android:id="@+id/button" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Increment" /> | |
<TextView | |
android:id="@+id/txtNumber" | |
android:text="0" | |
android:textSize="32sp" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" /> | |
<ImageButton | |
android:id="@+id/imageButton" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:src="@drawable/ic_launcher" | |
android:text="Decrement" /> | |
<ToggleButton | |
android:id="@+id/toggleButton" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="ToggleButton" /> | |
<CheckBox | |
android:id="@+id/checkBox" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="CheckBox" /> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.android; | |
import net.shisashi.android.widget.LongClickRepeatAdapter; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.view.View.OnClickListener; | |
import android.widget.Button; | |
import android.widget.CheckBox; | |
import android.widget.CompoundButton; | |
import android.widget.CompoundButton.OnCheckedChangeListener; | |
import android.widget.ImageButton; | |
import android.widget.TextView; | |
import android.widget.ToggleButton; | |
public class MainActivity extends Activity { | |
private TextView txtNumber; | |
private int number = 0; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
txtNumber = (TextView) findViewById(R.id.txtNumber); | |
txtNumber.setText(String.valueOf(number)); | |
Button button = (Button) findViewById(R.id.button); | |
button.setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
increment(); | |
} | |
}); | |
ImageButton imageButton = (ImageButton) findViewById(R.id.imageButton); | |
imageButton.setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
decrement(); | |
} | |
}); | |
final ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton); | |
final CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox); | |
toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() { | |
@Override | |
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { | |
checkBox.setChecked(isChecked); | |
} | |
}); | |
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() { | |
@Override | |
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { | |
toggleButton.setChecked(isChecked); | |
} | |
}); | |
LongClickRepeatAdapter.bless(button); | |
LongClickRepeatAdapter.bless(imageButton); | |
LongClickRepeatAdapter.bless(toggleButton); | |
LongClickRepeatAdapter.bless(checkBox); | |
} | |
private void increment() { | |
number++; | |
txtNumber.setText(String.valueOf(number)); | |
} | |
private void decrement() { | |
number--; | |
txtNumber.setText(String.valueOf(number)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment