当项目不是全屏时滚动Recyclerview

时间:2019-07-16 07:40:28

标签: android android-recyclerview

Recyclerview能够在物品不足时滚动

我有此链接,但仍然不知道如何继续进行

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

我知道添加addOnScrollListener将使我能够到达RecyclerView的底部,当只有2个项目时,如何触发onScroll?下面是我的代码

非常感谢帮助

商品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>

适配器

    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;
                    }
                }
            }
        });


    }

已更新

预期结果 Recyclerview会自动将物品填满屏幕

结束问题

我将继续回答这个问题。

感谢响应人员

1 个答案:

答案 0 :(得分:1)

包装您的布局高度,它将正常工作:-

android:layout_height="wrap_content"