Android滚动布局到底部,RecyclerView不会滚动到底部

博客讨论了在Android应用中遇到RecyclerView无法滚动到底部的常见问题,并提供了可能的解决方案,包括检查RecyclerView的依赖库版本,以及在XML布局中使用LinearLayout替代FrameLayout。作者还分享了适配器和Fragment的相关代码片段。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我遵循了recyclerview指南并为我正在制作的应用程序构建了一个,但由于某种原因它不会滚动到底部。 我将它与谷歌代码片段以及其他在线代码片段进行了比较,看不出差异。 我发布了一张图片和我正在使用的代码。 我正在使用制表符,因此recyclerview填充在片段中。

该应用程序的外观如下:

适配器类:

public class MyAdapter extends RecyclerView.Adapter { private List 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 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(); } }

Fragment类:

public class groupsFragment extends Fragment implements GroupLeaver, GroupRetriever { private RecyclerView rv; private List 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 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布局:

recyclerview项目的xml布局:

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

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

感谢所有回复的人,但事实certificate问题是我正在编译的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

也许将这些行添加到recyclerView将:

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

这是我的recyclerView工作:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值