-1

I have a problem with my LayoutInflater. First of all here is my item.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:background="@drawable/button" >
 
</RelativeLayout>

(I deleted all unneccessary stuff for this question)

When I set my LayoutInflater in my RecyclerViewAdapter like this:

View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, null);

Everything works fine and it looks like I want it to be: This is how I want it to look

In many documentations and articles about LayouInflater they recommend using the LayoutInflater like this:

View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);

But when I do that it ignores the attributes from the item.xml file : enter image description here

There are a lot of articles out there who explain that the first method (the one who works for me) should be avoided. Here is one of those articles: https://web.archive.org/web/20190528213406/https://possiblemobile.com/2013/05/layout-inflation-as-intended/

Here is my full RecyclerView.Adapter class:

public class AdapterTest extends RecyclerView.Adapter<AdapterTest.TestViewHolder> {


    @Override
    public TestViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, null);
        return new TestViewHolder(v);
    }

    @Override
    public void onBindViewHolder(final TestViewHolder holder, int position) {


    }

    @Override
    public int getItemCount() {
        return 10;
    }

    public class TestViewHolder extends RecyclerView.ViewHolder {
 

        public TestViewHolder(View itemView) {
            super(itemView); 
        }
    }


}

How can I use the recommended way but still have the results I want?

HavanaSun
  • 446
  • 3
  • 12
  • 39
  • Is it just me or these pictures are the same? – SlothCoding Feb 01 '21 at 10:43
  • @SlothCoding My bad, updated the second picture now – HavanaSun Feb 01 '21 at 11:10
  • Try to use margins on your item.xml on root view, in your case it's RelativeLayout. This way each item will have space in-between. Then just go with the second option. – SlothCoding Feb 01 '21 at 11:47
  • @SlothCoding It now fixed the overlapping but still has a weird behavior, it has 0 margin on the right but a lot on the left. – HavanaSun Feb 01 '21 at 11:57
  • Can you add your full XML and new picture? – SlothCoding Feb 01 '21 at 12:04
  • The XML is basically the same just with a 20dp margin left and right in the root View. Here is the picutre: https://imgur.com/a/4WeenFH – HavanaSun Feb 01 '21 at 12:25
  • I have almost the same adapter, using the inflater the same way, and have only a 5dp margin on the root layout on the item. It works fine without any problems. If you are using GridLayoutManager, research on how to create spacing with it, maybe that's the issue. – SlothCoding Feb 01 '21 at 12:30
  • 1
    @SlothCoding Thanks for trying to help me, I found a solution and postet the answer. – HavanaSun Feb 01 '21 at 12:46

1 Answers1

0

I fixed it by adding these two lines inside the onCreateViewHolder:

RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
v.setLayoutParams(lp);

My onCreateViewHolder now looks like this:

@NonNull
@Override
public TestViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View v = LayoutInflater.from(mContext).inflate(R.layout.item, parent, false);
    RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    v.setLayoutParams(lp);
    return new TestViewHolder(v);
}

Here is the answer where I got that from: https://stackoverflow.com/a/30692398/7735807

I can't really explain why it works now because my RecyclerView was set on MatchParent width and hight from the beginning on. I'm just glad it works now

HavanaSun
  • 446
  • 3
  • 12
  • 39