我的應用程序將一些項目加載到回收站視圖中,併爲它們中的每一個設置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>
多在ViewHolder你爲什麼不使用'公共ImageView的照片;'? – zihadrizkyef
它被聲明爲公共靜態ImageView foto; – Tostadora
我剛剛做了它,但我仍然有同樣的問題 – Tostadora