【问题标题】:RecyclerView not scrolling to the bottomRecyclerView 没有滚动到底部
【发布时间】:2015-08-13 05:04:15
【问题描述】:

我遵循 recyclerview 指南并为我正在制作的应用程序构建了一个,但由于某种原因它没有滚动到底部。我将它与google code sn-ps以及其他在线代码sn-ps进行了比较,看不出有什么区别。我发布了一张图片和我正在使用的代码。我正在使用选项卡,因此 recyclerview 填充在片段中。

应用的外观:

http://imgur.com/H5uOLFR

适配器类:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<Group> groups;

// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public class ViewHolder extends RecyclerView.ViewHolder {
    // each data item is just a string in this case
    public TextView groupName;
    public TextView groupDate;
    public TextView groupLocation;
    public TextView className;

    public ViewHolder(View v) {
        super(v);
        groupName = (TextView) v.findViewById(R.id.groupName);
        groupDate = (TextView) v.findViewById(R.id.groupDate);
        groupLocation = (TextView) v.findViewById(R.id.groupLocation);
        className = (TextView) v.findViewById(R.id.className);
    }
}

/*
 * TODO: finish this method
 */
public void add(int position, String item) {

    notifyItemInserted(position);
}

public void remove(String item) {
    int position = groups.indexOf(item);
    groups.remove(position);
    notifyItemRemoved(position);
}

// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(List<Group> groupsList) {
    groups = groupsList;
    Log.d("TEST", "Number of Groups: " +
            Integer.toString(groups.size()));
}

// Create new views (invoked by the layout manager)
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
    // create a new view
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.group_view, parent, false);
    // set the view's size, margins, paddings and layout parameters
    ViewHolder vh = new ViewHolder(v);
    return vh;
}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    // - get element from your dataset at this position
    // - replace the contents of the view with that element
    final Group group = groups.get(position);
//      holder.groupName.setText(group.getName());
    holder.groupName.setText(group.getName());
    holder.groupDate.setText(group.getFormattedDate());
    holder.groupLocation.setText(group.getLocation());
    holder.className.setText(group.getParent().getName());
}

// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
    return groups.size();
}

}

片段类:

public class groupsFragment extends Fragment implements GroupLeaver, GroupRetriever {
private RecyclerView rv;
private List<Group> groups;
private ProgressDialog progressDialog;

@Override
public void onCreate(Bundle savedInstance){
    super.onCreate(savedInstance);
    Log.d("TEST", "Entered onCreate");
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    AppMain.getController().retrieveGroups(groupsFragment.this);
    Log.d("TEST", "Entered onCreateView");
    View rootView =  inflater.inflate(R.layout.groups_fragment, container, false);

    rv = (RecyclerView) rootView.findViewById(R.id.recyclerView);

    rv.setLayoutManager(new LinearLayoutManager(getActivity()));

    Log.d("TEST", "Size of LIST: " + Integer.toString(groups.size()));
    MyAdapter adapter = new MyAdapter(groups);
    rv.setAdapter(adapter);

    return rootView;
}

@Override
public void onMyGroupsFound(List<Group> groups) {
    Log.d("TEST", "Entered onMyGroupsFound");
    Logg.info(this.getClass(), "Found %d groups for member %s", groups.size(), User.getCurrentUser().getDisplayName());
    this.groups = groups;
}

@Override
public void onGroupLeft(Group oldGroup) {

}

@Override
public void onGroupLeftFailed(Group group, ParseException e) {

}
}

recyclerview的xml布局:

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

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="@null"/>

</FrameLayout>

recyclerview 项目的 xml 布局:

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:layout_weight="3"
        android:orientation="vertical">

        <TextView
            android:id="@+id/groupName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Group Name"
            />

        <TextView
            android:id="@+id/groupDate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Group Date"
            />

        <TextView
            android:id="@+id/groupLocation"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Group Location"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/className"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="right"
            android:text="Class Name"
            />
    </LinearLayout>

</LinearLayout>
</FrameLayout>

【问题讨论】:

  • 请根据文件标记您的代码,以便于比较。我主要对包含您的片段的布局感兴趣。
  • 很抱歉,我不知道他们的问题在于那里,而是我如何制作片段本身。

标签: android android-fragments android-recyclerview recyclerview-layout


【解决方案1】:

您可以使用这些行将 recyclerview 滚动到:

list.add(0, group);
adapter.notifyItemInserted(0);
recyclerview.scrollToPosition(0);

【讨论】:

    【解决方案2】:

    感谢所有回复的人,但问题出在我正在编译的 RecyclerView 版本上。

    之前我正在编译这个

    compile 'com.android.support:recyclerview-v7:22.0.0'
    

    但我把它改成了这个,它工作了

    compile 'com.android.support:recyclerview-v7:22.2.0'
    

    感谢@roi divon 的回答:CoordinatorLayout with RecyclerView & CollapsingToolbarLayout

    【讨论】:

    • 也许您应该正确标记它,因为当我查看您的答案时,两边都一样
    【解决方案3】:

    也许将这些行添加到 recyclerView 就可以了:

    android:scrollbarAlwaysDrawVerticalTrack="true"
    android:scrollbars="vertical"
    

    这是我正在工作的 recyclerView:

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

    【讨论】:

    • @Mehul Goel 我的 recyclerView 处于相对布局中。你会试试吗?
    • @MehulGoel 做点别的。先设置adapter再设置recyclerview的layoutManager。看看是否有效
    • @MehulGoel 这是为了确保,但如果您使用的是 android studio 并且您更改了视图文件,那么在编译之前执行一个清理项目。有时在调试项目时视图不会改变...
    【解决方案4】:

    我不确定,但我认为问题可能出在 Framelayout 您可以在 recyclerview 项目的 xml 布局中尝试使用 Linearlayout 而不是 Framelayout

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-07
    • 2021-08-24
    • 2021-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多