如何获得RecyclerView中item的视图或者完整的高度

如何获得RecyclerView中item的视图或者完整的高度,第1张

思路是:因为ViewHolder我们可以拿到每个Item的根布局,所以如果我们为根布局设置单独的OnClick监听并将其开放给Adapter,那不就可以在组装RecyclerView时就能够设置ItemClickListener,只不过这个Listener不是设置到RecyclerView上而是设置到Adapter。

我们首先看ViewHolder的代码:

public class MyViewHolder extends ViewHolder implements OnClickListener,OnLongClickListener{

public ImageView iv;

public TextView tv;

private MyItemClickListener mListener;

private MyItemLongClickListener mLongClickListener;

public MyViewHolder(View rootView,MyItemClickListener listener,MyItemLongClickListener longClickListener) {

super(rootView);

iv = (ImageView)rootViewfindViewById(Riditem_iv);

tv = (TextView)rootViewfindViewById(Riditem_tv);

thismListener = listener;

thismLongClickListener = longClickListener;

rootViewsetOnClickListener(this);

rootViewsetOnLongClickListener(this);

}

/

点击监听

/

@Override

public void onClick(View v) {

if(mListener != null){

mListeneronItemClick(v,getPosition());

}

}

/

长按监听

/

@Override

public boolean onLongClick(View arg0) {

if(mLongClickListener != null){

mLongClickListeneronItemLongClick(arg0, getPosition());

}

return true;

}

}</span>

因为在构造ViewHolder时,rootView将作为一个必传参数传递进来,所以我们只需要拿到rootView并给其绑定点击监听事件即可。

下面要考虑的就是怎样把listener传递进来。Demo中设定了监听点击事件的Interface:MyItemClickListener:

1

2

3

public interface MyItemClickListener {

public void onItemClick(View view,int postion);

}

MyItemClickListener模仿ListView的OnItemClickListener,开放了view和position两个参数,这对习惯使用ListView的开发者们使用起来更得心应手。从ViewHolder的代码中可以看到,执行onClick方法时会调用getPosition()将当前Item的位置回调给listener。getPosition()是ViewHolder的内置方法,可直接使用。

拖拽的过程就不说了,这里主要说一下如何在前端获取到的相关信息。

html5里有一个fileReader的全局变量,用来读取本地文件,比如txt,img等,下面是一个简单的代码。

function checkFile(files){

var file = files[0];

var reader = new FileReader();

// show表示<div id='show'></div>,用来展示预览的

if(!/image\/\w+/test(filetype)){

            showinnerHTML = "请确保文件为图像类型";

            return false;

        }

        // onload是异步 *** 作

readeronload = function(e){

showinnerHTML = '<img src="'+etargetresult+'" alt="img">';

}

readerreadAsDataURL(file);

}

这样就能够在不上传到服务器的前提下预览。当然,这个问题的重点是获取的原始尺寸,html5里还提供了两个变量来获取:naturalWidth和naturalHeight,这两个分别来获取的原始宽度和原始高度

在上面的例子中,就能通过下面的方式获取到:

var width = etargetnaturalWidth;

var height = etargetnaturalHeight;

还有一种情况就是,如果已经存在页面里的,怎么获取到原始尺寸呢,可以这样:

var img = documentgetElementsByTagName('img')[0]; // 获取到

var width = imgnaturalWidth;

var height = imgnaturalHeight;

很多人在android开发中都遇到了生成bitmap时候内存溢出,也就是out of memory(OOM)的问题,网上对这样的问题的的解决说法不一。笔者作为一个初级开发者,在这里向大家提供一种比较实用,比较易于理解的方法,这种方法不如一些高级开发者提出的方案来的深刻,但是也能帮助大家有效地解决问题。

废话不多说了,直接上代码。

Java代码

BitmapFactoryOptions opt = new BitmapFactoryOptions();

//这个isjustdecodebounds很重要

optinJustDecodeBounds = true;

bm = BitmapFactorydecodeFile(absolutePath, opt);

//获取到这个的原始宽度和高度

int picWidth = optoutWidth;

int picHeight = optoutHeight;

//获取屏的宽度和高度

WindowManager windowManager = getWindowManager();

Display display = windowManagergetDefaultDisplay();

int screenWidth = displaygetWidth();

int screenHeight = displaygetHeight();

//isSampleSize是表示对的缩放程度,比如值为2的宽度和高度都变为以前的1/2

optinSampleSize = 1;

//根据屏的大小和大小计算出缩放比例

if(picWidth > picHeight){

if(picWidth > screenWidth)

optinSampleSize = picWidth/screenWidth;

}

else{

if(picHeight > screenHeight)

optinSampleSize = picHeight/screenHeight;

}

//这次再真正地生成一个有像素的,经过缩放了的bitmap

optinJustDecodeBounds = false;

bm = BitmapFactorydecodeFile(absolutePath, opt);

//用imageview显示出bitmap

ivsetImageBitmap(bm);

BitmapFactoryOptions opt = new BitmapFactoryOptions();

//这个isjustdecodebounds很重要

optinJustDecodeBounds = true;

bm = BitmapFactorydecodeFile(absolutePath, opt);

//获取到这个的原始宽度和高度

int picWidth = optoutWidth;

int picHeight = optoutHeight;

//获取屏的宽度和高度

WindowManager windowManager = getWindowManager();

Display display = windowManagergetDefaultDisplay();

int screenWidth = displaygetWidth();

int screenHeight = displaygetHeight();

//isSampleSize是表示对的缩放程度,比如值为2的宽度和高度都变为以前的1/2

optinSampleSize = 1;

//根据屏的大小和大小计算出缩放比例

if(picWidth > picHeight){

if(picWidth > screenWidth)

optinSampleSize = picWidth/screenWidth;

}

else{

if(picHeight > screenHeight)

optinSampleSize = picHeight/screenHeight;

}

//这次再真正地生成一个有像素的,经过缩放了的bitmap

optinJustDecodeBounds = false;

bm = BitmapFactorydecodeFile(absolutePath, opt);

//用imageview显示出bitmap

ivsetImageBitmap(bm);

inJustDecodeBounds 的介绍

public boolean inJustDecodeBounds

Since: API Level 1

If set to true, the decoder will return null (no bitmap), but the out fields will still be set, allowing the caller to query the bitmap without having to allocate the memory for its pixels

就是说,如果设置inJustDecodeBounds为true,仍可以获取到bitmap信息,但完全不用分配内存,因为没有获取像素,所以我们可以利用得到的Bitmap的大小,重新压缩,然后在内存中生成一个更小的Bitmap,这样即便是一个4MB的JPG,我们也可以随心所欲地把他压缩到任意大小,从而节省了内存,看到这里是不是恍然大悟,牛b了牛b了!

下面这个参数就是跟压缩有关的部分,很容易懂,不多解释了:

inSampleSize 的介绍

public int inSampleSize

Since: API Level 1

If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory The sample size is the number of pixels in either dimension that correspond to a single pixel in the decoded bitmap For example, inSampleSize == 4 returns an image that is 1/4 the width/height of the original, and 1/16 the number of pixels Any value

先说正确的获取屏幕高度方式

使用以下的方法去获取获取应用的屏幕高度

此处通过 getMetrics() 获取的高度其实是去除了虚拟按键后的高度。所以和手机的实际屏幕高度可能有差异

getReaMetrics() 则是真正原始的屏幕尺寸

这种方法一般用于tableview,制作类似于微博浏览,空间展示动态的功能。表的高度根据以及字符串字数确定。

通过文字展示的最大宽度以及文字字体大小确定文字展示的高度

[objc] view plain copy

- (CGRect)getHeightOfText:(NSString )text width:(CGFloat)width font:(UIFont )font{

/

width:设定的字符串展示的宽度

font :字符的字体大小

/

CGRect rect = [text boundingRectWithSize:CGSizeMake(width, 0) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:font} context:nil];

/

字符串对应高度

float aHeifht = rectsizeheight;

字符串对应宽度

float aWidth = rectsizewidth;

/

return rect;

}

根据需要展示的高度或者文字高度自适应表行高

[objc] view plain copy

- (CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath{

if (文字内容){

NSString text = [NSString stringWithFormat:@"\t%@",需要展示的文字];

CGRect rect = [self getHeightOfText:text width:300 font:[UIFont systemFontOfSize:14]];

return rectsizeheight+10;

}else//展示

{

return [[ objectForKey:@"height"] integerValue]+10;//获取服务器中高度,为了美观都比原始高多10

}

}

设置cell内容

[objc] view plain copy

- (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath )indexPath{

NewsDetailCell cell = [tableView dequeueReusableCellWithIdentifier:@"data"];

NSDictionary content = [_dataArray objectAtIndex:indexPathrow];

if ([[content objectForKey:@"data_type"] intValue] == 1) {

//文本

NSString text = [NSString stringWithFormat:@"\t%@",[content objectForKey:@"content"]];

celltxtLabeltext = text;

CGRect rect = [CommonTool getHeightOfText:text width:300 font:[UIFont systemFontOfSize:14]];

celltxtLabelframe = CGRectMake(10, 5, rectsizewidth, rectsizeheight);

}else{

//

NSString urlString = [NSString stringWithFormat:@"%@%@",SERVER_ADDRESS,[[content objectForKey:@"image"] objectForKey:@"source"]];

[cellnewsImageView setImageWithURL:[NSURL URLWithString:urlString]];

cellnewsImageViewframe = CGRectMake(10, 5, 300, [[[content objectForKey:@"image"] objectForKey:@"height"] integerValue]);

}

return cell;

}

以上就是关于如何获得RecyclerView中item的视图或者完整的高度全部的内容,包括:如何获得RecyclerView中item的视图或者完整的高度、html5拖拽图片上传,怎么获得图片原始尺寸、如何解决bitmap 内存溢出out of memory的问题等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9781964.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-01
下一篇2023-05-01

发表评论

登录后才能评论

评论列表(0条)

    保存