android - RecyclerView 滚动问题(子项目彼此远离)

标签 android view scroll

美好的一天。我有一个简单的回收器 View 和最简单的虚拟数据用于测试目的,因此我有一个奇怪的问题,谷歌没有找到任何解决方案,甚至根本没有找到任何问题。在首次启动时, View 一切正常,但我一开始滚动,子项目彼此之间的距离就没有人想象的那么远......真的非常非常远。但问题是实际的子项目布局参数是正确的,唯一的问题是我不知道为什么 RecyclerView 决定让每个项目堆得离彼此很远。你能帮帮我吗?发布我的 RecyclerView 的完整代码。

recyclerView 的 View

   <?xml version="1.0" encoding="utf-8"?>
<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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.ink.activities.HomeActivity"
    tools:showIn="@layout/app_bar_home">

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

适配器。

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

    private List<FeedModel> feedList;
    private Context mContext;

    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView title, content;

        public ViewHolder(View view) {
            super(view);
            title = (TextView) view.findViewById(R.id.feedTitle);
            content = (TextView) view.findViewById(R.id.feedContent);
        }
    }


    public FeedAdapter(List<FeedModel> feedList, Context context) {
        mContext = context;
        this.feedList = feedList;
    }

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

        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        FeedModel feedModel = feedList.get(position);
        holder.title.setText(feedModel.getTitle());
        holder.content.setText(feedModel.getContent());

//        animate(holder);
    }


    public void animate(RecyclerView.ViewHolder viewHolder) {
        final Animation animAnticipateOvershoot = AnimationUtils.loadAnimation(mContext, R.anim.bounce_interpolator);
        viewHolder.itemView.setAnimation(animAnticipateOvershoot);
    }

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

我猜你不需要 holder,因为它不会启动 View 。 RecyclerView 适配器的单个子项 View 。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:clickable="true"
        android:foreground="?android:attr/selectableItemBackground"
        app:cardCornerRadius="5dp"
        app:cardElevation="10dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/feedTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="10dp"
                android:fontFamily="@string/appFont"
                android:text="loading...."
                android:textColor="#000000"
                android:textSize="20sp" />

            <TextView
                android:id="@+id/feedContent"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/feedTitle"
                android:layout_marginLeft="2dp"
                android:layout_marginRight="2dp"
                android:layout_marginTop="10dp" />
        </RelativeLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>

实际参数的初始化。

 mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);

        mAdapter = new FeedAdapter(mFeedModelArrayList, this);
        RecyclerView.ItemAnimator itemAnimator = new DefaultItemAnimator();
        itemAnimator.setAddDuration(500);
        itemAnimator.setRemoveDuration(500);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setItemAnimator(itemAnimator);

这段代码尽可能简单,重要的是要提到我在 android studio 的默认 NAVIGATION DRAWER ACTIVITY 中启动所有这些(content_main 布局中的默认模板).那么请你给我任何关于这个问题的提示吗?

最佳答案

你正在使用

android:layout_width="match_parent"
android:layout_height="match_parent"

在您的子项 View 上。截至Support Library 23.2 :

The RecyclerView widget provides an advanced and flexible base for creating lists and grids as well as supporting animations. This release brings an exciting new feature to the LayoutManager API: auto-measurement! This allows a RecyclerView to size itself based on the size of its contents. This means that previously unavailable scenarios, such as using WRAP_CONTENT for a dimension of the RecyclerView, are now possible. You’ll find all built in LayoutManagers now support auto-measurement.

Due to this change, make sure to double check the layout parameters of your item views: previously ignored layout parameters (such as MATCH_PARENT in the scroll direction) will now be fully respected.

如果您只想让您的项目尽可能大,请将您的 layout_height 更改为 wrap_contentmatch_parent 表示它们将与屏幕一样大。

关于android - RecyclerView 滚动问题(子项目彼此远离),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37931229/

相关文章:

javascript - 锁定主体滚动,阻止目标元素滚动

java - (Android)应用程序是否共享内存页面?

android - Android 中 UDP 多播中的丢包

java - 使用空检查、类型转换值的更好方法?

c++ - 如何通过单击 MainFrame 中的按钮来更改 MFC View

android - 底部导航 View 显示不正确

javascript - document.ontouchmove 和 iOS 5 上的滚动

android - 导航后,iframe中的YouTube视频继续播放-Android和Phonegap

android - 如何删除 android SearchView 组件中的底线?

javascript - 使用 Jquery 删除滚动绑定(bind)而不删除其他绑定(bind)