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

我试图把所有固定的尺寸和时间不提高充气。 –