0

I have a relativeLayout above my listView. And the relativeLayout moves up at the same time as the listView is scrolling up. For example If the relativeLayout height is 500px, the listView padding becomes 500px and the relativeLayout stays in the padding part. When list view is scrolled up the relativeLayout goes up also in the same pixels.

Everything works fine if the listView items are created. However if the relativeLayout height is higher than the visible area, the padding becomes so high that the listView items are not created. Because only visible part of the list view is created by calling getView. And scrolling up is not working as there are no items in the list.

So if I can load also non-visible items that are below the padding area everything would work fine. Is there anyway to do this or create the getView call manually with a for loop?

enter image description here

Below is the code that I use.

    private void reloadData() {

    adapter = new MyListAdapter();
    list = (ListView) view.findViewById(R.id.fragment_commenters_myListView);
    list.setAdapter(adapter);
    list.setPadding(0, movingRelativeayout.getHeight(), 0, 0);


    list.setOnScrollListener(new AbsListView.OnScrollListener() {

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            // TODO Auto-generated method stub
            this.isScrollCompleted();
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                             int visibleItemCount, int totalItemCount) {
            // TODO Auto-generated method stub


            if (list.getChildAt(0) != null) {


                View listChildView = list.getChildAt(0);
                int scrollPosition = -listChildView.getTop() + list.getFirstVisiblePosition() * listChildView.getHeight();

                if (scrollPosition < 5000) {

                    RelativeLayout.LayoutParams params1 = (RelativeLayout.LayoutParams) movingRelativeayout.getLayoutParams();
                    params1.setMargins(Math.round(0 * dipToPixel), Math.round(-scrollPosition) - movingRelativeayout.getHeight(), Math.round(0 * dipToPixel), Math.round(0 * dipToPixel));
                    movingRelativeayout.setLayoutParams(params1);

                }

            }

        }

        private void isScrollCompleted() {

        }
    });



}


private class MyListAdapter extends ArrayAdapter<String>  {
    public MyListAdapter() {
        super(getActivity().getApplicationContext(), R.layout.fragment_commenters_cell, myItemList);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View cellView = convertView;


        if (cellView == null){
            cellView = getActivity().getLayoutInflater().inflate(R.layout.fragment_commenters_cell, parent, false);
        }

        cellProfileBtn = (Button) cellView.findViewById(R.id.fragment_commenters_cell_profileBtn);
        cellProfileImage = (ImageView) cellView.findViewById(R.id.fragment_commenters_cell_profileImg);




        cellProfileImage.setImageBitmap(resultsImageBitmapFiles.get(position));


        cellProfileBtn.setText(resultsNameArray.get(position));




        return cellView;

    }

}
saner
  • 821
  • 2
  • 10
  • 32
  • I don't think that you should do it like that. Better have a look at NestedScrollView and/or CoordinatorLayout. They are built to handle scenarios like yours. And you really should use RecyclerView instead of ListView. It's so much better and easier to use. – Ridcully Apr 29 '20 at 05:06
  • @Ridcully is correct, migrating to RecyclerView is a good idea. But either way, if the RelativeLayout content should always stay on top of the list and even disappear if the list is long enough, then how about providing different View types for the ListView (or RecyclerView) and treat the RelativeLayout content like another list item? – Bö macht Blau Apr 29 '20 at 05:31
  • @BömachtBlau Thank you for the advice? How can I treat the relative layout like another list item at the top? – saner Apr 29 '20 at 05:40
  • If you want to keep the ListView: [Android ListView with different layouts for each row](https://stackoverflow.com/questions/4777272/android-listview-with-different-layouts-for-each-row) – Bö macht Blau Apr 29 '20 at 05:42
  • And for RecyclerView: [How to create RecyclerView with multiple view type?](https://stackoverflow.com/questions/26245139/how-to-create-recyclerview-with-multiple-view-type) – Bö macht Blau Apr 29 '20 at 05:46
  • I will have to reorganize lots of my files. It will take long time. So isn't there a way to load the invisible items of a listview? – saner Apr 29 '20 at 05:50
  • Personally, I think attempting to interfere with the internal workings of ListView is a bad idea. OK, the code is not likely to change since today RecyclerView is the recommended choice for implementing a list. But even so, learning how exactly to manipulate the ListView without unwanted side effects will also take some time. And leave you with code which is difficult to maintain because few people really understand it... How many files can there be? Some layouts, an Adapter, a Fragment? I'd take that approach if it were my code. But of course it's up to you :) – Bö macht Blau Apr 29 '20 at 06:18

0 Answers0