1

私は、断片を含むアクティビティを持っています。フラグメントにはRecyclerViewが含まれ、CardViewが表示されます。RecyclerView - Layout_weightは初回読み込み時に無視されます

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> 

TextViewは幅の1/4とCustomView 3/4を使用する必要があります。重みがRecyclerView負荷

Image of RecyclerView loaded for the first time

初めて無視しかし、私は上下にスクロールしている場合、一部のカード

Image of RecyclerView scrolled up and down

のために予想されるように、レイアウトが機能している理由のトラブルの理解を持つ

イム

私はRecyclerViewを持っていて、この同じCardViewレイアウトを使用していて、それが完璧に動作する別のアクティビティ(ここではフラグメントはありません)を持っています!

私はそれがフラグメントに関係している場合は知らない

...

私は何をしないのですか?

ありがとうございます!

EDIT

このアダプターコード

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); 
      } 
     } 
    } 
+0

あなたのアダプターコードをポスト –

+0

@BhuvaneshBsアダプターコード – user1553028

答えて

3

私は犯人を見つけた願っています。

フラグメントを含むアクティビティの階層の最上部には、ConstraintLayoutがありました。ネストされたレイアウトで作業していたので、ConstraintLayoutは正しい方法ではありませんでした。

RelativeLayoutに変更し、カードが正しくレンダリングされるようになりました。

0

私はあなたのレイアウトインフレが間違っていると思います。

View view = View.inflate(mContext, R.layout.your_layout, null); 

このようにすれば、このようにコードを変更できます。

View view = LayoutInflater.from(mContext).(R.layout.your_layout, parent, false); 

は、それが役立ちます:)

+0

変更は効果がありません<:o(...同じ奇妙な動作 – user1553028

関連する問題