2016-08-06 41 views
1

这是愚蠢的问题,但我无法找到我做错了什么。意外的空间进入recyclerview项目

我正在使用FirebaseRecyclerAdapter填充recyclerView,但有意想不到的空间正在与项目。 enter image description here

片段类:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_tags, container, false); 

    mProgressBar = (ProgressBar)view.findViewById(R.id.progressBar); 
    mTagsRecyclerView = (RecyclerView) view.findViewById(R.id.tagsRecyclerView); 
    mLinearLayoutManager = new LinearLayoutManager(getContext()); 

    mFirebaseDatabaseReference = FirebaseDatabase.getInstance().getReference(); 
    mFirebaseAdapter = new FirebaseRecyclerAdapter<Tags, TagsViewHolder>(
      Tags.class, 
      R.layout.item_message, 
      TagsViewHolder.class, 
      mFirebaseDatabaseReference.child("tags")) { 

     @Override 
     protected void populateViewHolder(TagsViewHolder viewHolder, Tags tags, int position) { 
      mProgressBar.setVisibility(ProgressBar.INVISIBLE); 
      viewHolder.tagTextView.setText(tags.getTagname()); 
      viewHolder.usrTextView.setText(tags.getUserid()); 
     } 
    }; 


    mFirebaseAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() { 
     @Override 
     public void onItemRangeInserted(int positionStart, int itemCount) { 
      super.onItemRangeInserted(positionStart, itemCount); 
     } 
    }); 

    mTagsRecyclerView.setLayoutManager(mLinearLayoutManager); 
    mTagsRecyclerView.setAdapter(mFirebaseAdapter); 

    mTagsRecyclerView.addOnItemTouchListener(new RecyclerViewItemClickListener(getContext(), 
      new RecyclerViewItemClickListener.OnItemClickListener() { 
       @Override public void onItemClick(View v, int position) { 
        //TODO: 
        // do something on item click 
        Log.d("Item clicked at",Integer.toString(position)); 
       } 
      })); 
    return view; 
} 

片段布局:

<FrameLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.helpplusapp.amit.helpplus.TagsFragment"> 

<android.support.v7.widget.RecyclerView 
    android:id="@+id/tagsRecyclerView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    /> 
<ProgressBar 
    style="?android:attr/progressBarStyleSmall" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/progressBar" 
    android:layout_gravity="center" 
    /> 

列表项的布局:

<?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="match_parent" 
    android:divider="@drawable/view_separator" 
    android:paddingTop="@dimen/margin_default" 
    android:paddingBottom="@dimen/margin_default" 
    android:showDividers="end"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:id="@+id/tagTextView" 
      /> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:id="@+id/usrTextView" 
      /> 

</LinearLayout> 

请建议。

回答

6

在listitemlayout XML文件进行线性布局的高度来包装内容

+0

是啊说得对了..谢谢了很多@Moulesh –

1

在列表项的布局改变线性布局机器人:身高=“WRAP_CONTENT”

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="wrap_content" 
android:layout_height="match_parent" 
android:divider="@drawable/view_separator" 
android:paddingTop="@dimen/margin_default" 
android:paddingBottom="@dimen/margin_default" 
android:showDividers="end"> 


    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:id="@+id/tagTextView" 
     /> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:id="@+id/usrTextView" 
     /> 
</LinearLayout> 
+0

难过...谢谢 –

1

随着推出了支持库23.2谷歌改变回收站视图:

LayoutManager API:自动测量!这允许RecyclerView根据其内容的大小自行决定其大小。这意味着 以前不可用的方案,例如使用WRAP_CONTENT为 尺寸的RecyclerView,现在是可能的。您会发现布局管理器中的所有内置 现在都支持自动测量。由于此更改, 请确保仔细检查您的项目视图的布局参数: 现在将会完全遵守之前忽略的布局参数(例如滚动方向为 的MATCH_PARENT)。

Source

您需要更改项目的高度为wrap_content代替match_parent。

<?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="wrap_content" 
    android:divider="@drawable/view_separator" 
    android:paddingTop="@dimen/margin_default" 
    android:paddingBottom="@dimen/margin_default" 
    android:showDividers="end"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:id="@+id/tagTextView" 
      /> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:id="@+id/usrTextView" 
      /> 

</LinearLayout>