0

各セルに bg_button というカスタム ボタンを含むグリッドビューがあります。私はボグルのようなゲームを作成しようとしていますが、まだ Android の初心者です。この問題について1週間以上インターネットで検索していましたが、まだ何も得られませんでした.

問題は、 touch_down のときに特定のアイテムを問題なく取得できますが、斜めに移動し始めると、不要なグリッドアイテムが複数取得されることです。例えば;

アフト

クトゥル

TRSV

じょうく

上にあるグリッドで、T に触れてから O に移動しようとすると、次のようになります。

T -> J -> O または T -> R -> O

私はJもRも欲しくないが、それにも触れている。パディング、または垂直方向と水平方向の間隔を変更しようとしましたが、問題は同じままでした。この問題について私を助けていただけますか、少なくともこれを行う方法、または少なくとも私がグーグルで検索して役立つ情報を見つけることができる特定のタグを教えていただけますか? お時間をいただきありがとうございました。

これは、タッチ イベントのコードの一部です。ArrayList へのパスを保存していますが、コードが乱雑で申し訳ありません。ハードコーディングが完了したら、クリーンアップします。

 final ArrayList<Integer> myList = new ArrayList<Integer>();
        gridView = (GridView) this.findViewById(R.id.gridFriends);
        MyAdapter gridAdapter = new MyAdapter(Boggler.this,board_1d);
        gridView.setAdapter(gridAdapter);
        gridView.setOnTouchListener(new View.OnTouchListener(){
            @Override
            public boolean onTouch(View v, MotionEvent event)  {


                    final GridView layout = (GridView)v;
                    int action = event.getActionMasked();
                    float currentXPosition = event.getX();
                    float currentYPosition = event.getY();
                    int position = gridView.pointToPosition((int) currentXPosition, (int) currentYPosition);
                    // position = layout.pointToPosition( (int)event.getX(), (int)event.getY() );

                     while(position == -1)
                         position=event.getAction();



                     View v2 =layout.getChildAt(position);



                         myList.add(position);
                        Bg_button bt = (Bg_button) v2.findViewById(R.id.grid_item);

                       bt.setPressed(true);




                         Log.d(this.getClass().getName(), String.format("Over view.id[%d]", position));

                         if (action == MotionEvent.ACTION_MOVE){



                             myList.add(position);


                            return true;

                         }


                        if (action == MotionEvent.ACTION_UP) {

                            Log.d(this.getClass().getName(), myList.toString());
                            int i=0,j=0;
                            int state = 0;
                            Object[] st = myList.toArray();
                            for (Object s : st) {
                                if (myList.indexOf(s) != myList.lastIndexOf(s)) {
                                    myList.remove(myList.lastIndexOf(s));}

                                    else {  
                                     v2 =layout.getChildAt(myList.get(myList.lastIndexOf(s)));

                                     bt = (Bg_button) v2.findViewById(R.id.grid_item);
                                     bt.setPressed(false);
                                       name = name + bt.getText();



                                     Log.d(this.getClass().getName(), name);

                                   }
                            }

そして、これは私がbutton_bogglerを使用しているxmlファイルです:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.example.proje_test.bg_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<com.example.proje_test.Bg_button
    android:id="@+id/grid_item"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:layout_gravity="center"
    android:clickable="false"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:background="@drawable/color_bg_selector"
    android:textSize="50dp" 

/>

</LinearLayout>

そして activity_boggler:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >

<GridView
    android:id="@+id/gridFriends"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:clipChildren="true"
    android:columnWidth="100dp"
    android:gravity="center"
    android:numColumns="4"
    android:scrollbars="none"
    android:stretchMode="columnWidth" >

</GridView>
 <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/feedback"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />

</LinearLayout>
4

1 に答える 1

0

私のために働く解決策を見つけました。この種の問題について、回答のない同じ質問をいくつか見たことがあります。そのため、自分の質問に答えて、この種の問題を抱えている人に役立つかもしれません。Bg_button と呼ばれるさまざまな属性を持つカスタム ボタン クラスを作成しました。

public class Bg_button extends Button {

private static final int[] STATE_C = {R.attr.state_chosen};
private static final int[] STATE_R = {R.attr.state_right};
private static final int[] STATE_W = {R.attr.state_wrong};
public boolean mIschosen = false;
public boolean mIsright = false;
public boolean mIswrong = false;

public Bg_button(Context context, AttributeSet attrs) {
    super(context, attrs);
}

    @Override
    protected int[] onCreateDrawableState(int extraSpace) {
       final int[] drawableState = super.onCreateDrawableState(extraSpace + 3);
       if (mIschosen) {
           mergeDrawableStates(drawableState, STATE_C);
               }
       if (mIsright) {
           mergeDrawableStates(drawableState, STATE_R);
               }
       if (mIswrong) {
           mergeDrawableStates(drawableState, STATE_W);
               }
       return drawableState;
          }

       public void setchosen(boolean ischosen) {mIschosen = ischosen;
           refreshDrawableState();}
       public void setright(boolean isright) {mIsright = isright;
           refreshDrawableState();}
       public boolean setwrong(boolean iswrong) {mIswrong = iswrong;
           refreshDrawableState();
       return true;}

       @Override
       public void getHitRect(Rect outRect) {
     outRect.set(getLeft() + 20, getTop() + 20, getRight() - 20, getBottom() - 20);
                 }
               }

したがって、私が見つけた解決策は、ボタンのタッチ領域を制限して、インターセプトしないようにすることです。これがどれほど倫理的かはわかりませんが、今はうまくいっています。

@Override
    public void getHitRect(Rect outRect) {
        outRect.set(getLeft() + 20, getTop() + 20, getRight() - 20, getBottom() - 20);
    }
}

これは、これまでのところ私が思いつくことができる最良の答えです。ボタンを拡張するために行うトランザクションを逆にして、ボタンのタッチ領域を中央に制限しました。上記の 3 行のコードでうまくいきました。これが役立つことを願っています。

于 2014-06-18T16:48:01.710 に答える