首页 > 解决方案 > 自定义 StackLayout 膨胀的对象比预期的要多

问题描述

我有一个自定义 FrameLayout 视图组,它具有“addCard()”方法,该方法将另一个自定义 FrameLayout 子项添加到其中,称为 TinderCardView。

这是功能 -

public void addCard(TinderCardView tinderCardView) {
    if (onCardSwipedListener == null)
      onCardSwipedListener = tinderCardView.getOnCardSwipedListener();

    topCardOnStack = tinderCardView;

    ViewGroup.LayoutParams layoutParams;
    layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

    int childCount = getChildCount();
    addView(tinderCardView, 0, layoutParams);

    float scaleValue = 1 - (childCount / 50.0f);

    tinderCardView.animate()
        .x(0)
        .y(childCount * yMultiplier)
        .scaleX(scaleValue)
        .setInterpolator(new AnticipateOvershootInterpolator())
        .setDuration(DURATION);
  }

当我需要实例化一张新卡时,问题就开始了,它需要一个接口来确定交换卡时发生的情况 -

  public TinderCardView(Context context, OnCardSwipedListener onCardSwipedListener) {
    super(context);
    this.onCardSwipedListener = onCardSwipedListener;
    init(context, null);
  }

public interface OnCardSwipedListener {

  void send(Object object);
  void onNext(Integer integer);

}

所以,在我的 MainActivity 中,我尝试设置一个新的堆栈布局,它创建了更多的子视图,然后它应该 -

@Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    observeLiveData();

    initViewsAndListeners();
  }

  private void initViewsAndListeners() {
    tinderStackLayout = findViewById(R.id.activity_main_tinder_stack_layout);
    mDeleteButton = findViewById(R.id.activity_main_delete_button);
    mPassButton = findViewById(R.id.activity_main_pass_button);
    mApproveButton = findViewById(R.id.activity_main_approve_button);
    mDeleteButton.setOnClickListener(this);
    mApproveButton.setOnClickListener(this);
    mPassButton.setOnClickListener(this);
    listener = new OnCardSwipedListener() {
      @Override
      public void send(Object object) {

      }

      @Override
      public void onNext(Integer integer) {


        if (integer == 1) {
          addCards(1);
        }

      }
    };
  }

private void observeLiveData() {
    userViewModel = ViewModelProviders.of(this).get(UserViewModel.class);
    userViewModel.getAllUsers().observe(this, users -> {
      //update a new list of users
      usersList = (ArrayList) users;
      addCards(-1);
    });
  }

  private void addCards(int stackSizeToAdd) {
    TinderCardView tinderCardView;

    for (int i = index; index < i + (STACK_SIZE + stackSizeToAdd); index++) {
      if (index >= usersList.size()) {
        index = 0;
        i = 0;
        addCards(-1);
      }
      tinderCardView = new TinderCardView(this, listener);
      tinderCardView.bind(usersList.get(index));
      tinderStackLayout.addCard(tinderCardView);
    }
  }

我肯定我的循环做错了什么,但我不知道是什么。

我一开始的变量 -

private static final int STACK_SIZE = 2;

  private TinderStackLayout tinderStackLayout;
  private OnCardSwipedListener listener;
  private UserViewModel userViewModel;
  private ArrayList<User> usersList;
  private int index = 0;

所以我的问题实际上是比需要多一张卡。启动应用程序时的第一张卡被复制,所以这就是为什么我没有得到正确的堆栈大小。但是在刷卡和删除一些卡的时候,就没有更多的重复了。所以我的问题是理解为什么第一张卡被复制。

标签: android

解决方案


    for (int i = index; index < i + (STACK_SIZE + stackSizeToAdd); index++) {

假设 index 在开始时为 0,而你这样做addCard(-1)

索引为 0,stackSizeToAdd = -1,STACK_SIZE = 10

第一次迭代

| i | index | index < i + (STACK_SIZE + stackSizeToAdd)| 
------------------------------------------------------
  0     0       0< 0 + (10-1)   
  0     1       1 < 0 + (10-1)
  0     2       2 < 0 + (10-1)
  0     3       3 < 0 + (10-1)
    ....
  0     9       9 < 0 + (10-1) => STOP

此迭代将再运行 9 次

如果索引在开始时为 1

   | i | index | index < i + (STACK_SIZE + stackSizeToAdd)| 
    ------------------------------------------------------
      1     1       1 < 1 + (10-1)
      1     2       2 < 1 + (10-1)
      1     3       3 < 1 + (10-1)
        ....
      1     10     10 < 1 + (10-1) => STOP
       

index = 1,stackSizeToAdd = -1,STACK_SIZE = 4

    | i | index | index < i + (STACK_SIZE + stackSizeToAdd)| 
    ------------------------------------------------------
      1     1       1 < 1 + (4-1)
      1     2       2 < 1 + (4-1)
      1     3       3 < 1 + (4-1)
      1     4       4 < 1 + (4-1) => STOP

推荐阅读