2016-05-08 12 views
1

私は垂直に並べられたアイテムのリストを持っています。各項目には、CheckBoxとTextViewがあります。 CheckBoxのLongClick動作を実現しようとしています。もっと正確には、LongClickedがある間はゴミ箱アイコンでそれを変更します。リリース時には、もう一度checkBoxになる必要があります。また、単純なクリックでCheck/Uncheck動作を維持します。CheckBox OnLongClick動作

どうすれば実現できますか?いくつのアプローチがありますか? 私は助けの王を懇願する!


私はちょうどsetBackgroud()関数を使用しようとしましたが、それはただのボックスがまだ上に残っていると、既存のチェックボックスのビューの下に引きます。

cb.setOnLongClickListener(new View.OnLongClickListener() { 
      @Override 
      public boolean onLongClick(View v) { 
       Resources res = tmp_context.getResources(); 
       Drawable drawable = res.getDrawable(R.mipmap.ic_trash); 
       cb.setBackground(drawable); 
       return false; 
      } 
     }); 
+0

@ ObscureGeek setBackgroud()を使用しようとしましたが、チェックボックスの画像が上に残り、下に描画されます。 – Starivore

答えて

0

UPDATE

まずでframeLayoutの内側にあなたのchekcboxを入れて:

<FrameLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/one" 
    android:id="@+id/fl"> 
    <CheckBox 

     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Two" 
     android:longClickable="true" 
     android:id="@+id/two"/> 
</FrameLayout> 

あなたはLongClickListenerとOnTouchListener使用することができますあなたの中

private View.OnLongClickListener pressHoldListener = new View.OnLongClickListener() { 

    @Override 
    public boolean onLongClick(View pView) { 
     // Do something when your hold starts here. 
     isSpeakButtonLongPressed = true; 
     mFrameLayout.setBackgroundResource(R.drawable.trash_can); 
     yourCheckBox.setVisibility(VIEW.INVISIBLE); 
     return true; 
    } 
}; 

private View.OnTouchListener pressTouchListener = new View.OnTouchListener() { 

    @Override 
    public boolean onTouch(View pView, MotionEvent pEvent) { 
     pView.onTouchEvent(pEvent); 
     // We're only interested in when the button is released. 
     if (pEvent.getAction() == MotionEvent.ACTION_UP) { 
      // We're only interested in anything if our speak button is currently pressed. 
      if (isSpeakButtonLongPressed) { 
       // Do something when the button is released. 
       yourCheckBox.setVisibility(View.VISIBLE); 
       mFrameLayout.setBackground(null); 
       isSpeakButtonLongPressed = false; 
      } 
     } 
     return false; 
    } 
}; 

のonCreate():

yourCheckBox.setOnLongClickListener(pressHoldListener); 
yourCheckBox.setOnTouchListener(pressTouchListener); 

また、あなたのチェックボックスのXMLに

android:longClickable="true" 

を追加します。

+0

@Akihil Somanありがとうございますが、setBackgroundResourceは引き続きCheckBoxのバックグラウンドのみを設定します。チェックボックスは上部にそのまま残ります。 – Starivore

+0

chexkboxをframelayoutの中に入れて、長いクリックの間に背景をframelayoutに設定し、チェックボックスを見えなくすることができます。 framelayoutの設定された背景をnullに解放し、チェックボックスを表示させるとき。私は自分のコードを更新しました。 –

+0

Soamanありがとうございます!しかし、私はバグを生成しました。また、チェックボックスにsetOnCheckedChangeListenerがあり、チェックボックスをオンにするとonCheckedChangedが2回呼び出されます。したがって、ワンクリックでチェックとアンチェックが行われます。あなたはこのバグを解決する方法を知っていますか? – Starivore

関連する問題