Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何绘制更改后的RecyclerView元素的右侧?_Android_Android Recyclerview_Android Viewholder - Fatal编程技术网

Android 如何绘制更改后的RecyclerView元素的右侧?

Android 如何绘制更改后的RecyclerView元素的右侧?,android,android-recyclerview,android-viewholder,Android,Android Recyclerview,Android Viewholder,有片段。它是否RecyclerView附在下面。底线是,根据设计,第一个元素应该比其他元素占用更多的空间。当呈现以下内容时,适配器的ViewHolder中的His I增加: public class myAdapter extends RecyclerView.Adapter ... ... @Override public void onBindViewHolder(final ViewHolder holder, final int position) { W

片段
。它是否
RecyclerView
附在下面。底线是,根据设计,第一个元素应该比其他元素占用更多的空间。当呈现以下内容时,适配器的
ViewHolder
中的His I增加:

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;
        }
...
}
如何使元素沿上边缘和下边缘对齐

另外,同样的问题也出现在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>

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


你试过使用重力吗?例如:

  • a) 为RecyclerView项目布局定义android:gravity=“center\u vertical”

  • b) 为mRoundLayout定义android:layout\u gravity=“center\u vertical”

  • c) 定义android:layout\u centerInParent或 android:layout\u centerVertical=“true”

关于重力问题:

您必须将android:layout\u gravity=“center\u vertical”或
android:layout\u gravity=“center”
设置为项目的根元素(
LinearLayout

此外:

我建议您使用
viewType
并为位置0和位置>0创建不同的布局,而不是手动更改布局参数。这样,您可以确保只有位置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());
}

希望这对你有帮助

我更新我的问题。但是方法getAdapterPosition()的thx!是的,我使用重力,但我找不到合适的选项。我更改了if(holder.getAdapterPosition==0)的条件,然后…,但仍然绘制了一个额外的大图circle@abbath0767尝试在circle视图父对象上调用invalidate(),甚至在之后在整个RecyclervIew上调用invalidate()adding@abbath0767还将recyclerview.hasFixedSize设置为false。问:你们在第一个位置总是有“大圆圈”吗?在第一个位置——总是