1

There Fragment. Is it RecyclerView attached below. The bottom line is that by design is the first element should take a little more space than the others. His I increase in ViewHolder of the adapter when rendering as follows:

public class myAdapter extends RecyclerView.Adapter ...
...
    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        WeatherForDay weather = mWeathersList.get(position);

        holder.mDayTextView.setText(weather.getDay());
        //todo update iconManager
        //need icon manager, with input -> String, output ->(R.drawable.icon) int
        holder.mIconImageView.setImageResource(R.drawable.testicon1);
        holder.mTempTextView.setText(weather.getTmp());

        if (position == 0) {
            if (isFirstBind) {
                Log.d(DEBUG_TAG, "is first bind and first position");
                holder.setBig();
                isFirstBind = false;
            }
        }
    }
...
        public void setBig() {
            LinearLayout.LayoutParams param = (LinearLayout.LayoutParams) mRoundLayout.getLayoutParams();
            int newHeight = (int) (param.height * 1.2f);
            int newWidth = (int) (param.height * 1.2f);
            param.height = newHeight;
            param.width = newWidth;
            mRoundLayout.setLayoutParams(param);
            mRoundLayout.setBackgroundDrawable(createBigShape(newHeight));
        }

        private Drawable createBigShape(int newHW) {
            GradientDrawable shape = new GradientDrawable();
            shape.setShape(GradientDrawable.RECTANGLE);
            shape.setCornerRadii(new float[]{newHW, newHW, newHW, newHW, newHW, newHW, newHW, newHW});
            shape.setColor(mContext.getResources().getColor(R.color.weryDark));
            shape.setStroke(1, mContext.getResources().getColor(R.color.weryDark));
            return shape;
        }
...
}

How to make elements are aligned along the upper edge and the lower?

enter image description here

P.S. The same problem arises in that the N-element also somehow rendered "large" (on the same device with a large screen N = 13, on the small screen N = 8)

P.S.2 Can eat what is more optimal way to change any element RecyclerView?

item_recycler.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginLeft="4dp"
              android:layout_marginRight="4dp"
              android:gravity="center_horizontal"
              android:orientation="vertical">

    <TextView
        android:id="@+id/day_text_view_list_item_bottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/colorWhite"
        android:background="@null"/>

    <LinearLayout
        android:id="@+id/rounded_linear_layout"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@drawable/round_shape"
        android:gravity="center_horizontal"
        android:orientation="vertical">

        <android.support.v7.widget.AppCompatImageView
            android:id="@+id/icon_image_view_list_item_bottom"
            android:layout_width="42dp"
            android:layout_height="42dp"
            android:background="@null"
            app:srcCompat="@drawable/testicon1"
            tools:ignore="MissingPrefix"/>

        <TextView
            android:id="@+id/temperature_text_view_list_item_bottom"
            android:textColor="@color/colorWhite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:textStyle="bold"/>

    </LinearLayout>

</LinearLayout>
abbath0767
  • 946
  • 2
  • 11
  • 31

2 Answers2

1

Don't use position that provided from onBindViewHolder(final ViewHolder holder, final int position), it's not permanent (that's why your N-element also "large"). Use holder.getAdapterPosition() instead


Did you try to use some Gravity? For example:

  • a) Define android:gravity="center_vertical" for RecyclerView item layout

  • b) Define android:layout_gravity="center_vertical" for mRoundLayout

  • c) Define android:layout_centerInParent or android:layout_centerVertical ="true"

Ekalips
  • 1,473
  • 11
  • 20
  • i update my question. But thx for method getAdapterPosition() ! And yes, i use Gravity, but I could not find a suitable option. – abbath0767 Nov 17 '16 at 09:23
  • I changed the condition on if (holder.getAdapterPosition == 0) then....but still draws an additional large circle – abbath0767 Nov 17 '16 at 09:29
  • @abbath0767 try to call invalidate() on your circle view parent, or even on whole RecyclervIew after adding –  Ekalips Nov 17 '16 at 12:20
  • @abbath0767 also set recyclerview.hasFixedSize to false. And question: do you always have "big circle" at first position? –  Ekalips Nov 17 '16 at 12:22
  • in first position - always – abbath0767 Nov 17 '16 at 12:35
  • @abbath0767 so I recommend you to use another implementation. In fact this is "header" and you just need to define header of your RecyclerView. Unfortinently you can't do it as easely as in ListView, but you still can. –  Ekalips Nov 17 '16 at 12:51
  • @abbath0767 please, see this for example http://stackoverflow.com/a/26573338/4850642 –  Ekalips Nov 17 '16 at 12:51
1

For the gravity issue:

You have to set android:layout_gravity="center_vertical" or android:layout_gravity="center" to your item's root element (LinearLayout).

In addition:

Instead of changing LayoutParams manually, I suggest you to use viewType and create different layouts for position 0 and position > 0. This way, you ensure only position 0 item is bigger and you don't modify it's natural functioning.

You would add this method:

private static final int TYPE_BIG = 0;
private static final int TYPE_STANDARD = 1;

@Override
public int getItemViewType(int position) {
    return position == 0 ? TYPE_BIG : TYPE_STANDARD;
}

And modify your onCreateViewHolder:

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
     int layout = viewType == TYPE_BIG
             ? R.layout.item_recycler_big
             : R.layout.item_recycler;

     View v = View.inflate(parent.getContext(), layout, parent);

     return new ViewHolder(v);
}

And your onBindViewHolder would be as simple as:

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
    WeatherForDay weather = mWeathersList.get(position);

    holder.mDayTextView.setText(weather.getDay());
    holder.mIconImageView.setImageResource(R.drawable.testicon1);
    holder.mTempTextView.setText(weather.getTmp());
}

Hope this helps you!

josemigallas
  • 3,761
  • 1
  • 29
  • 68