gpt4 book ai didi

android - 如何在右边绘制改变后的 RecyclerView 元素?

转载 作者:行者123 更新时间:2023-11-29 19:28:50 25 4
gpt4 key购买 nike

fragment 。是不是下面附上的RecyclerView。最重要的是,根据设计,第一个元素应该比其他元素占用更多的空间。他我在渲染时在adapter的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

附言同样的问题出现在 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>

最佳答案

对于引力问题:

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

此外:

我建议您使用 viewType 并为位置 0 和位置 > 0 创建不同的布局,而不是手动更改 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());
}

希望对你有帮助!

关于android - 如何在右边绘制改变后的 RecyclerView 元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40650587/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com