-1

Recyclerview able to scroll when not enough items

I got this link but yet still no idea how to proceed further

How to force RecyclerView to scroll when there's not enough items to fill the height of the screen enter image description here

I know that adding addOnScrollListener will allow me to get the bottom of the RecyclerView, how do I able to trigger the onScroll when there are only 2 items? below are my codes

help is much appreciated

Item XML

    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardElevation="8dp"
    app:cardUseCompatPadding="true">


    <TextView
        android:id="@+id/tvItem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:text="Item X" />

</android.support.v7.widget.CardView>

Recyclerview Xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".RvAndSV">

    <!--<android.support.v4.widget.NestedScrollView-->
        <!--android:id="@+id/ntsv"-->
        <!--android:layout_width="match_parent"-->
        <!--android:layout_height="wrap_content">-->

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            app:layoutManager="android.support.v7.widget.LinearLayoutManager"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    <!--</android.support.v4.widget.NestedScrollView>-->

</RelativeLayout>

Adapter

    public class RecyclerViewAdapter extends 
    RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final int VIEW_TYPE_ITEM = 0;
    private final int VIEW_TYPE_LOADING = 1;

    public List<String> mItemList;


    public RecyclerViewAdapter(List<String> itemList) {

        mItemList = itemList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == VIEW_TYPE_ITEM) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
            return new ItemViewHolder(view);
        } else {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_loading, parent, false);
            return new LoadingViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {

        if (viewHolder instanceof ItemViewHolder) {

            populateItemRows((ItemViewHolder) viewHolder, position);
        } else if (viewHolder instanceof LoadingViewHolder) {
            showLoadingView((LoadingViewHolder) viewHolder, position);
        }

    }

    @Override
    public int getItemCount() {
        return mItemList == null ? 0 : mItemList.size();
    }

    /**
     * The following method decides the type of ViewHolder to display in the RecyclerView
     *
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        return mItemList.get(position) == null ? VIEW_TYPE_LOADING : VIEW_TYPE_ITEM;
    }


    private class ItemViewHolder extends RecyclerView.ViewHolder {

        TextView tvItem;

        public ItemViewHolder(@NonNull View itemView) {
            super(itemView);

            tvItem = itemView.findViewById(R.id.tvItem);
        }
    }

    private class LoadingViewHolder extends RecyclerView.ViewHolder {

        ProgressBar progressBar;

        public LoadingViewHolder(@NonNull View itemView) {
            super(itemView);
            progressBar = itemView.findViewById(R.id.progressBar);
        }
    }

    private void showLoadingView(LoadingViewHolder viewHolder, int position) {
        //ProgressBar would be displayed

    }

    private void populateItemRows(ItemViewHolder viewHolder, int position) {

        String item = mItemList.get(position);
        viewHolder.tvItem.setText(item);

    }
}

MainActivity

    public class RvAndSV extends AppCompatActivity {

    RecyclerView recyclerView;
    RecyclerViewAdapter recyclerViewAdapter;
    ArrayList<String> rowsArrayList = new ArrayList<>();
    boolean isLoading = false;
    NestedScrollView nestedScrollView;

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

        recyclerView = findViewById(R.id.recyclerView);
//        nestedScrollView = findViewById(R.id.ntsv);
        populateData();
        initAdapter();
        initScrollListener();
        recyclerView.setNestedScrollingEnabled(false);

    }

    private void populateData() {
        rowsArrayList.add("Nigeria");
        rowsArrayList.add("China");
        rowsArrayList.add("USA");
        rowsArrayList.add("Ghana");
        rowsArrayList.add("Canada");
        rowsArrayList.add("Finland");
        rowsArrayList.add("Denmark");
        rowsArrayList.add("Argentina");
//        rowsArrayList.add("Andorra");
//        rowsArrayList.add("Angola");
//        rowsArrayList.add("Benin");
//        rowsArrayList.add("Brazil");
//        rowsArrayList.add("Chile");
//        rowsArrayList.add("Denmark");
//        rowsArrayList.add("Egypt");
//        rowsArrayList.add("Fiji");
//        rowsArrayList.add("France");
//        rowsArrayList.add("Togo");

    }

    private void initAdapter() {

        recyclerViewAdapter = new RecyclerViewAdapter(rowsArrayList);
        recyclerView.setAdapter(recyclerViewAdapter);
    }

    private void initScrollListener() {
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                Toast.makeText(RvAndSV.this, "Scrolled", Toast.LENGTH_SHORT).show();

                LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();

                if (!isLoading) {
                    if (linearLayoutManager != null && linearLayoutManager.findLastCompletelyVisibleItemPosition() == rowsArrayList.size() - 1) {
                        //bottom of list!
//                        loadMore();
                        isLoading = true;
                    }
                }
            }
        });


    }

Updated

Expected result Recyclerview will auto fill the screen with items

Closing the question

I will proceed on closing this question.

Thanks for the response guys

Arduino
  • 285
  • 1
  • 3
  • 17
  • 2
    can you elaborate what you want to achieve, how can one scroll if there are no enough items on the list. – darwin Jul 16 '19 at 07:49

1 Answers1

1

wrap your layout height and it will work fine :-

android:layout_height="wrap_content"
John Joe
  • 12,412
  • 16
  • 70
  • 135
Sandeep Malik
  • 1,972
  • 1
  • 8
  • 17