我有一个自定义适配器,将包含 6 个项目的列表传递给该适配器,
但三星 galaxy s6 中只显示了 3 个项目,而 galaxy j7 手机上显示了所有项目!
适配器 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( 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>
卡片 View 布局:
<?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>
获得的 Toasts 输出:
toast 1:项目:6
toast 2:项目:0
toast 3:项目:1
toast 4:项目:2
最佳答案
你必须使用 onBindViewHolder 而不是使用 position
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 并为您的 nestedScrollView 使用 android:fillViewport="true"
。
关于Android 自定义适配器不显示所有项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55075028/