gpt4 book ai didi

java - 我可以在 RecyclerView 中将 Cursor Adapter 与 GridView 一起使用吗

转载 作者:太空宇宙 更新时间:2023-11-04 11:45:35 24 4
gpt4 key购买 nike

我正在构建一个带有描述链接的图片应用程序。会GridView RecyclerView使用光标适配器是执行此操作的合适方法,因为使用电影数据库作为电影缩略图和描述数据的源。

最佳答案

请在下面的链接中找到光标适配器实现,我还在链接下面放置了代码,它可能会对您有所帮助。

Recycler view with cursor adapter

首先,CursorAdapter 不适用于 RecyclerView。你正试图破解其中一些永远无法正常工作的东西。您不能只调用它的方法并期望它能够正确运行。查看来源。

所以首先要做的事情。我们想要使用游标。让我们分离出这个责任并创建 RecyclerViewCursorAdapter。正如它所说,CursorAdapter 用于 RecyclerView。其细节与 CursorAdapter 的工作方式几乎相同。查看其来源,看看哪些相同,哪些不同。

接下来,我们现在有了原始类 RVAdapter 来实现抽象 RecyclerViewCursorAdapter,它要求我们实现 onCreateViewHolder 和新的 onBindViewHolder,它为我们提供了一个要绑定(bind)的 Cursor 参数。这些 View 的详细信息与您原来的 View 相同,只是稍微整理了一下。

RVAdapter.java

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.Random;


public class RVAdapter extends RecyclerViewCursorAdapter<RVAdapter.ProductViewHolder>
{
private static final String TAG = RVAdapter.class.getSimpleName();
private final Context mContext;
private final Random mRandom;


public RVAdapter(Context context, String locationSetting)
{
super(null);
mContext = context;
mRandom = new Random(System.currentTimeMillis());

// Sort order: Ascending, by date.
String sortOrder = ProductContract.ProductEntry.COLUMN_DATE + " ASC";
Uri productForLocationUri = ProductContract.ProductEntry
.buildProductLocationWithStartDate(locationSetting, System.currentTimeMillis());

// Students: Uncomment the next lines to display what what you stored in the bulkInsert
Cursor cursor = mContext.getContentResolver()
.query(productForLocationUri, null, null, null, sortOrder);

swapCursor(cursor);
}

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

@Override
protected void onBindViewHolder(ProductViewHolder holder, Cursor cursor)
{
String imagePath = cursor.getString(ShopFragment.COL_PRODUCT_IMAGE);
String price = cursor.getString(ShopFragment.COL_PRODUCT_PRICE);

holder.productPrice.setText("US $" + price);

int height = mRandom.nextInt(50) + 500;

//Download image using picasso library
Picasso.with(mContext)
.load(imagePath)
.resize(500, height)
.error(R.drawable.placeholder)
.placeholder(R.drawable.placeholder)
.into(holder.productPhoto);
}


public static class ProductViewHolder extends RecyclerView.ViewHolder
{
CardView cv;
TextView productPrice;
ImageView productPhoto;

ProductViewHolder(View itemView)
{
super(itemView);
cv = (CardView) itemView.findViewById(R.id.cv);
productPrice = (TextView) itemView.findViewById(R.id.product_price);
productPhoto = (ImageView) itemView.findViewById(R.id.product_photo);
}
}
}

RecyclerViewCursorAdapter.java

import android.database.Cursor;
import android.database.DataSetObserver;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;


/**
* RecyclerView CursorAdapter
* <p>
* Created by Simon on 28/02/2016.
*/
public abstract class RecyclerViewCursorAdapter<VH extends RecyclerView.ViewHolder> extends
RecyclerView.Adapter<VH>
{
private Cursor mCursor;
private boolean mDataValid;
private int mRowIDColumn;


public RecyclerViewCursorAdapter(Cursor cursor)
{
setHasStableIds(true);
swapCursor(cursor);
}

public abstract VH onCreateViewHolder(ViewGroup parent, int viewType);

protected abstract void onBindViewHolder(VH holder, Cursor cursor);

@Override
public void onBindViewHolder(VH holder, int position)
{
if(!mDataValid){
throw new IllegalStateException("this should only be called when the cursor is valid");
}
if(!mCursor.moveToPosition(position)){
throw new IllegalStateException("couldn't move cursor to position " + position);
}
onBindViewHolder(holder, mCursor);
}

@Override
public long getItemId(int position)
{
if(mDataValid && mCursor != null && mCursor.moveToPosition(position)){
return mCursor.getLong(mRowIDColumn);
}
return RecyclerView.NO_ID;
}

@Override
public int getItemCount()
{
if(mDataValid && mCursor != null){
return mCursor.getCount();
}
else{
return 0;
}
}

protected Cursor getCursor()
{
return mCursor;
}

public void changeCursor(Cursor cursor)
{
Cursor old = swapCursor(cursor);
if(old != null){
old.close();
}
}

public Cursor swapCursor(Cursor newCursor)
{
if(newCursor == mCursor){
return null;
}
Cursor oldCursor = mCursor;
if(oldCursor != null){
if(mDataSetObserver != null){
oldCursor.unregisterDataSetObserver(mDataSetObserver);
}
}
mCursor = newCursor;
if(newCursor != null){
if(mDataSetObserver != null){
newCursor.registerDataSetObserver(mDataSetObserver);
}
mRowIDColumn = newCursor.getColumnIndexOrThrow("_id");
mDataValid = true;
notifyDataSetChanged();
}
else{
mRowIDColumn = -1;
mDataValid = false;
notifyDataSetChanged();
}
return oldCursor;
}


private DataSetObserver mDataSetObserver = new DataSetObserver()
{
@Override
public void onChanged()
{
mDataValid = true;
notifyDataSetChanged();
}

@Override
public void onInvalidated()
{
mDataValid = false;
notifyDataSetChanged();
}
};
}

关于java - 我可以在 RecyclerView 中将 Cursor Adapter 与 GridView 一起使用吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42381925/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com