Help with Scroll Android

Asked

Viewed 68 times

0

Guys, I have a floating button in my view that prints all the items on my listview. What happens is that when I click on the floating button it takes a picture of the first 3 items and does not take the rest, but at the end it updates the listview for the next items. It looks like the code to take the print is faster than the listview update. When I click the floating button the screenshot function is called.

@Override

public void onScrollStateChanged(AbsListView view, int scrollState) {

    if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE){
        printScreen();
    }
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}

public void screenShot (){
    if (list.getFirstVisiblePosition() == 0) {
        printScreen();
    } else {
        list.smoothScrollToPosition(0);
    }
}

public void printScreen(){
    int lastPosition = list.getLastVisiblePosition();
    int j = list.getFirstVisiblePosition();
    for (int i = j; i < lastPosition; i++){
        if (list.getChildAt(i) != null && list.getChildAt(i).isEnabled()) {
            JUtil.saveScreenShot(list.getChildAt(j), "operadora" + j + ".png");
        }
    }
    list.setSelection(lastPosition);
}
  • That line JUtil.saveScreenShot(list.getChildAt(j), "operadora" + j + ".png"); isn’t it a bug? Shouldn’t it be i in place of j being like this JUtil.saveScreenShot(list.getChildAt(i), "operadora" + i + ".png"); ?

  • Yes, I was wrong, but it didn’t solve the problem either. But I already did, I’ll post as it turned out

1 answer

0


I changed the code and it went like this.

@Override

public void onScrollStateChanged(AbsListView view, int scrollState) {
    if (takingPictures && scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE){
        printScreen();
    }
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}

public void screenShot (){
    takingPictures = true;
    nextPicture = 0;
    if (list.getFirstVisiblePosition() == 0) {
        printScreen();
    } else {
        list.smoothScrollToPosition(0);
    }
}

public void printScreen(){
    int lastPosition = list.getLastVisiblePosition();
    int firstPosition = list.getFirstVisiblePosition();
    for (int i = nextPicture; i <= lastPosition; i++){
        if (list.getChildAt(i - firstPosition) != null && list.getChildAt(i - firstPosition).isEnabled()) {
            JUtil.saveScreenShot(list.getChildAt(i - firstPosition), "NomeArquivo" + i + ".png");
        }
    }
    nextPicture = lastPosition + 1;
    if (nextPicture == list.getCount()){
        takingPictures = false;
        list.smoothScrollToPosition(0);
        Toast.makeText(rootView.getContext(), "Imagens salvas", Toast.LENGTH_SHORT).show();
    } else {
        int position = lastPosition + (lastPosition - firstPosition) + 1;
        if (position > list.getCount() - 1) {
            position = list.getCount() - 1;
        }
        list.smoothScrollToPosition(position);
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.