Android自定义适配器未显示所有项目

时间:2019-03-09 07:25:40

标签: android android-recyclerview

我有一个自定义适配器,将包含6个项目的列表传递给该适配器,

但是三星Galaxy s6中仅显示3个项目,而在Galaxy J7手机上显示所有项目!

适配器@Override public void onBindViewHolder(@NonNull ViewHolder holder, final int position) { try { Toast.makeText( context, "items: " + items.size(), Toast.LENGTH_SHORT ).show(); News newsList = items.get( position ); Picasso.with( context ).load( newsList.getThumbnail() ).into( holder.image ); holder.txtNewsTitle.setText( newsList.getPublisherId() ); holder.txtNewsType.setText( newsList.getTypeCode() ); holder.txtTitle.setText( newsList.getTitle() ); holder.txtDate.setText( newsList.getCreatedAt() ); holder.txtLikeNumber.setText( newsList.getLikeCount() ); //holder.txtComment.setText(newsList.getTxtComment()); holder.txtSeen.setText( newsList.getViewCount() ); Toast.makeText( context, "item: " + position, Toast.LENGTH_SHORT ).show(); holder.cardViewNewsList.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { FragmentManager fm = ( (Activity) context ).getFragmentManager(); fm.beginTransaction().replace( R.id.fragmentContainer, new ShowNewsFragment() ).addToBackStack( null ).commit(); } } ); } catch (Exception e) { Log.i( "onBindViewHolder", "onBindViewHolder: " + e ); } } 代码:

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

                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/recyclerNewsList"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />

                </LinearLayout>

主要布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_marginTop="@dimen/xxsmall"
    android:background="@drawable/bg_linear_frag_search"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        android:id="@+id/cardViewNewsList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardElevation="@dimen/zero">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="center"
                android:orientation="horizontal"
                android:layout_marginTop="@dimen/xxxsmall">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginLeft="@dimen/xxsmall"
                    android:gravity="center">

                    <ImageView
                        android:id="@+id/imgShare"
                        android:layout_width="@dimen/ic_xsmall"
                        android:layout_height="@dimen/ic_xsmall"
                        app:srcCompat="@drawable/ic_comment_client"/>

                </LinearLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginLeft="@dimen/xlarg"
                    android:gravity="center">

                    <ImageView
                        android:id="@+id/imgAddBookmark"
                        android:layout_width="@dimen/ic_xsmall"
                        android:layout_height="@dimen/ic_xsmall"
                        app:srcCompat="@drawable/ic_favorite_client"/>

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/medium"
                    android:layout_gravity="right"
                    android:gravity="right">

                    <TextView
                        android:id="@+id/txtNewsTitle"
                        android:layout_width="@dimen/xxxxlarg"
                        android:layout_height="@dimen/medium"
                        android:layout_gravity="right"
                        android:layout_marginTop="@dimen/xxxsmall"
                        android:layout_marginBottom="@dimen/xxxsmall"
                        android:layout_marginRight="@dimen/small"
                        android:background="@drawable/frag_showlist_bg_btn_fars"
                        android:gravity="center"
                        android:textColor="@color/colorWhite"
                        android:textSize="@dimen/txt_xsmall" />

                </LinearLayout>

            </LinearLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="@dimen/xxxxsmall"
                android:layout_marginTop="@dimen/xxsmall"
                android:layout_marginLeft="@dimen/xxsmall"
                android:layout_marginRight="@dimen/xxsmall"
                android:background="@color/colorGray" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:gravity="right"
                    android:orientation="horizontal">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical">

                        <TextView
                            android:id="@+id/txtNewsType"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:paddingRight="@dimen/xxsmall"
                            android:paddingLeft="@dimen/xxsmall"
                            android:textColor="@color/colorDarkGray"
                            android:lines="2"
                            android:textSize="@dimen/txt_xsmall" />

                        <TextView
                            android:id="@+id/txtTitle"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:paddingRight="@dimen/xxsmall"
                            android:paddingLeft="@dimen/xxsmall"
                            android:lines="2"
                            android:textSize="@dimen/txt_xsmall"
                            android:textColor="@color/colorBlack"/>

                    </LinearLayout>

                    <android.support.v7.widget.CardView
                        android:layout_width="@dimen/ic_xbig"
                        android:layout_height="@dimen/ic_big"
                        android:layout_marginTop="@dimen/xxsmall"
                        android:layout_marginBottom="@dimen/xxsmall"
                        android:layout_marginRight="@dimen/xxsmall"
                        app:cardElevation="0dp"
                        app:cardCornerRadius="0dp">

                        <ImageView
                            android:id="@+id/image"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:scaleType="fitCenter"/>

                    </android.support.v7.widget.CardView>

                </LinearLayout>

                <View
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/xxxxsmall"
                    android:layout_marginLeft="@dimen/xxsmall"
                    android:layout_marginRight="@dimen/xxsmall"
                    android:background="@color/colorGray" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:orientation="horizontal"
                    android:paddingTop="@dimen/xxxsmall"
                    android:paddingBottom="@dimen/xxxsmall">

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_marginLeft="@dimen/xxsmall"
                        android:gravity="center">

                        <TextView
                            android:id="@+id/txtDate"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textColor="@color/colorGrayText"
                            android:gravity="center"
                            android:layout_gravity="center"
                            android:textSize="@dimen/txt_xsmall" />

                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="@dimen/xsmall"
                        android:gravity="center"
                        android:orientation="horizontal">

                        <ImageView
                            android:id="@+id/imgLike"
                            android:layout_width="@dimen/ic_xsmall"
                            android:layout_height="@dimen/ic_xsmall"
                            app:srcCompat="@drawable/ic_like_client"/>

                        <TextView
                            android:id="@+id/txtLikeNumber"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="@dimen/xxxsmall"
                            android:textColor="@color/colorGrayText"
                            android:textSize="@dimen/txt_xsmall"/>

                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="@dimen/xxsmall"
                        android:gravity="center">

                        <ImageView
                            android:id="@+id/imgComment"
                            android:layout_width="@dimen/ic_xsmall"
                            android:layout_height="@dimen/ic_xsmall"
                            app:srcCompat="@drawable/ic_comment_client"/>

                        <TextView
                            android:id="@+id/commentNumber"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="@dimen/xxxsmall"
                            android:textColor="@color/colorGrayText"
                            android:textSize="@dimen/txt_xsmall" />

                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="@dimen/xxsmall"
                        android:gravity="center">

                        <ImageView
                            android:id="@+id/imgSeen"
                            android:layout_width="@dimen/ic_xsmall"
                            android:layout_height="@dimen/ic_xsmall"
                            app:srcCompat="@drawable/ic_view_client" />

                        <TextView
                            android:id="@+id/txtSeen"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="@dimen/xxxsmall"
                            android:textColor="@color/colorGrayText"
                            android:textSize="@dimen/txt_xsmall" />

                    </LinearLayout>

                </LinearLayout>

            </LinearLayout>

        </LinearLayout>

    </android.support.v7.widget.CardView>

</LinearLayout>

卡片视图布局:

db.collection.aggregate([
  { "$group": {
    "_id": null,
    "count": { "$sum": 1 },
    "valid": {
      "$sum": {
        "$size": {
          "$filter": {
            "input": "$items",
            "cond": "$$this.valid"
          }
        }
      },
    "nonValid": {
      "$sum": {
        "$size": {
          "$filter": {
            "input": "$items",
            "cond": { "$not": "$$this.valid" }
          }
        }
      }
    }
  }}
])

获得的吐司输出:

吐司1:项目:6

吐司2:项目:0

吐司3:项目:1

吐司4:项目:2

2 个答案:

答案 0 :(得分:1)

您不必使用onBindViewHolder中的位置,而必须使用

holder.getAdapterPosition()

从列表中获取数据时。在onBindViewHolder中使用的位置是该项目在屏幕上的位置,该位置将限于当前屏幕上的最大项目数。

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
    try {
        Toast.makeText( context, "items: " + items.size(), Toast.LENGTH_SHORT ).show();

        News newsList = items.get( holder.getAdapterPosition() );
        Picasso.with( context ).load( newsList.getThumbnail() ).into( holder.image );

        holder.txtNewsTitle.setText( newsList.getPublisherId() );
        holder.txtNewsType.setText( newsList.getTypeCode() );
        holder.txtTitle.setText( newsList.getTitle() );
        holder.txtDate.setText( newsList.getCreatedAt() );
        holder.txtLikeNumber.setText( newsList.getLikeCount() );
        //holder.txtComment.setText(newsList.getTxtComment());
        holder.txtSeen.setText( newsList.getViewCount() );

        Toast.makeText( context, "item: " + holder.getAdapterPosition(), Toast.LENGTH_SHORT ).show();

        holder.cardViewNewsList.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager fm = ( (Activity) context ).getFragmentManager();
                fm.beginTransaction().replace( R.id.fragmentContainer, new ShowNewsFragment() ).addToBackStack( null ).commit();
            }
        } );
    } catch (Exception e) {
        Log.i( "onBindViewHolder", "onBindViewHolder: " + e );
    }
}

更新

使用 NestedScrollView 代替 ScrollView 并添加recyclerView.setNestedScrollingEnabled(false);

返回到RecyclerView,并将android:fillViewport="true"用于nestedScrollView。

答案 1 :(得分:0)

sethasstableids

该图显示了recyclerviewadapter sethasstableids的属性。 使其真实。