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

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

问题描述

我想跳过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

我们有2个函数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天全站免登陆