2017-05-10 77 views
0

我的應用程序將一些項目加載到回收站視圖中,併爲它們中的每一個設置onClickListeners。兩個小時前,我遇到了Picasso,Glide或異步任務的問題,所有這些任務都將前兩個或4個圖像加載得很好,然後開始隨機加載(僅圖像,文本視圖和項目本身很好),並且有時會閃爍不同的圖像之間。RecyclerView不加載XML

我已經開始更改我的代碼以嘗試修復它,現在,回收站視圖不會加載任何內容。

有人可以看看我的代碼嗎?我只涉及這兩個班。

編輯:這是現在如何看待http://imgur.com/a/4rDLU

EDIT2:使用調試,程序不會進入onBindViewHolder或onCreateViewHolder方法

主要活動

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

適配器

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

謝謝。

EDIT3:

活動主要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

多在ViewHolder你爲什麼不使用'公共ImageView的照片;'? – zihadrizkyef

+0

它被聲明爲公共靜態ImageView foto; – Tostadora

+0

我剛剛做了它,但我仍然有同樣的問題 – Tostadora

回答

0

嘗試當您設置適配器以及recyclerview的線性佈局翻轉命令代碼。因爲如果沒有佈局管理器,RecyclerView將無法工作。所以我們必須先設置線性佈局,然後再設置適配器。它會是這樣的:

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

然後嘗試將public ImageView foto;放到您的ViewHolder類中。因此,這將是這樣的:

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

而且還使用了holderonBindViewHolder()。因此,這將是這樣的:

@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); 
    } 

並確保getItemCount()是返回大於零

+0

感謝您的答案。它已經定義了imageView – Tostadora

+0

我只是編輯它。您必須使用持有人,這是使用回收商視圖的正確方法:D – zihadrizkyef

+0

而且您不需要在ViewHolder類之外聲明它:D 這就是RecyclerView的ViewHolder的用途:D – zihadrizkyef