RecyclerView 项目高度和重力不起作用

Posted

技术标签:

【中文标题】RecyclerView 项目高度和重力不起作用【英文标题】:RecyclerView item height and gravity not working 【发布时间】:2020-04-17 10:49:34 【问题描述】:

如图所示,我在水平回收站视图中遇到了项目高度和重力问题:RecyclerView item height and gravity not working

如何将item高度设置为与recyclerciew相同的高度和recyclerview底部的重力?

感谢您的帮助。

我的 Activity_Drill_Graph.xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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_
    android:layout_
    tools:context=".Drill_Graph_Activity">

    <LinearLayout
        android:layout_
        android:layout_
        android:orientation="vertical">

        <include
            android:id="@+id/toolbar_drill_graph"
            layout="@layout/toolbar_drill_graph" />

        <LinearLayout
            android:layout_
            android:layout_
            android:orientation="vertical">

            <LinearLayout
                android:layout_
                android:layout_
                android:layout_margin="@dimen/margin_5dp"
                android:background="@color/grey"
                android:gravity="bottom"
                android:orientation="horizontal"
                android:weightSum="100">

                <LinearLayout
                    android:layout_
                    android:layout_
                    android:layout_gravity="bottom"
                    android:background="@color/lightgrey"
                    android:orientation="vertical"
                    android:padding="@dimen/padding_5dp">

                    <TextView
                        android:id="@+id/textView10"
                        android:layout_
                        android:layout_
                        android:text="@string/_10"
                        android:textAlignment="textEnd"
                        android:textColor="@color/black"
                        android:textSize="14sp" />

                    <TextView
                        android:id="@+id/textView9"
                        android:layout_
                        android:layout_
                        android:text="@string/_9"
                        android:textAlignment="textEnd"
                        android:textColor="@color/black"
                        android:textSize="14sp" />

                    <TextView
                        android:id="@+id/textView8"
                        android:layout_
                        android:layout_
                        android:text="@string/_8"
                        android:textAlignment="textEnd"
                        android:textColor="@color/black"
                        android:textSize="14sp" />

                    <TextView
                        android:id="@+id/textView7"
                        android:layout_
                        android:layout_
                        android:text="@string/_7"
                        android:textAlignment="textEnd"
                        android:textColor="@color/black"
                        android:textSize="14sp" />

                    <TextView
                        android:id="@+id/textView6"
                        android:layout_
                        android:layout_
                        android:text="@string/_6"
                        android:textAlignment="textEnd"
                        android:textColor="@color/black"
                        android:textSize="14sp" />

                    <TextView
                        android:id="@+id/textView5"
                        android:layout_
                        android:layout_
                        android:text="@string/_5"
                        android:textAlignment="textEnd"
                        android:textColor="@color/black"
                        android:textSize="14sp" />

                    <TextView
                        android:id="@+id/textView4"
                        android:layout_
                        android:layout_
                        android:text="@string/_4"
                        android:textAlignment="textEnd"
                        android:textColor="@color/black"
                        android:textSize="14sp" />

                    <TextView
                        android:id="@+id/textView3"
                        android:layout_
                        android:layout_
                        android:text="@string/_3"
                        android:textAlignment="textEnd"
                        android:textColor="@color/black"
                        android:textSize="14sp" />

                    <TextView
                        android:id="@+id/textView2"
                        android:layout_
                        android:layout_
                        android:text="@string/_2"
                        android:textAlignment="textEnd"
                        android:textColor="@color/black"
                        android:textSize="14sp" />

                    <TextView
                        android:id="@+id/textView1"
                        android:layout_
                        android:layout_
                        android:text="@string/_1"
                        android:textAlignment="textEnd"
                        android:textColor="@color/black"
                        android:textSize="14sp" />

                    <TextView
                        android:id="@+id/textView0"
                        android:layout_
                        android:layout_
                        android:text="@string/_0"
                        android:textAlignment="textEnd"
                        android:textColor="@color/black"
                        android:textSize="14sp" />

                    <TextView
                        android:id="@+id/textViewDate"
                        android:layout_
                        android:layout_
                        android:text="@string/date"
                        android:textAlignment="textEnd"
                        android:textColor="@color/black"
                        android:textSize="18sp" />
                </LinearLayout>

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/recyclerViewDrillGraph"
                    android:layout_
                    android:layout_
                    android:layout_gravity="bottom"
                    android:layout_margin="@dimen/margin_5dp"
                    android:background="@color/white"
                    android:orientation="horizontal"
                    android:padding="@dimen/padding_5dp"
                    android:scrollbars="horizontal" />

            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

</android.support.constraint.ConstraintLayout>

项目代码:drill_graph_single_column.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"
    android:layout_
    android:layout_
    android:layout_gravity="bottom"
    android:background="@color/lightgrey"
    android:gravity="bottom"
    android:orientation="vertical"
    android:padding="@dimen/padding_1dp"
    android:weightSum="110">

    <Button
        android:id="@+id/btnSkill"
        style="@style/btnStyleScoreGrey"
        android:layout_
        android:layout_
        android:layout_weight="50"
        android:text="@string/skill"
        android:textAllCaps="false"
        android:textSize="16sp" />

    <Button
        android:id="@+id/btnDate"
        style="@style/btnStyleDrillGraphGrey"
        android:layout_
        android:layout_
        android:layout_marginTop="@dimen/margin_1dp"
        android:layout_weight="10"
        android:text="@string/date"
        android:textAllCaps="false"
        android:textSize="12sp" />

</LinearLayout>

我的 RecyclerView 适配器代码:

package org.poolshot.poolshotacademy;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import java.util.List;

public class My_DrillGraph_RecyclerView_Adapter extends RecyclerView.Adapter<My_DrillGraph_RecyclerView_Adapter.MyHolder> 
    SharedPreferences defaultPreferencesSettings;
    private Context myRCAContext;
    private List<String> myDrillGraphDates;
    private List<Integer> myDrillGraphSkill;

    // constructor
    public My_DrillGraph_RecyclerView_Adapter(Context applicationContext, List<String> mDrillGraphDates, List<Integer> mDrillGraphSkill) 
        this.myRCAContext = applicationContext;
        this.myDrillGraphDates = mDrillGraphDates;
        this.myDrillGraphSkill = mDrillGraphSkill;
    

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int position) 
        View layout = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.drill_graph_single_column,null);
        MyHolder myHolder = new MyHolder(layout);
        return myHolder;
    

    @Override
    public void onBindViewHolder(@NonNull final MyHolder viewHolder, final int position) 
        String skill = myDrillGraphSkill.get(position).toString();
        // set color for buttons
        viewHolder.btnDate.setBackground(myRCAContext.getResources().getDrawable(R.drawable.custom_btn_grey));
        viewHolder.btnSkill.setTextColor(myRCAContext.getResources().getColor(R.color.white));
        // set text for buttons
        viewHolder.btnDate.setText(myDrillGraphDates.get(position));
        viewHolder.btnSkill.setText(skill);
    

    @Override
    public int getItemCount() 
        return myDrillGraphDates.size();
    

    public static class MyHolder extends RecyclerView.ViewHolder 
        Button btnDate;
        Button btnSkill;

        public MyHolder(View itemView) 
            super(itemView);
            btnDate = itemView.findViewById(R.id.btnDate);
            btnSkill = itemView.findViewById(R.id.btnSkill);
        
    

【问题讨论】:

请分享所需视图的图片/绘图。 图片链接在原帖中。我们清楚地看到物品的位置和高度。它们的背景颜色与recyclerview 不同。项目应位于回收站视图的底部并具有 match_parent 高度。 【参考方案1】:

这是我的解决方案: 我将 MyHolder 类的代码更改为:

public static class MyHolder extends RecyclerView.ViewHolder 
    Button btnDate;
    Button btnSkill;

    public MyHolder(View itemView) 
        super(itemView);
        btnDate = itemView.findViewById(R.id.btnDate);
        btnSkill = itemView.findViewById(R.id.btnSkill);
        LinearLayout.LayoutParams layoutParams =
                new LinearLayout.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.MATCH_PARENT);
        layoutParams.gravity = Gravity.BOTTOM;
        itemView.setLayoutParams(layoutParams);
    

【讨论】:

以上是关于RecyclerView 项目高度和重力不起作用的主要内容,如果未能解决你的问题,请参考以下文章

Android center_vertical 重力不起作用

如何将 RecyclerView 添加到 LinearLayout(高度问题)

recyclerView 的 setOnClickListener 不起作用

RecyclerView的项目XML中的Android TextView左重力问题

RecyclerView分页不起作用

Android recyclerView 项目布局参数不起作用