RecyclerView滚动条不会触及屏幕底部

时间:2015-09-18 12:08:04

标签: android scrollbar android-recyclerview

问题:

enter image description here

RecyclerView 完全向下滚动但滚动条不会触及屏幕底部

这是特定于 RecyclerView 与ListView一起发生

关于Stack Overflow的类似问题,我已经尝试过了:

  • 向RecyclerView添加小的底部填充/边距
  • 在末尾添加假视图
  • 从每个项目中删除边距/填充

在花了足够的时间之后,我创造了一个小的可验证的例子。我正在寻找如何让它正常运行并在视图完全向下滚动时触摸底部。

activity_main.xml中

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:id="@+id/fragment"
          android:name="com.package.scrollerexample.MainActivityFragment"
          tools:layout="@layout/fragment_main"
          android:layout_width="match_parent"
          android:layout_height="match_parent"/>

row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="130dp"
              android:background="#05EEFF">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="txt"
        android:id="@+id/txt1"
        android:padding="10dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/txt2"
        android:padding="10dp"/>
</LinearLayout>

fragment_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:fab="http://schemas.android.com/apk/res-auto"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">


            <android.support.v7.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scrollbars="vertical"
                android:id="@+id/recyclerList"/>

</LinearLayout>

RecyclerAdapterBasic

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class RecyclerAdapterBasic extends RecyclerView.Adapter<RecyclerAdapterBasic.ViewHolder> {
    private List<Dummy> mDataSet = new ArrayList<>();

    public void addAll(List<Dummy> data){
        mDataSet.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row,parent,false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Dummy i = mDataSet.get(position);
        holder.txt2.setText(i.getFieldA());
        holder.txt1.setText(i.getFieldB());
    }

    @Override
    public int getItemCount() {
        return mDataSet.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView txt1;
        public TextView txt2;

        public ViewHolder(View itemView) {
            super(itemView);
            txt1 = (TextView) itemView.findViewById(R.id.txt1);
            txt2 = (TextView) itemView.findViewById(R.id.txt2);
        }
    }
}

虚拟

public class Dummy {
    public Dummy() {
    }

    public static String getFieldA(){
        return "12";
    }

    public static String getFieldB(){
        return "Field";
    }
}

MainActivity

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

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

}

MainActivityFragment

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * A placeholder fragment containing a simple view.
 */
public class MainActivityFragment extends Fragment {

    public MainActivityFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_main, container, false);
        RecyclerView recyclerView = (RecyclerView) v.findViewById(R.id.recyclerList);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        RecyclerAdapterBasic adapterBasic = new RecyclerAdapterBasic();
        List<Dummy> list = new ArrayList<Dummy>();
        for(int i = 0; i < 18; i++){
            list.add(new Dummy());
        }
        adapterBasic.addAll(list);
        layoutManager.setSmoothScrollbarEnabled(true);
        recyclerView.setAdapter(adapterBasic);
        recyclerView.setLayoutManager(layoutManager);
        return v;
    }
}

1 个答案:

答案 0 :(得分:0)

这是Issue 78545中提到的一个实际错误,可以通过切换到 21.0.2

版本来修复
compile 'com.android.support:recyclerview-v7:21.0.2'