Android RecyclerView-首次加载时忽略布局重量

Android RecyclerView-首次加载时忽略布局重量,android,android-layout,android-fragments,android-recyclerview,Android,Android Layout,Android Fragments,Android Recyclerview,我有一个包含片段的活动。片段包含一个RecyclerView,显示CardView CardView非常简单,只有一个TextView和一个CustomView。这是布局图: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizo

我有一个包含片段的活动。片段包含一个RecyclerView,显示CardView

CardView非常简单,只有一个TextView和一个CustomView。这是布局图:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="100dp">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:id="@+id/card_current_heat_cardview"
        android:layout_gravity="center_vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:weightSum="4">

            <TextView
                android:id="@+id/card_current_heat_textview"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:textStyle="bold"
                android:textColor="#000000"
                android:background="@android:color/holo_red_dark"
                android:gravity="center_vertical"/>

            <com.amige.vm2.CurrentHeatChartView
                android:id="@+id/card_current_heat_chart_view"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="3"/>

        </LinearLayout>

    </android.support.v7.widget.CardView>

</LinearLayout>

但是如果我上下滚动,一些卡片的布局会像预期的那样工作

我还有另一个活动(这里没有涉及片段),它有一个RecyclerView,并且使用相同的CardView布局,它工作得非常好

我不知道这是否与碎片有关

我错过了什么

谢谢

编辑

这是适配器代码

public class MainAdapter extends RecyclerView.Adapter<MyFragment.MainAdapter.ViewHolder>
    {
        public MainAdapter()
        {

        }

        @Override
        public MyFragment.MainAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
        {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_current_heat, parent, false);

            return new MyFragment.MainAdapter.ViewHolder(v);
        }

        @Override
        public void onBindViewHolder(MyFragment.MainAdapter.ViewHolder holder, int position)
        {
            if (arrayList.size() == 2)
            {
                if (position == 0)
                {
                    holder.variableNameTextView.setText(“Var Name”);

                    holder.currentHeatChartView.reloadChartWithSamples(arrayList.get(0));
                }
                else
                {
                    if ((position - 1) < arrayList.get(1).size())
                    {
                        HashMap variableHashMap = (HashMap) arrayList.get(1).get(position - 1);

                        holder.variableNameTextView.setText(variableHashMap.get("VarName") != null ? (String)variableHashMap.get("VarName") : "");

                        holder.currentHeatChartView.reloadChartWithVariableValuesAndColors(variableHashMap, colorGradientsArrayList.get((position - 1) % colorGradientsArrayList.size()));
                    }
                }
            }
        }

        @Override
        public int getItemCount()
        {
            if (arrayList.size() == 2)
            {
                if (arrayList.get(1).size() > 0)
                {
                    return 1 + arrayList.get(1).size();
                }
                else
                {
                    return 1;
                }
            }
            return 0;
        }

        public class ViewHolder extends RecyclerView.ViewHolder
        {
            TextView variableNameTextView;

            CurrentHeatChartView currentHeatChartView;

            public ViewHolder(View v)
            {
                super(v);

                this.variableNameTextView = (TextView) v.findViewById(R.id.card_current_heat_textview);

                this.currentHeatChartView = (CurrentHeatChartView) v.findViewById(R.id.card_current_heat_chart_view);
            }
        }
    }
公共类MainAdapter扩展了RecyclerView.Adapter { 公共MainAdapter() { } @凌驾 public MyFragment.MainAdapter.ViewHolder onCreateViewHolder(视图组父级,int-viewType) { 视图v=LayoutInflater.from(parent.getContext()).flate(R.layout.card\u current\u heat,parent,false); 返回新的MyFragment.MainAdapter.ViewHolder(v); } @凌驾 BindViewHolder上的公共无效(MyFragment.MainAdapter.ViewHolder,int位置) { if(arrayList.size()=2) { 如果(位置==0) { holder.variableNameTextView.setText(“变量名称”); holder.currentHeatChartView.reloadChartWithSamples(arrayList.get(0)); } 其他的 { if((位置-1)0) { 返回1+arrayList.get(1.size(); } 其他的 { 返回1; } } 返回0; } 公共类ViewHolder扩展了RecyclerView.ViewHolder { TextView变量NameTextView; CurrentHeatChartView CurrentHeatChartView; 公共视图持有者(视图v) { 超级(五); this.variableNameTextView=(TextView)v.findViewById(R.id.card\u current\u heat\u TextView); this.currentHeatChartView=(currentHeatChartView)v.findViewById(R.id.card\u current\u heat\u chart\u view); } } }
我认为您的布局错误

View view = View.inflate(mContext, R.layout.your_layout, null);
如果您确实喜欢这样,请像这样更改代码

View view = LayoutInflater.from(mContext).(R.layout.your_layout, parent, false);
希望有帮助:)

我找到了罪犯

在包含该片段的活动层次结构的顶部有一个ConstraintLayout。因为我使用的是嵌套布局,所以ConstraintLayout不是正确的方法


我将其更改为RelativeLayout,现在卡已正确呈现。

发布适配器code@BhuvaneshBs添加适配器代码更改无效