当我在 android 中滚动 Recyclerview 时,项目视图的高度会降低
Height of the item view gets reduced when i scroll on Recyclerview in android
我想显示名称 list.I 我正在使用 recylerview 来显示 that.For 我已经使用 Vertical TextView 作为项目 view.it 有效 properly.But 问题是什么时候我滚动 recyclerview,项目高度降低
垂直文本视图 class
public class VerticalTextView extends androidx.appcompat.widget.AppCompatTextView {
boolean topDown;
public boolean getTopDown(){
return topDown;
}
public void setTopDown(boolean key){
this.topDown=key;
}
public VerticalTextView(Context context, AttributeSet attrs){
super(context, attrs);
final int gravity = getGravity();
if(Gravity.isVertical(gravity) && (gravity&Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) {
setGravity((gravity&Gravity.HORIZONTAL_GRAVITY_MASK) | Gravity.TOP);
topDown = false;
}else
topDown = true;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(getMeasuredHeight(),getMeasuredWidth());
}
@Override
protected void onDraw(Canvas canvas) {
TextPaint tp =getPaint();
tp.setColor(getCurrentTextColor());
tp.drawableState=getDrawableState();
canvas.save();
if(topDown){
canvas.translate(getWidth(), 0);
canvas.rotate(90);
}else {
canvas.translate(0, getHeight());
canvas.rotate(-90);
}
canvas.translate(getCompoundPaddingLeft(), getExtendedPaddingTop());
getLayout().draw(canvas);
canvas.restore();
}
}
这是我的项目布局
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:sayaki="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible"
android:layout_marginVertical="5dp">
<VerticalTextView
android:id="@+id/tv_sub_category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sathyan"
android:gravity="center"
android:paddingVertical="1dp"
android:maxLines="1"
android:textSize="14sp"
android:layout_marginVertical="10dp"
android:textColor="@color/black"
android:textStyle="bold"
/>
</LinearLayout>
</layout>
但是在滚动时项目高度会降低。
这是滚动前的样子:
滚动后:
你可以看到最后 3 个项目的高度降低了一些 c
我找到了 problem.I 的解决方案,只是改变了我的垂直 TextView class,如下所示
public class NewVerticalView extends TextView {
private Rect bounds = new Rect();
private TextPaint textPaint;
private int color;
public NewVerticalView(Context context) {
super(context);
}
public NewVerticalView(Context context, AttributeSet attrs) {
super(context, attrs);
color = getCurrentTextColor();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
textPaint = getPaint();
textPaint.getTextBounds((String) getText(), 0, getText().length(), bounds);
setMeasuredDimension((int) (bounds.height() + textPaint.descent()), bounds.width());
}
@Override
protected void onDraw(Canvas canvas) {
textPaint.setColor(color);
canvas.rotate(-90,bounds.width(),0
);
canvas.drawText((String) getText(), 0,- bounds.width() + bounds.height(), textPaint);
}
}
并在 xml.Now 中将旋转添加为 180,我的问题已解决。
我想显示名称 list.I 我正在使用 recylerview 来显示 that.For 我已经使用 Vertical TextView 作为项目 view.it 有效 properly.But 问题是什么时候我滚动 recyclerview,项目高度降低
垂直文本视图 class
public class VerticalTextView extends androidx.appcompat.widget.AppCompatTextView {
boolean topDown;
public boolean getTopDown(){
return topDown;
}
public void setTopDown(boolean key){
this.topDown=key;
}
public VerticalTextView(Context context, AttributeSet attrs){
super(context, attrs);
final int gravity = getGravity();
if(Gravity.isVertical(gravity) && (gravity&Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) {
setGravity((gravity&Gravity.HORIZONTAL_GRAVITY_MASK) | Gravity.TOP);
topDown = false;
}else
topDown = true;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(getMeasuredHeight(),getMeasuredWidth());
}
@Override
protected void onDraw(Canvas canvas) {
TextPaint tp =getPaint();
tp.setColor(getCurrentTextColor());
tp.drawableState=getDrawableState();
canvas.save();
if(topDown){
canvas.translate(getWidth(), 0);
canvas.rotate(90);
}else {
canvas.translate(0, getHeight());
canvas.rotate(-90);
}
canvas.translate(getCompoundPaddingLeft(), getExtendedPaddingTop());
getLayout().draw(canvas);
canvas.restore();
}
}
这是我的项目布局
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:sayaki="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible"
android:layout_marginVertical="5dp">
<VerticalTextView
android:id="@+id/tv_sub_category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sathyan"
android:gravity="center"
android:paddingVertical="1dp"
android:maxLines="1"
android:textSize="14sp"
android:layout_marginVertical="10dp"
android:textColor="@color/black"
android:textStyle="bold"
/>
</LinearLayout>
</layout>
但是在滚动时项目高度会降低。
这是滚动前的样子:
我找到了 problem.I 的解决方案,只是改变了我的垂直 TextView class,如下所示
public class NewVerticalView extends TextView {
private Rect bounds = new Rect();
private TextPaint textPaint;
private int color;
public NewVerticalView(Context context) {
super(context);
}
public NewVerticalView(Context context, AttributeSet attrs) {
super(context, attrs);
color = getCurrentTextColor();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
textPaint = getPaint();
textPaint.getTextBounds((String) getText(), 0, getText().length(), bounds);
setMeasuredDimension((int) (bounds.height() + textPaint.descent()), bounds.width());
}
@Override
protected void onDraw(Canvas canvas) {
textPaint.setColor(color);
canvas.rotate(-90,bounds.width(),0
);
canvas.drawText((String) getText(), 0,- bounds.width() + bounds.height(), textPaint);
}
}
并在 xml.Now 中将旋转添加为 180,我的问题已解决。