2

How to change the margin (bottom with 20 dp) of first complete visible item on horizontal recycleview with cards? I can get only the index of first complete visible item but not any reference to the view.

 mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
       @Override
       public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
           super.onScrolled(recyclerView, dx, dy);

           firstVisibleItem = lm.findFirstCompletelyVisibleItemPosition();
        View view=  mRecyclerView.getChildAt(firstVisibleItem);

        RelativeLayout.LayoutParams lp= new RelativeLayout.LayoutParams(300, 150);
        lp.setMargins(0, 0, 0, 20);
        view.setLayoutParams(lp);

       }
    });
dorianmusaj
  • 56
  • 1
  • 7
  • i tried to get the view with View view= mRecyclerView.getChildAt(firstVisibleItem); and set layout params with margin lp.setMargins(0, 0, 0, 20); but didnt work – dorianmusaj May 06 '16 at 21:08

2 Answers2

3

Assuming that view contains a reference to the view for which you want to change the margins, you can set the bottom margin to 20 pixels with the following:

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
    LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(0, 0, 0, 20);
view.setLayoutParams(lp);

Note that this will change the bottom margin to 20 pixels and not 20dp. You will need to convert your 20dp value to pixels. Converting pixels to dp would be a good place to take a look at how that conversion can be done.

You may also need to change the height and width parameters on the constructor for LinearLayout.LayoutParams to fit you needs.

If the issue is getting a reference to the view, posting some code would be helpful.

Edit/Update The code helped. I don't think that you are setting up access to the view you want. Here is another approach:

 mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
   @Override
   public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
       super.onScrolled(recyclerView, dx, dy);

       // Find the adapter position of the first fully visible item
       // May return RecyclerView.NO_POSITION that is not handled here.
       firstVisibleItem = lm.findFirstCompletelyVisibleItemPosition();

       // Find the corresponding view holder for this position.
       // MyViewHolder should already be defined (just don't know the name).
       MyViewHolder vh = (MyViewHolder) mRecyclerView
           .findViewHolderForAdapterPosition(firstVisibleItem);

       // Get the layout parameters from the top-level item of this view.
       // This could also be another view captured within the view holder.
       RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) vh.itemView.getLayoutParams();

       // Make our changes and set them.
       lp.setMargins(0, 0, 0, 50);
       vh.itemView.setLayoutParams(lp);
   }
});
Community
  • 1
  • 1
Cheticamp
  • 61,413
  • 10
  • 78
  • 131
  • there is my code, i edited it..its the same as your suggestion in onscroll methos of recycleview, lm is the layoutmanager for the recycleview but nothing happens, please help..i need an interface pretty much like field trip app – dorianmusaj May 07 '16 at 09:43
0

First of all, if you are changing the layout or a very specific item in a RecyclerView, you might not be designing your layout properly. You might better want to do something like:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingBottom="20dp"/>

    <android.support.v7.widget.RecyclerView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

If you still want to use just a RecyclerView then you should override onBindViewHolder. In this method you can discriminate based on the position of that ViewHolder and set different layouts for your View.

@Override
    public void onBindViewHolder(ViewHolder holder,
                                 @SuppressLint("RecyclerView") final int position) {
}
Jorge
  • 1,353
  • 10
  • 25