我的应用程序需要按顺序在列表循环视图中显示大量图像,每个项目视图都是一个图像视图,而项目视图的高度应该与所显示图像的高度完全相同,并且图像需要从网络加载。在加载图像后似乎无法更改项目视图的高度,发生NullPoint异常。有没有人遇到同样的问题?
以下是代码:
c = db.rawQuery("SELECT * FROM Your_tabename", null);
int i = c.getCount();
LogCat :
recyclerView = (RecyclerView) findViewById(R.id.recycleView);
recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
recyclerView.setItemAnimator(new DefaultItemAnimator());
RecycleviewAdapter mRecycleviewAdapter = new RecycleviewAdapter(chData,chName,this);
recyclerView.setAdapter(mRecycleviewAdapter);
public class RecycleviewAdapter extends RecyclerView.Adapter<RecycleviewAdapter.MangaImageItemHolder> {
ChJs chJsonData;
String chName;
Context mContext;
int[] screenSize;
public RecycleviewAdapter(ChJs chJsonData, String chName, Context context){
this.chJsonData = chJsonData;
this.mContext = context;
this.chName = chName;
screenSize = Commons.getScreenSize(context);
}
@Override
public int getItemCount() {
if(chJsonData!=null)
return chJsonData.countOfPages();
return 0;
}
@Override
public MangaImageItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
ImageView mImageView = new ImageView(mContext);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
mImageView.setLayoutParams(params);
return new MangaImageItemHolder(mImageView, this);
}
@Override
public void onBindViewHolder(final MangaImageItemHolder holder, int position) {
String imageName = ImangaFileManager.imageName(chName,
String.valueOf(position));
final String localImagePath = ImangaFileManager.localImagePath(MangaReader.website,
MangaReader.mangaName, imageName, mContext);
ExecutorService executorService = MangaReader.Executor4Local();
executorService.execute(new Runnable() {
@Override
public void run() {
BitmapFactory.Options op = new BitmapFactory.Options();
op.inPreferredConfig = Bitmap.Config.RGB_565;
final Bitmap bm = BitmapFactory.decodeFile(localImagePath, op);
((SherlockActivity)mContext).runOnUiThread(new Runnable() {
@Override
public void run() {
int height = screenSize[0]*bm.getHeight()/bm.getWidth();
RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT,height);
holder.itemView.setLayoutParams(params);
holder.mImageView.setImageBitmap(bm);
}
});
}
});
}
public static class MangaImageItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private ImageView mImageView;
public MangaImageItemHolder(View itemView, RecycleviewAdapter adapter) {
super(itemView);
itemView.setOnClickListener(this);
mImageView = (ImageView) itemView;
}
@Override
public void onClick(View v) {
}
}
}
答案 0 :(得分:2)
您可以通过在适配器中应用元素的屏幕比例高度来实现。返回值的函数:
public static int containerHeight(MainActivity ba) {
DisplayMetrics dm = new DisplayMetrics();
ba.getWindowManager().getDefaultDisplay().getMetrics(dm);
//get predefined value
double ratio = Static.PIC_RATIO_VALUE; //ex 4.0
return (int) (dm.heightPixels / ratio);
}
在适配器(onBindViewHolder模块)中,您需要具有以下内容:
//set height in proportion to screen size
int proportionalHeight UIUtil.containerHeight((MainActivity)mCntx);
TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, proportionalHeight); // (width, height)
holder.container.setLayoutParams(params);
holder.container 是要应用高度逻辑的元素的根布局。
请参阅此blog post,其中详细了解如何实现此目标。
答案 1 :(得分:0)
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
holder.imageviewlogo.post(new Runnable() {
@Override
public void run() {
holder.imageviewlogo.getLayoutParams().height = holder.linearlayoutDetails.getHeight();
holder.imageviewlogo.getLayoutParams().width = holder.linearlayoutDetails.getHeight();
holder.imageviewlogo.postInvalidate();
}
});
}