在Recycler View中显示照片APP的奇怪图标?

时间:2016-05-02 01:01:35

标签: android android-recyclerview

我正在使用GridLayoutManager制作一个Recycler View,用于显示存储在Disk上的一组数据。但是当我更新第一个元素时,它随机显示我的图像的图标,底部是照片应用程序图标,当我点击它时,它会打开图片照片应用程序中的图像。这里发生了什么? enter image description here

你可以在那里看到图标。 这是我的代码:

适配器

public class AdapterForCardView extends RecyclerView.Adapter<AdapterForCardView.CardAdapterHolder>
{
private Context context;
private File[] image_files;
private File[] strings_of_files;
private String logging=getClass().getSimpleName();
public AdapterForCardView(Context context, File[] image_files, File[] strings_of_files)
{
    this.context = context;
    this.image_files = image_files;
    this.strings_of_files = strings_of_files;
}

@Override
public CardAdapterHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
    View view= LayoutInflater.from(context).inflate(R.layout.card_view_recycler,parent,false);
    return new CardAdapterHolder(view);
}

@Override
public void onBindViewHolder(CardAdapterHolder holder, int position)
{
    Glide.with(context).load(image_files[position]).into(holder.getImageView());
    if (position!=holder.Position())
    {
        if (holder.Position()!=-1)holder.getLoadingText().cancel(true);
        removeAllText(holder.getTableLayout());
        LoadingText loadingText = new LoadingText(context,holder.getTableLayout());
        holder.setLoadingText(loadingText, position);
        loadingText.execute(strings_of_files[position]);
    }
}

private void removeAllText(TableLayout tableLayout)
{
    tableLayout.removeAllViews();
}

public void setStrings_of_files(File[] strings_of_files)
{
    this.strings_of_files = strings_of_files;
}

public void setImage_files(File[] image_files)
{
    this.image_files = image_files;
}

@Override
public int getItemCount()
{
    return image_files==null?0:image_files.length;
}

class CardAdapterHolder extends RecyclerView.ViewHolder
{
    private TableLayout tableLayout;
    private ImageView imageView;
    private LoadingText loadingText;
    private int position;
    public CardAdapterHolder(View itemView)
    {
        super(itemView);
        imageView=(ImageView)itemView.findViewById(R.id.image_view_card_view);
        tableLayout=(TableLayout)itemView.findViewById(R.id.tableLayout);
        position=-1;
    }

    public ImageView getImageView()
    {
        return imageView;
    }

    public TableLayout getTableLayout()
    {
        return tableLayout;
    }

    public LoadingText getLoadingText()
    {
        return loadingText;
    }

    public void setLoadingText(LoadingText loadingText,int position)
    {
        this.loadingText = loadingText;
        this.position=position;
    }

    public int Position()
    {
        return position;
    }
}
}

布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_marginBottom="5dp"
         android:layout_marginTop="5dp">

<android.support.v7.widget.CardView android:id="@+id/card_view"
                                    android:layout_width="match_parent"
                                    android:layout_height="250dp"
                                    android:layout_gravity="center"
                                    android:layout_marginEnd="3dp"
                                    android:layout_marginLeft="3dp"
                                    android:layout_marginRight="3dp"
                                    android:layout_marginStart="3dp"
                                    app:cardBackgroundColor="@color/white"
                                    app:cardCornerRadius="4dp">
    <RelativeLayout android:layout_width="match_parent"
                    android:layout_height="match_parent">
        <ImageView android:id="@+id/image_view_card_view"
                   android:layout_width="match_parent"
                   android:layout_height="170dp"
                   android:scaleType="centerCrop"/>
        <TableLayout android:id="@+id/tableLayout"
                     android:layout_width="match_parent"
                     android:layout_height="80dp"
                     android:layout_below="@id/image_view_card_view"/>
    </RelativeLayout>

</android.support.v7.widget.CardView>

这是关于我如何更新数据并通知第一个项目被更改的代码

public class AsyncTaskForInternalFiles extends AsyncTask<Void, Void, File[]>
{
private Context context;
private AdapterForCardView adapterForCardView;
private File[] labels;
private boolean ifSaved;
private String logging=getClass().getSimpleName();
public AsyncTaskForInternalFiles(Context context, AdapterForCardView adapterForCardView,boolean ifSaved)
{
    this.context = context;
    this.adapterForCardView=adapterForCardView;
    this.ifSaved=ifSaved;
}
@Override
protected File[] doInBackground(Void... params)
{
    File internal_save = context.getDir(context.getResources().getString(R.string.directory_images), Context.MODE_PRIVATE);
    File[] files = internal_save.listFiles();
    labels=context.getDir(context.getResources().getString(R.string.directory_labels),Context.MODE_PRIVATE).listFiles();
    sortItOut(files,labels);
    return files;
}

private void sortItOut(File[] files, File[] labels)
{
    PairForSorting[] pairForSorting=new PairForSorting[files.length];
    PairForSorting[] labelsForSorting=new PairForSorting[files.length];
    for (int i=0;i<files.length;i++)
    {
        pairForSorting[i] =new PairForSorting(files[i]);
        labelsForSorting[i]=new PairForSorting(labels[i]);
        Log.d(logging,files[i].getAbsolutePath()+"=fileNames");
        Log.d(logging,labels[i].getAbsolutePath()+"=labelNames");
    }
    Arrays.sort(pairForSorting);
    Arrays.sort(labelsForSorting);
    for (int i=0;i<files.length;i++)
    {
        files[i]=pairForSorting[i].fi;
        labels[i]=labelsForSorting[i].fi;
    }
}

@Override
protected void onPostExecute(File[] files)
{
    if (files != null)
    {
        adapterForCardView.setImage_files(files);
        adapterForCardView.setStrings_of_files(labels);
        if (ifSaved)adapterForCardView.notifyItemInserted(0);
        else adapterForCardView.notifyDataSetChanged();
    }
}
}

如果您需要更多代码,请随时发表评论。

如果你想录制我也可以发送。

1 个答案:

答案 0 :(得分:0)

似乎MediaScanner在广播时将图片添加到图库时会产生问题。 不知道问题是否因为我正在使用Oxygen OS或者它是否在所有Android手机中产生相同的错误。 由于它是在从另一个Activity转换回来时刚刚将MainSctivity添加到它返回的MainActivity中,现在它不再显示。如果仅在Oxygen OS中更新,或者在我对它们进行测试时也会在正常的Androids中出现问题。