如何绘制改变后的RecyclerView元素的权利?

时间:2016-11-17 09:04:11

标签: android android-recyclerview android-viewholder

Fragment。是RecyclerView是否附在下面。底线是,设计是第一个元素应该占用比其他元素更多的空间。在渲染时,我的适配器的ViewHolder增加如下:

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;
        }
...
}

如何使元素沿上边缘和下边缘对齐?

enter image description here

P.S。同样的问题出现了,N元素也以某种方式呈现“大”(在大屏幕N = 13的同一设备上,在小屏幕上N = 8)

P.S.2可以吃什么是更改任何元素的最佳方式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>

2 个答案:

答案 0 :(得分:1)

不要使用onBindViewHolder(final ViewHolder holder, final int position)提供的位置,它不是永久性的(这就是为什么你的N元素也“大”)。请改用holder.getAdapterPosition()

你尝试使用一些重力吗?例如:

  • a)为RecyclerView项目布局定义android:gravity =“center_vertical”

  • b)为mRoundLayout定义android:layout_gravity =“center_vertical”

  • c)定义android:layout_centerInParent或 android:layout_centerVertical =“true”

答案 1 :(得分:1)

对于重力问题:

您必须将 android:layout_gravity="center_vertical" android:layout_gravity="center" 设置为项目的根元素(LinearLayout)。

另外:

我建议您使用viewType并为位置0和位置&gt;创建不同的布局,而不是手动更改LayoutParams。这样,你可以确保只有位置0项更大,你不会修改它的自然功能。

您可以添加此方法:

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;
}

修改你的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);
}

你的onBindViewHolder就像:

一样简单
@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());
}

希望这能帮到你!