2017-05-10 9 views
0

Meine App lädt einige Elemente in die Recycleransicht und legt für jede eine onClickListener fest. Vor zwei Stunden hatte ich das Problem, wo Picasso, Glide oder asyncronous Aufgabe, alle von ihnen die ersten 2 oder 4 Bilder geladen, dann begann es nach dem Zufallsprinzip zu laden (nur die Bilder, die Textansicht und das Element selbst war in Ordnung) und manchmal flackern zwischen verschiedenen Bildern.RecyclerView lädt XML nicht

Ich habe angefangen, meinen Code zu ändern, um zu versuchen, es zu beheben, und jetzt lädt die Recycleransicht nichts.

Kann jemand meinen Code ansehen? Ich habe nur diese 2 Klassen berührt.

Edit: Dies ist, wie es jetzt http://imgur.com/a/4rDLU

EDIT2 aussehen: debug verwenden, das Programm nie

Hauptaktivität

public class Principal extends AppCompatActivity implements AppBarLayout.OnOffsetChangedListener{ 
    public static LugaresBD lugares; 
    private RecyclerView recyclerView; 
    public static AdaptadorLugares adaptador; 
    private android.support.v7.widget.RecyclerView.LayoutManager layoutManager; 
    static final int RESULTADO_PREFERENCIAS = 0; 
    static public ArrayList<Lugar> arrayLugares = new ArrayList<>(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     lugares = new LugaresBD(this); 
     setContentView(R.layout.activity_main); 

     recyclerView = (RecyclerView) 
       findViewById(R.id.recycler_view); 
     adaptador = new AdaptadorLugares(this, lugares, lugares.extraeCursor()); 
     recyclerView.setAdapter(adaptador); 
     layoutManager = new LinearLayoutManager(this); 
     recyclerView.setLayoutManager(layoutManager); 


     adaptador.setOnItemClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent i = new Intent(Principal.this, 
         VistaLugarActivity.class); 
       i.putExtra("id", arrayLugares.get(recyclerView.getChildAdapterPosition(v)).get_id()); 
       startActivityForResult(i, 1433); 
      } 
     }); 
    } 
} 

Adapter

public class AdaptadorLugares extends RecyclerView.Adapter<AdaptadorLugares.ViewHolder> { 
     protected Cursor cursor; 
     protected Lugares lugares; //Lugares a mostrar 
     protected LayoutInflater inflador; //Crea Layouts con el XML 
     protected Context contexto; //Lo necesitamos para el inflador 
     protected View.OnClickListener onClickListener; 
     public static ImageView foto; 
     AsyncTask<String,Void,Bitmap> task = null; 
     Bitmap mBitmap; 

    public AdaptadorLugares(Context contexto, Lugares lugares, Cursor cursor){ 
     this.cursor = cursor; 
     this.contexto = contexto; 
     this.lugares = lugares; 
     inflador = (LayoutInflater) contexto.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 



     public static class ViewHolder extends RecyclerView.ViewHolder { 
      public TextView nombre; 

      public ViewHolder(View itemView) { 
       super(itemView); 
       nombre = (TextView) itemView.findViewById(R.id.nombrecito); 
       foto = (ImageView) itemView.findViewById(R.id.foto); 

      } 
     } 


     @Override 
     public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
      // Inflamos la vista desde el xml 
      View v = inflador.inflate(R.layout.elemento_lista, parent, 
        false); 
      v.setOnClickListener(onClickListener); 
      return new ViewHolder(v); 
     } 


     @Override 
     public void onBindViewHolder(ViewHolder holder, int posicion) { 
      Lugar lugar = lugares.elemento(posicion); 

      holder.nombre.setText(lugar.get_id() + lugar.getNombre()); 
      Picasso.with(contexto).load(lugar.getImage()).into(foto); 



     } 



     @Override 
     public int getItemCount() { 
      return lugares.tamanyo(); 
     } 

     public void setOnItemClickListener(View.OnClickListener onClickListener) { 
      this.onClickListener = onClickListener; 
     } 

     public Cursor getCursor(){ 
      return cursor; 
     } 

     public void setCursor(Cursor cursor){ 
      this.cursor = cursor; 
     } 

     public Lugar lugarPosicion(int posicion){ 
      cursor.moveToPosition(posicion); 
      return LugaresBD.extraeLugar(cursor); 
     } 

     public int idPosicion(int posicion){ 
      cursor.moveToPosition((posicion)); 
      return cursor.getInt(0); 
     } 
} 
die onBindViewHolder oder onCreateViewHolder Methoden eintritt

Vielen Dank.

Edit3:

Aktivität Haupt xml

<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:ignore="RtlHardcoded" 
    > 

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/main.appbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
     > 

     <android.support.design.widget.CollapsingToolbarLayout 
      android:id="@+id/main.collapsing" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      app:layout_scrollFlags="scroll|exitUntilCollapsed|snap" 
      > 

      <ImageView 
       android:id="@+id/main.imageview.placeholder" 
       android:layout_width="match_parent" 
       android:layout_height="300dp" 
       android:scaleType="centerCrop" 
       android:src="@drawable/street_view" 
       android:tint="#11000000" 
       app:layout_collapseMode="parallax" 
       app:layout_collapseParallaxMultiplier="0.9" 
       /> 

      <FrameLayout 
       android:id="@+id/main.framelayout.title" 
       android:layout_width="match_parent" 
       android:layout_height="100dp" 
       android:layout_gravity="bottom|center_horizontal" 
       android:background="@color/primary" 
       android:orientation="vertical" 
       app:layout_collapseMode="parallax" 
       app:layout_collapseParallaxMultiplier="0.3" 
       > 

       <LinearLayout 
        android:id="@+id/main.linearlayout.title" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center" 
        android:orientation="vertical" 
        > 

        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_gravity="center_horizontal" 
         android:gravity="bottom|center" 
         android:text="ITEMS" 
         android:textColor="@android:color/white" 
         android:textSize="30sp" 
         /> 

        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_gravity="center_horizontal" 
         android:layout_marginTop="4dp" 
         android:text="My store STORE" 
         android:textColor="@android:color/white" 
         /> 

       </LinearLayout> 
      </FrameLayout> 
     </android.support.design.widget.CollapsingToolbarLayout> 
    </android.support.design.widget.AppBarLayout> 


    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scrollbars="none" 
     android:layout_marginTop="30dp" 
     android:fitsSystemWindows="true" 
     app:behavior_overlapTop="30dp" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" 

     > 

     <ImageView 
      android:id="@+id/main.backdrop" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:scaleType="centerCrop" 
      android:fitsSystemWindows="true" 
      app:layout_collapseMode="parallax" 
      /> 
     <include layout="@layout/content_principal" /> 


    </RelativeLayout> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/main.toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="@color/primary" 
     app:layout_anchor="@id/main.framelayout.title" 
     app:theme="@style/ThemeOverlay.AppCompat.Dark" 
     app:title="" 
     > 

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:orientation="horizontal" 
      > 

      <Space 
       android:layout_width="@dimen/image_final_width" 
       android:layout_height="@dimen/image_final_width" 
       /> 

      <TextView 
       android:id="@+id/main.textview.title" 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:layout_marginLeft="8dp" 
       android:gravity="center_vertical" 
       android:text="My Store" 
       android:textColor="@android:color/white" 
       android:textSize="20sp" 
       /> 

     </LinearLayout> 
    </android.support.v7.widget.Toolbar> 

    <de.hdodenhof.circleimageview.CircleImageView 
     android:id="@+id/circleView" 
     android:layout_width="@dimen/image_width" 
     android:layout_height="@dimen/image_width" 
     android:layout_gravity="center_horizontal" 
     android:src="@drawable/logo" 
     app:border_color="@android:color/white" 
     app:border_width="2dp" 
     app:finalHeight="@dimen/image_final_width" 
     app:finalYPosition="2dp" 
     app:layout_behavior="com.example.dam202.mislugares.AvatarImageBehavior" 
     app:startHeight="2dp" 
     app:startToolbarPosition="2dp" 
     app:startXPosition="2dp" 
     /> 
</android.support.design.widget.CoordinatorLayout> 

content_principal.xml

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:weightSum="10" 
    android:theme="@style/ThemeOverlay.AppCompat.Light"> 
<android.support.v7.widget.RecyclerView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/recycler_view" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context=".Principal" 
    tools:showIn="@layout/activity_main" 
    /> 


</LinearLayout> 
+0

im ViewHolder, warum Sie nicht 'foto öffentliches Image verwenden;'? – zihadrizkyef

+0

Es ist als statisches ImageView-Foto deklariert; – Tostadora

+0

Ich habe es gerade jetzt gemacht, aber ich habe immer noch das gleiche Problem – Tostadora

Antwort

0

Versuchen Sie, den Bestellcode leicht zu schlagen, wenn Sie den Adapter und stellen Sie die lineare Layout recyclerview gesetzt. Weil RecyclerView nicht funktioniert, wenn es keinen Layout-Manager hat. Also müssen wir zuerst das lineare Layout und dann den Adapter einstellen. Es wird so aussehen:

layoutManager = new LinearLayoutManager(this); 
recyclerView.setLayoutManager(layoutManager); 
adaptador = new AdaptadorLugares(this, lugares, lugares.extraeCursor()); 
recyclerView.setAdapter(adaptador); 

Dann public ImageView foto; auf Ihre ViewHolder Klasse zu setzen versuchen. So wird es so aussehen:

public static class ViewHolder extends RecyclerView.ViewHolder { 
    public ImageView foto; 
    public TextView nombre; 

    public ViewHolder(View itemView) { 
     super(itemView); 
     nombre = (TextView) itemView.findViewById(R.id.nombrecito); 
     foto = (ImageView) itemView.findViewById(R.id.foto); 
    } 
} 

und verwenden auch die holder innerhalb onBindViewHolder(). So wird es so aussehen:

@Override 
    public void onBindViewHolder(ViewHolder holder, int posicion) { 
     Lugar lugar = lugares.elemento(posicion); 

     holder.nombre.setText(lugar.get_id() + lugar.getNombre()); 
     Picasso.with(contexto).load(lugar.getImage()).into(holder.foto); 
    } 

Und stellen Sie sicher getItemCount() ist mehr zurückgeben als Null

+0

Danke für die Antworten. Es ist bereits definiert das ImageView – Tostadora

+0

Ich bearbeite es einfach. Sie müssen den Halter Mann verwenden, das ist der richtige Weg Recycler Ansicht zu verwenden: D – zihadrizkyef

+0

Und Sie müssen es außerhalb der ViewHolder-Klasse nicht deklarieren: D Das ist der Zweck von ViewHolder von RecyclerView: D – zihadrizkyef