为什么不工作notifyItemChanged(Why does not work notifyItemChanged)

编程入门 行业动态 更新时间:2024-10-08 04:29:57
为什么不工作notifyItemChanged(Why does not work notifyItemChanged)

我知道这可能是一个愚蠢的问题,但我找不到答案。 我问你的帮助。

我有一个使用LinearLayoutManager的RecyclerView和一个自定义的RecyclerView.Adapter。

我的数据类:

public class Img extends ArrayList<Img> { int id = 0; String filepath = ""; boolean checked = false; int progress = 0; ... getters and setters... }

活动代码:

File folder = new File("My path"); File[] files = folder.listFiles(); int id = 0; for (File file : files) { if (!file.isDirectory()) { Img img = new Img(); img.setId(id); img.setFilepath(file.toString()); imgs.add(img); ++id; } } mAdapter = new GalleryAdapter(getApplicationContext(), imgs); RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getApplicationContext(), 2); recyclerView.setLayoutManager(mLayoutManager); recyclerView.setItemAnimator(new DefaultItemAnimator()); recyclerView.setAdapter(mAdapter);

和适配器:

@Override public void onBindViewHolder(final MyViewHolder holder, final int position) { Img item = imgs.get(position); holder.txt.setText(String.valueOf(item.getProgress())); holder.thumbnail.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { File source = new File(imgs.get(position).getFilepath()); String strFileName = source.getName(); File destination = new File("My path" + "/" + strFileName); try { InputStream input = null; OutputStream output = null; long lenghtOfFile = source.length(); int count; try { input = new FileInputStream(source); output = new FileOutputStream(destination); byte[] data = new byte[1024]; long total = 0; int xc2 = 0; while ((count = input.read(data)) != -1) { total += count; int xc = (int) ((total * 100) / lenghtOfFile); output.write(data, 0, count); imgs.get(position).setProgress(xc); notifyItemChanged(position); try { Thread.sleep(20); } catch (InterruptedException e) { e.printStackTrace(); } } } finally { output.flush(); output.close(); input.close(); } } catch (IOException e) { e.printStackTrace(); } } });

为什么notifyItemChanged(position)只在操作完成后才被触发并提交给TextView(txt)只有100的值?

UPD:此代码不起作用

@Override public MyViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) { final View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.gallery_thumbnail, parent, false); final MyViewHolder holder = new MyViewHolder(itemView); itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { final int position = holder.getAdapterPosition(); if (position != RecyclerView.NO_POSITION) { File source = new File(imgs.get(position).getFilepath()); String strFileName = source.getName(); File destination = new File("My path" + "/" + strFileName); try { InputStream input = null; OutputStream output = null; long lenghtOfFile = source.length(); int count; try { input = new FileInputStream(source); output = new FileOutputStream(destination); byte[] data = new byte[1024]; long total = 0; int xc2 = 0; while ((count = input.read(data)) != -1) { total += count; int xc = (int) ((total * 100) / lenghtOfFile); output.write(data, 0, count); if (xc2!=xc){ //holder.progress_bar.setProgress(xc); imgs.get(position).setProgress(xc); notifyItemChanged(position); try { Thread.sleep(40); } catch (InterruptedException e) { e.printStackTrace(); } } xc2 = xc; } } finally { output.flush(); output.close(); input.close(); } } catch (IOException e) { e.printStackTrace(); } } } }); return holder; }

UPD2:此代码不起作用

@Override public MyViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) { View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.gallery_thumbnail, parent, false); final MyViewHolder holder = new MyViewHolder(itemView); itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { final int position = holder.getAdapterPosition(); if (position != RecyclerView.NO_POSITION) { for (int i=1; i<=100; ++i) { imgs.get(position).setProgress(i); notifyItemChanged(position); try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } } } } }); //return new MyViewHolder(itemView); return holder; }

I understand that it is possible a silly question but I can not find the answer to it. I ask your help.

I have a RecyclerView using a LinearLayoutManager, and a custom RecyclerView.Adapter.

My data class:

public class Img extends ArrayList<Img> { int id = 0; String filepath = ""; boolean checked = false; int progress = 0; ... getters and setters... }

Code in activity:

File folder = new File("My path"); File[] files = folder.listFiles(); int id = 0; for (File file : files) { if (!file.isDirectory()) { Img img = new Img(); img.setId(id); img.setFilepath(file.toString()); imgs.add(img); ++id; } } mAdapter = new GalleryAdapter(getApplicationContext(), imgs); RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getApplicationContext(), 2); recyclerView.setLayoutManager(mLayoutManager); recyclerView.setItemAnimator(new DefaultItemAnimator()); recyclerView.setAdapter(mAdapter);

and adapter:

@Override public void onBindViewHolder(final MyViewHolder holder, final int position) { Img item = imgs.get(position); holder.txt.setText(String.valueOf(item.getProgress())); holder.thumbnail.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { File source = new File(imgs.get(position).getFilepath()); String strFileName = source.getName(); File destination = new File("My path" + "/" + strFileName); try { InputStream input = null; OutputStream output = null; long lenghtOfFile = source.length(); int count; try { input = new FileInputStream(source); output = new FileOutputStream(destination); byte[] data = new byte[1024]; long total = 0; int xc2 = 0; while ((count = input.read(data)) != -1) { total += count; int xc = (int) ((total * 100) / lenghtOfFile); output.write(data, 0, count); imgs.get(position).setProgress(xc); notifyItemChanged(position); try { Thread.sleep(20); } catch (InterruptedException e) { e.printStackTrace(); } } } finally { output.flush(); output.close(); input.close(); } } catch (IOException e) { e.printStackTrace(); } } });

Why notifyItemChanged (position) is triggered only after the operation is completed and submitted to the TextView (txt) only the value of 100?

UPD: This code does not work

@Override public MyViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) { final View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.gallery_thumbnail, parent, false); final MyViewHolder holder = new MyViewHolder(itemView); itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { final int position = holder.getAdapterPosition(); if (position != RecyclerView.NO_POSITION) { File source = new File(imgs.get(position).getFilepath()); String strFileName = source.getName(); File destination = new File("My path" + "/" + strFileName); try { InputStream input = null; OutputStream output = null; long lenghtOfFile = source.length(); int count; try { input = new FileInputStream(source); output = new FileOutputStream(destination); byte[] data = new byte[1024]; long total = 0; int xc2 = 0; while ((count = input.read(data)) != -1) { total += count; int xc = (int) ((total * 100) / lenghtOfFile); output.write(data, 0, count); if (xc2!=xc){ //holder.progress_bar.setProgress(xc); imgs.get(position).setProgress(xc); notifyItemChanged(position); try { Thread.sleep(40); } catch (InterruptedException e) { e.printStackTrace(); } } xc2 = xc; } } finally { output.flush(); output.close(); input.close(); } } catch (IOException e) { e.printStackTrace(); } } } }); return holder; }

UPD2: This code does not work

@Override public MyViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) { View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.gallery_thumbnail, parent, false); final MyViewHolder holder = new MyViewHolder(itemView); itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { final int position = holder.getAdapterPosition(); if (position != RecyclerView.NO_POSITION) { for (int i=1; i<=100; ++i) { imgs.get(position).setProgress(i); notifyItemChanged(position); try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } } } } }); //return new MyViewHolder(itemView); return holder; }

最满意答案

基本上你在UI线程上加载你的文件,所以你每次调用notifyItemChanged()都会向Looper添加一个任务,但只有当你完成加载时,Looper才能完成任务,你只能看到100。

我会在不同的线程上运行负载,如下所示:

new View.OnClickListener() { @Override public void onClick(View view) { File source = new File(imgs.get(position).getFilepath()); String strFileName = source.getName(); File destination = new File("My path" + "/" + strFileName); **> Thread t1 = new Thread(new Runnable(){ public void run(){ <** try { InputStream input = null; OutputStream output = null; long lenghtOfFile = source.length(); int count; try { input = new FileInputStream(source); output = new FileOutputStream(destination); byte[] data = new byte[1024]; long total = 0; int xc2 = 0; while ((count = input.read(data)) != -1) { total += count; int xc = (int) ((total * 100) / lenghtOfFile); output.write(data, 0, count); imgs.get(position).setProgress(xc); notifyItemChanged(position); try { Thread.sleep(20); } catch (InterruptedException e) { e.printStackTrace(); } } } finally { output.flush(); output.close(); input.close(); } } catch (IOException e) { e.printStackTrace(); } } t1.start(); } }

多数民众赞成我的猜测,祝你好运!

Basically your'e doing your'e file loading on the UI thread, so everytime you call to notifyItemChanged() you add a task to the Looper but the Looper gets to that task only when you finish the loading therefor you only see the 100.

I would run the load on a different thread like so:

new View.OnClickListener() { @Override public void onClick(View view) { File source = new File(imgs.get(position).getFilepath()); String strFileName = source.getName(); File destination = new File("My path" + "/" + strFileName); **> Thread t1 = new Thread(new Runnable(){ public void run(){ <** try { InputStream input = null; OutputStream output = null; long lenghtOfFile = source.length(); int count; try { input = new FileInputStream(source); output = new FileOutputStream(destination); byte[] data = new byte[1024]; long total = 0; int xc2 = 0; while ((count = input.read(data)) != -1) { total += count; int xc = (int) ((total * 100) / lenghtOfFile); output.write(data, 0, count); imgs.get(position).setProgress(xc); notifyItemChanged(position); try { Thread.sleep(20); } catch (InterruptedException e) { e.printStackTrace(); } } } finally { output.flush(); output.close(); input.close(); } } catch (IOException e) { e.printStackTrace(); } } t1.start(); } }

Thats my guess, good luck!

更多推荐

本文发布于:2023-07-09 10:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1085525.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:工作   notifyItemChanged   work

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!