Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
962 views
in Technique[技术] by (71.8m points)

android - Skip items in recycler view

Hi I want to skip some items from recyclerview.

here is the screenshot

Here is the bit of code

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" />

And recycler view is

   @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());
    }

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There's a very simple fix for this:

If you perform view.setVisibility(View.GONE); on the view while binding it to the ViewHolder, the view would be hidden but there would still be a space right where the view was supposed to be; therefore, this approach isn't efficient.

How then do we solve this problem?

All you need to do is to set the height and/or width of the view you're trying to hide to zero. Here's a simple way to achieve this:

View Holder:

    public class MyViewHolder extends RecyclerView.ViewHolder{

        public LinearLayout.LayoutParams params;
        public LinearLayout rootView //the outermost view from your layout. Note that it doesn't necessarily have to be a LinearLayout.

        //todo: Don't forget to add your other views

        public MyViewHolder(View itemView){
            super(itemView);

            params = new LinearLayout.LayoutParams(0, 0);
            rootView = itemView.findViewById(R.id.rootView);

            //todo: Don't forget to initialize your views


        }

    }

onBindViewHolder:

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position){

    if(your_condition){
        holder.rootView.setLayoutParams(holder.params);  
        //this line hides the view completely
    }
    else{
        //todo: Do your regular stuff
    }

}

I hope this helps. Merry coding!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...