2016-06-20 48 views
1

Goodd day.I具有简单的回收视图和最简单的虚拟数据用于测试目的,因此我有一个奇怪的问题,谷歌没有找到任何解决方案,甚至是一个问题在第一次发布视图是好的,但只要我开始scrool,儿童项目是远离彼此,因为没有人可以形象...真的非常非常远。但问题是,实际的子项目布局参数是正确的,唯一的问题是,我不知道为什么RecyclerView决定让每个项目堆彼此远离。请你给我一个帮助吗?发布我的RecyclerView的完整代码。RecyclerView滚动问题(儿童项目彼此远离)

为recyclerView

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

我猜你不需要持有人,因为没有视图启动它。 RecyclerView适配器的单个子项目视图。

<?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工作室(内content_main layout默认templae)。所以plaese的默认NAVIGATION DRAWER ACTIVITY里面,你可以给我很重要暗示这个问题?

+0

最新版本的RecyclerView发生了变化,请尝试将子视图高度设置为wrap_parent在顶层。该更改导致视图match_parent,其中旧版本强制wrap_content。 – wblaschko

回答

3

您使用

android:layout_width="match_parent" 
android:layout_height="match_parent" 
你的孩子项目的意见

。截至Support Library 23.2

RecyclerView小工具为创建列表和网格以及支持动画提供了一个先进而灵活的基础。此版本为LayoutManager API带来了令人兴奋的新功能:自动测量!这允许RecyclerView根据其内容的大小自行调整大小。这意味着以前不可用的场景,例如对RecyclerView的维度使用WRAP_CONTENT,现在是可能的。您会发现所有内置的LayoutManagers现在都支持自动测量。

由于此更改,请务必仔细检查项目视图的布局参数:现在将完全遵守先前忽略的布局参数(例如滚动方向上的MATCH_PARENT)。

更改layout_heightwrap_content如果你只是想在需要你的项目是一样大。这意味着它们将和屏幕一样大。

+0

完美!!!!我只是玩弄,想把自己的答案作为解决方案,因为我发现回收站视图不像listView或任何其他视图,所以ROOT布局必须具有wrap_content高度!完美!非常感谢你解决! –