在回收者视图中跳过项目 [英] Skip items in recycler view

查看:14
本文介绍了在回收者视图中跳过项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 recyclerview 跳过一些项目.

Hi I want to skip some items from recyclerview.

代码如下

item_Data.xml

item_Data.xml

 <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:id="@+id/mainlayout"
android:layout_height="wrap_content">
<ImageView
    android:visibility="gone"
    android:id="@id/image"
    android:layout_gravity="center"
    android:layout_width="100dp"
    android:layout_height="100dp" />
<TextView
    android:visibility="gone"
    android:textStyle="bold"
    android:id="@id/title"
    android:layout_gravity="center"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:maxLength="15" />

回收者的观点是

   @Override
public void onBindViewHolder(final MovieViewHolder holder, final int position) {
    String download= news.get((position)).getDownloadLinkForApkNew();
    String desc_new = news.get(position).getApkJData();
    if (download.isEmpty()==false && desc_new.isEmpty()==false) {
        holder.movieTitle.setVisibility(View.VISIBLE);
        holder.imageView.setVisibility(View.VISIBLE);
        Picasso.with(context).load(news.get((position)).getBetterFeaturedImage().getSourceUrl()).into(holder.imageView);
        holder.movieTitle.setText(news.get(position).getTitle().getRendered());
    }

我不想要没有下载和 desc_new 的项目.我的逻辑工作项不可见,但它们在那里留下空间.如何删除项目之间的空格.

I don't want the items that doesn't have download and desc_new. My logic works items are not visible but they leave there space. how can I remove the spaces between the items.

推荐答案

让我们深入了解回收器视图的工作原理

Lets go in depth as of how recycler view works

我们有两个函数 onCreateView 和 onBindview.由于函数的名称是不言自明的,所以 onCreateView 创建视图,而 onBindView 获取创建的视图并将数据绑定到其中

we have 2 functions onCreateView and onBindview. As the names of functions are quite self explaining onCreateView creates the view and onBindView takes the created view and binds data into it

现在假设整个视图类型是相似的,并且您使用一组对象或游标来填充整个视图.

now lets assume that entire view type is similar and you use an array of objects or cursor to populate the entire view.

因此在 bindView 中为了获取数据,您必须使用

so in bindView in order to fetch data you must have used either

 cursor.moveToPosition(position)

 mList.get(position)

如您所见,绑定是根据我们从 onBindView 参数中获得的位置而发生的

as you can see that binding is happening based on the position that we get from onBindView arguments

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
     //mList.get(position) or cursor.moveToPosition
 }

所以你可以利用这些知识专门跳过视图的绑定

so you can use this knowledge to specifically skip binding of view

假设您有一个函数,它接受位置作为参数并返回实际位置作为结果

say you have a function which accepts postion as parameter and returns actual position as result

private int getActualPostion(int position){
     //your logic to skip the given postion 
     //say  if(position == 4){return postion+2}
}

所以你可以实现这样的东西

so you can implement something like this

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
       mList.get(getActualPosition(position));
 }

这将允许您跳过那些不显示的视图

this will allow you to skip those view which are not to be shown

最后在方法 getCount 中,回收者视图使用它来决定视图的数量

finally in method getCount which is used by recycler view to decide the number of views

 @Override
 public int getItemCount() {
     //foreach in array { if(already downloaded) i++} 
     // return array.size - i
 }

我希望这会有所帮助这也将为您提供更大的灵活性,您可以添加更多过滤器并使用相同的数据集......更轻松地跳过视图

I hope this helps this will also give your more flexibility in a way that u may add more filters and use same dataset ... skip views more easily

这篇关于在回收者视图中跳过项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆