我的应用将一些项目加载到回收站视图中,并为每个项目设置onClickListeners。两个小时前我遇到了Picasso,Glide或asyncronous任务的问题,所有这些都加载了前2或4个图像,然后开始随机加载(只有图像,textview和项目本身很好)并且有时会闪烁在不同的图像之间。
我已经开始更改我的代码以尝试修复它,现在,回收站视图无法加载任何内容。
有人可以看看我的代码吗?我只触及了这两个班级。
编辑:这就是它现在的样子http://imgur.com/a/4rDLU
Edit2:使用debug,程序永远不会进入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 :(得分: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);
}
}
并使用holder
内的onBindViewHolder()
。所以它会是这样的:
@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()
返回大于零