ImageView标记丢失

时间:2017-07-25 09:47:17

标签: android imageview relativelayout

我正在为最多四名玩家制作一个基本的纸牌游戏,我有一个问题,我无法解决。

我有几个布局堆叠在一起(match_parent)。

我已将图像视图添加到每个布局,并在将cardView传递给下一个方法之前为其添加了标记。

 void animateCard(int finalX, int finalY, final ImageView cardView, final String playerId, final int n){

    int duration = 180;

    ObjectAnimator animX = ObjectAnimator.ofFloat(cardView, "x", finalX);
    ObjectAnimator animY = ObjectAnimator.ofFloat(cardView, "y", finalY);

   animX.setDuration(duration);
   animY.setDuration(duration);

   // animX.start();

    AnimatorSet animSetXY = new AnimatorSet();
    animSetXY.playTogether(animX, animY);
    animSetXY.start();

    animSetXY.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {

        }

        @Override
        public void onAnimationEnd(Animator animation) {


            float cardX = cardView.getX();
            float cardY = cardView.getY();



                if (playerId.equals("human")) {


                }

                else if (playerId.equals("topPlayer")){


                    Log.e("TopPlayerHAnd: ", String.valueOf(topPlayerHand));

                    Log.e("cardViewGetTag: ", String.valueOf(cardView.getTag()));

                    Log.e("parent: ", String.valueOf(cardView.getParent()));

                    Log.e("TPAL tag at 0: ", String.valueOf(topPlayerAreaLayout.getChildAt(0).getTag()));


                }





        }

        @Override
        public void onAnimationCancel(Animator animation) {

        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }
    });


}

问题是尝试通过调用Log.e来获取标记(“TPAL tag at 0:”,String.valueOf(topPlayerAreaLayout.getChildAt(0).getTag()));正在返回“null”。

然而,Log.e(“cardViewGetTag:”,String.valueOf(cardView.getTag()));

返回实际标记和

Log.e(“parent:”,String.valueOf(cardView.getParent()));

记录android.widget.RelativeLayout {7133a69 VE ..... ...... ID 0,0-1184,720#7f0c0061 app:id / topPlayerAreaLayout}作为找到cardView的布局。< / p>

topPlayerAreaLayout.getChildCount();返回正确数量的子项,但没有标记!

我已经连续几天撞到了墙上,但没有结果。

编辑:这是我为视图设置标签的方法: edit2:如果playerId.equals(“human”)

,标签将完整无缺地通过animateCard方法
 void addCardsToTopAndBottomHands(final String whichPlayer, final RelativeLayout thisLayout, final int initialCardX, final int initialCardY, ArrayList playerDeck, int totalCardsToAdd){

        Log.e("addCardsLayout: ", String.valueOf(thisLayout));

    int n = 0;

    String cardTag = "";

    while( n< totalCardsToAdd){

        final int q = n;

        int duration = 180 *(n+1);

        int multiplier = 0;

        final ImageView thisCardView = new ImageView(this);

        playerDeck.add(thisDeck.get(0));
        String cardTag = String.valueOf(thisDeck.get(0));



        thisDeck.remove(0);
        thisCardView.setTag(cardTag);


        if (whichPlayer.equals("human")) {

            multiplier =((playingCardWidth*(q+1))+9);


            final int cardId = getResources().getIdentifier(String.valueOf(playerDeck.get(n)), "drawable", getPackageName());


            thisCardView.setImageResource(cardId);


        }

        else {
            thisCardView.setImageResource(R.drawable.cardback);

            multiplier = halfCardWidth *(q+1);

            Log.e("playerName: ", whichPlayer);
            Log.e("playerDeck: ", String.valueOf(playerDeck));

        }

        final int cardXCoord = initialCardX + multiplier;
        final int cardYCoord = initialCardY + multiplier;





        CountDownTimer countDownTimer = new CountDownTimer(duration, duration) {
            @Override
            public void onTick(long millisUntilFinished) {


            }

            @Override
            public void onFinish() {



                thisLayout.addView(thisCardView);

                Log.e("playerName: ", whichPlayer);
                Log.e("counting in: ", String.valueOf(thisLayout.getChildCount()));
                Log.e ("thisCardTag: ", String.valueOf(thisCardView.getTag()));
                Log.e("thisLayout: ", String.valueOf(thisLayout));

                Log.e("TPAL count: ", String.valueOf(topPlayerAreaLayout.getChildCount()));
                String message = "Tag at " + String.valueOf(q);

                Log.e(message, String.valueOf(topPlayerAreaLayout.getChildAt(q).getTag()));


                //TODO: TPAL imageView tags are intact here! WTF did they go?


                thisCardView.requestLayout();
                thisCardView.setX(centreCardX);
                thisCardView.setY(centreCardY);
                thisCardView.getLayoutParams().height = playingCardHeight;
                thisCardView.getLayoutParams().width = playingCardWidth;

               // Log.e("this q: ", String.valueOf(q) );

              if (whichPlayer.equals("human")|| whichPlayer.equals("topPlayer")) {

                    animateCard(cardXCoord, initialCardY, thisCardView, whichPlayer, q);

                }

                else if (whichPlayer.equals("leftPlayer")|| whichPlayer.equals("rightPlayer")){

                          animateCard(initialCardX, cardYCoord, thisCardView, whichPlayer, q );

              }

            }
        }; countDownTimer.start();

        n++;

    }

}

0 个答案:

没有答案