2016-01-22 46 views
-1

我想動態地設置在CardView中的RecycleView中的TextView上的maxLines。動態設置CardView中的TextView中的maxLines

這裏是我的CardView_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="16dp"> 

    <android.support.v7.widget.CardView 
     android:id="@+id/fragment_advice_cardview_CV_cardview" 
     android:layout_width="match_parent" 
     android:layout_height="150sp" 
     card_view:cardCornerRadius="5sp" 
     card_view:cardElevation="5sp" 
     card_view:contentPadding="2dp" 
     card_view:cardUseCompatPadding="true"> 


     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="@color/green_background" 
      android:padding="16sp"> 

      <ImageView 
       android:id="@+id/fragment_advice_cardview_IV_doctor_photo" 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:layout_alignParentStart="true" 
       android:layout_alignParentTop="true" 
       android:layout_marginEnd="16sp" 
       android:adjustViewBounds="true" 
       android:maxWidth="150sp" 
       android:scaleType="fitCenter" 
       android:src="@drawable/batman_logo" /> 

      <TextView 
       android:id="@+id/fragment_advice_cardview_TV_doctor_name" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_alignParentTop="true" 
       android:layout_toEndOf="@+id/fragment_advice_cardview_IV_doctor_photo" 
       android:text="@string/omg" 
       android:textSize="30sp" /> 

      <TextView 
       android:id="@+id/fragment_advice_cardview_TV_doctor_advice" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_below="@+id/fragment_advice_cardview_TV_doctor_name" 
       android:layout_toEndOf="@+id/fragment_advice_cardview_IV_doctor_photo" 
       android:text="@string/omg" /> 

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

這裏是我的fragment_layout.xml:

<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/fragment_advice_VS_view_switcher" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".android.fragments.AdviceFragment"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/fragment_advice_RV_list" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <TextView 
     android:id="@+id/fragment_advice_TV_empty_error_message" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</ViewSwitcher> 

我的適配器:

public class RVAdapter extends RecyclerView.Adapter<RVAdapter.DoctorViewHolder> { 
    private final ArrayList<Doctor> doctors; 

    public RVAdapter(ArrayList<Doctor> doctors) { 
     this.doctors = doctors; 
    } 


    @Override 
    public DoctorViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_advice_cardview_layout, parent, false); 

     final TextView doctorAdviceTV = (TextView) view.findViewById(R.id.fragment_advice_cardview_TV_doctor_advice); 
     System.out.println("doctorAdviceTV.getId() : " + doctorAdviceTV.getId()); 
     System.out.println("doctorAdviceTV.getHeight() : " + doctorAdviceTV.getHeight()); 


     ViewTreeObserver doctorAdviceTVviewTreeObserver = doctorAdviceTV.getViewTreeObserver(); 
     doctorAdviceTVviewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
      @Override 
      public void onGlobalLayout() { 
       doctorAdviceTV.getViewTreeObserver().removeOnGlobalLayoutListener(this); 

       int maxLines = doctorAdviceTV.getHeight()/doctorAdviceTV.getLineHeight(); 
       BaseActivity.logD("RVAdapter", "maxLines : " + maxLines + " = " + doctorAdviceTV.getHeight() + "/" + doctorAdviceTV.getLineHeight()); 
       doctorAdviceTV.setMaxLines(maxLines); 
       doctorAdviceTV.setEllipsize(TextUtils.TruncateAt.END); 
      } 
     }); 

     DoctorViewHolder dvh = new DoctorViewHolder(view); 
     return dvh; 
    } 

    @Override 
    public void onBindViewHolder(DoctorViewHolder holder, int position) { 
     Doctor doctor = doctors.get(position); 
     holder.doctorNameTV.setText(doctor.getSettingValue(Doctor.SETTINGS.NAME)); 
     holder.doctorAdviceTV.setText(doctor.getSettingValue(Doctor.SETTINGS.ADVICE)); 

     Bitmap profilePicture = doctor.getProfilePicture(); 
     if (profilePicture == null) { 
      holder.doctorImageIV.setImageResource(R.drawable.batman_logo); 
     } else { 
      holder.doctorImageIV.setImageBitmap(profilePicture); 
     } 
    } 

    @Override 
    public int getItemCount() { 
     return doctors.size(); 
    } 

    public static class DoctorViewHolder extends RecyclerView.ViewHolder { 

     CardView cv; 
     TextView doctorNameTV; 
     TextView doctorAdviceTV; 
     ImageView doctorImageIV; 

     public DoctorViewHolder(View itemView) { 
      super(itemView); 
      cv = (CardView) itemView.findViewById(R.id.fragment_advice_cardview_CV_cardview); 
      doctorNameTV = (TextView) itemView.findViewById(R.id.fragment_advice_cardview_TV_doctor_name); 
      doctorAdviceTV = (TextView) itemView.findViewById(R.id.fragment_advice_cardview_TV_doctor_advice); 
      doctorImageIV = (ImageView) itemView.findViewById(R.id.fragment_advice_cardview_IV_doctor_photo); 
     } 
    } 
} 

當然的結果

在圖片之前NGE:

before pic change

pic 1 change

pic 2 change

正如你所看到的第一個CardView的TextView被正確地更新到線的正確數量,第二個卻保持1即使顯然更空間。

請幫忙。

謝謝

回答

1

嘗試

textView.setMaxLines(("This is a \n \n test".split("\n").length+1));