0

想了很多關於這個文檔,我決定分享我的代碼和問題。 我有我的下一個觀點的傾向。 Item_grid.xmlreciclerview GridLayoutManager很慢

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:card_view="http://schemas.android.com/apk/res-auto" 
android:id="@+id/card" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_margin="0dp" 

android:background="@color/blanco" 
card_view:cardCornerRadius="0dp" 
card_view:cardElevation="0dp" 
card_view:cardUseCompatPadding="true"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 

    android:orientation="vertical"> 

    <RelativeLayout 
     android:id="@+id/trlDiaNombre" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:background="@color/naranjaffb973" 
     android:minHeight="30dp" 
     android:visibility="visible"> 

     <TextView 
      android:id="@+id/txtADPDiaNombre" 
      android:layout_width="match_parent" 
      android:layout_height="30dp" 
      android:gravity="center" 
      android:singleLine="true" 
      android:text="DIA" 
      android:textColor="@color/blanco" 
      android:textSize="14sp" 
      android:textStyle="bold" /> 


    </RelativeLayout> 

    <RelativeLayout 
     android:id="@+id/rtlDiaNumero" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:gravity="center" 
     android:minHeight="40dp"> 

     <TextView 
      android:id="@+id/txtADPDiaNumero" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:text="6" 
      android:textColor="@color/morao_moraito_8C008C" 
      android:textSize="18sp" /> 


    </RelativeLayout> 

</LinearLayout> 

這是簡單的兩個Textviews。

是適配器 這是充氣視圖 adapter.java

public class CustomAdapterListadoFechasRCV 
    extends 
    RecyclerView.Adapter<CustomAdapterListadoFechasRCV.ElementosLinea> { 

private ArrayList<Cdia> mDataset; 

private AfrmProgramaControl afrmGes; 


// Provide a reference to the views for each data item 
// Complex data items may need more than one view per item, and 
// you provide access to all the views for a data item in a view holder 
public class ElementosLinea extends RecyclerView.ViewHolder { 
    // each data item is just a string in this case 

    TextView txtNombre; 
    TextView txtNumero; 
    RelativeLayout rtlNombre; 
    RelativeLayout rtlNumero; 
    // CardView card; 

    public ElementosLinea(View v) { 
     super(v); 
     ArrayList<CUtilsTipografia.CtipoGraf> lstTipograf = new ArrayList<CUtilsTipografia.CtipoGraf>(); 
     // card = (CardView) v.findViewById(R.id.card); 
     txtNombre = (TextView) v.findViewById(R.id.txtADPDiaNombre); 
     txtNumero = (TextView) v.findViewById(R.id.txtADPDiaNumero); 
     rtlNombre = (RelativeLayout) v.findViewById(R.id.trlDiaNombre); 
     rtlNumero = (RelativeLayout) v.findViewById(R.id.rtlDiaNumero); 
     lstTipograf.add(new CUtilsTipografia.CtipoGraf(txtNombre, 0, 1)); 
     lstTipograf.add(new CUtilsTipografia.CtipoGraf(txtNumero, 0, 0)); 
     CUtilsTipografia.setTypefaces(afrmGes.getActivity(), lstTipograf); 

    } 

} 

public void add(int position, Cdia item) { 
    mDataset.add(position, item); 
    notifyItemInserted(position); 
} 

public void remove(Cdia item) { 
    int position = mDataset.indexOf(item); 
    mDataset.remove(position); 
    notifyItemRemoved(position); 


} 


// Provide a suitable constructor (depends on the kind of dataset) 
public CustomAdapterListadoFechasRCV(
     ArrayList<Cdia> myDataset, AfrmProgramaControl afrm) { 
    mDataset = myDataset; 
    afrmGes = afrm; 
} 

// Create new views (invoked by the layout manager) 
@Override 
public ElementosLinea onCreateViewHolder(
     ViewGroup parent, int viewType) { 
    // create a new view 
    View v = LayoutInflater.from(parent.getContext()).inflate(
      R.layout.ll_adapter_listado_fechas, parent, false); 
    // set the view's size, margins, paddings and layout parameters 
    ElementosLinea vh = new ElementosLinea(v); 
    return vh; 
} 

// Replace the contents of a view (invoked by the layout manager) 
@Override 
public void onBindViewHolder(ElementosLinea holder, final int position) { 
    // - get element from your dataset at this position 
    // - replace the contents of the view with that element 
    final Cdia datossss = mDataset.get(position); 

    if (datossss != null) { 


     if (datossss.getDia() == -1) {//s 
      holder.rtlNombre.setVisibility(View.VISIBLE); 
      holder.rtlNumero.setVisibility(View.GONE); 
      holder.txtNombre.setText(datossss.getNombreDia()); 

     } else { 
      holder.rtlNombre.setVisibility(View.GONE); 
      holder.rtlNumero.setVisibility(View.VISIBLE); 
      if (datossss.getDia() != 0) { 

       if (datossss.getMarcado() == 1) { 
        holder.txtNumero.setTextColor(afrmGes.getResources().getColor(R.color.marcacalendarioff4000)); 
       } else { 
        holder.txtNumero.setTextColor(afrmGes.getResources().getColor(R.color.grisclarod8d8d8)); 
       } 

       holder.txtNumero.setText(String.valueOf(datossss.getDia())); 
      } else { 
       holder.txtNumero.setText(""); 
      } 


     } 


     // 


    } 


    holder.rtlNumero.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Cdia dia = mDataset.get(position); 
      afrmGes.cargarProgramaDia(dia); 
     } 
    }); 

} 

// Return the size of your dataset (invoked by the layout manager) 
@Override 
public int getItemCount() { 
    return mDataset.size(); 
} 

}

,你可以看到適配器,我的適配器只需要幾天 CDIA的列表。 java

public class Cdia { 


private int _iDia; 
private String _sNombreDia; 
private int _iMarcado; 
private int _iMes; 


public Cdia() { 
    _iDia = 0; 
    _sNombreDia = ""; 
    _iMarcado = 0; 
    _iMes =0; 
} 


public int getDia() { 
    return _iDia; 
} 

public void setDia(int _iDia) { 
    this._iDia = _iDia; 
} 

public String getNombreDia() { 
    return _sNombreDia; 
} 

public void setNombreDia(String _sNombreDia) { 
    this._sNombreDia = _sNombreDia; 
} 


public int getMarcado() { 
    return _iMarcado; 
} 

public void setMarcado(int _iMarcado) { 
    this._iMarcado = _iMarcado; 
} 


public int getMes() { 
    return _iMes; 
} 

public void setMes(int _iMes) { 
    this._iMes = _iMes; 
} 

一旦一切準備就緒,我將解釋我的問題 gridLayoutManages的這種分佈被分成七列,這樣我就會爲每列生成兩個月的相應日曆天,如下所示。 enter image description here

自從成爲適配器的數據列表,直到它在pantalls通過太長,超過4秒。這是多麼充分我的適配器。

enter image description here

我沒能找到一個解決我的加載速度的問題 有人可以給我光在我的黑暗它的路徑? 謝謝。

+1

我有使用Rcyclerview和GridLayoutManager類似的性能問題。經過幾個小時的頭撞。我注意到我有一個不透明度爲0.2的視圖,用作我的項目視圖中的疊加層。當我將不透明度更改回1.所有性能問題都消失了。看起來不透明度圖很重。 –

回答

0

我可以看到許多方法來提高你的代碼的性能,但這些不會減少4個整秒。

我猜如果從數據庫中獲取myDataset(或者您獲取數據),大部分性能問題。

嘗試測量您的代碼的特定部分,以更好地瞭解到底需要這麼長時間。一些代碼塊之前

long before = System.currentTimeMillis(); 

,並: 可以通過增加測量

long after = System.currentTimeMillis(); 

後,然後打印了多長時間(毫秒):反正(after - before)

中,東西你可能改善你的代碼(但可能不會有太大的區別):

  1. 在適配器的構造函數中僅獲取LayoutInflater一次,並將其存儲到成員中。
  2. 重複使用相同的OnClickListener而不是在綁定方法上創建新對象,然後可以通過檢查傳遞給OnClick方法的視圖來決定採取哪種操作。
+0

您好,我的加載數據源需要0.2 ms的速度非常快,當您必須在屏幕上繪製所有框時,我的問題纔會出現,大多數框的onclick方法都被禁用。 –

0

在你xml文件,嘗試從wrap_content改變屬性的值固定值或match_parent因爲每一個觀點是init或重用的時候,如果heigh/width價值wrap_content這意味着OS始終計算以獲得最佳大小和它需要很多的表現。 您可以通過登錄onMeasure()來查看它。

+0

我嘗試了你說的,並且在繪畫中仍然是tardardo,我把所有的固定大小。 如果你看我的處置沒有任何寬度的wrap_content。 –

+0

寬度和高度都應該是值固定的測試性能,我認爲 –

+0

我試圖把所有固定的尺寸和時間不提高充氣。 –