【问题标题】:Unexpected space is coming in recyclerview itemsrecyclerview 项目中出现了意外的空间
【发布时间】:2016-08-06 17:44:51
【问题描述】:

这是一个愚蠢的问题,但我找不到我做错了什么。

我正在使用 FirebaseRecyclerAdapter 填充 recyclerView,但项目中出现了意外空间。

片段类:

    @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>

请提出建议。

【问题讨论】:

    标签: android firebase firebase-realtime-database firebaseui


    【解决方案1】:

    在listitemlayout xml文件中设置线性布局的高度以包裹内容

    【讨论】:

      【解决方案2】:

      在列表项布局中更改线性布局 android:height="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>
      

      【讨论】:

        【解决方案3】:

        随着支持库 23.2,google 引入了对 recyclerview 的更改:

        LayoutManager API:自动测量!这允许 RecyclerView 大小本身基于其内容的大小。这意味着 以前不可用的方案,例如将 WRAP_CONTENT 用于 RecyclerView 的维度,现在是可能的。你会发现所有构建 在 LayoutManagers 现在支持自动测量。由于这种变化, 确保仔细检查项目视图的布局参数: 以前忽略的布局参数(例如 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>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-06-28
          • 1970-01-01
          • 2020-12-17
          • 1970-01-01
          • 1970-01-01
          • 2022-01-10
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多