滚动进度条的 ListView

发布于 2024-11-16 20:22:30 字数 1813 浏览 9 评论 0原文

我正在开发一个使用 ListView 的 Android 应用程序,其中每一行都由一个文本视图和一个进度栏组成。除非用户必须滚动长列表,否则一切都会顺利进行。

ProgressBar 开始获取当前在屏幕上不可见的其他 ProgressBar 的进度。

我知道这是由 GetView 的实现引起的常见问题,但我想知道对 ProgressBars 采取的最佳行动方案是什么。

GetView:

public View getView(int position, View convertView, ViewGroup parent){

        View row = convertView;
        ViewWrapper wrapper;
        ProgressBar myProgressBar;
        int initProgress = myData.get(position).getProgress();

        if (row == null){
            LayoutInflater inflater = context.getLayoutInflater();
            row = inflater.inflate(R.layout.row, null); 

            wrapper = new ViewWrapper(row);
            row.setTag(wrapper);    
        }

        else{
            wrapper = (ViewWrapper)row.getTag();

        }

        RowModel model = getModel(position);        
        wrapper.getPid().setText(model.toString());
        myProgressBar = wrapper.getProgressBar();
        myProgressBar.setProgress(initProgress);
        myProgressBar.setMax(100);

        myProgressBar.setTag(new Integer(position));

        return row;
    }   

ViewWrapper:

public class ViewWrapper {
    View base;
    TextView pid = null;
    ProgressBar pb= null;

    ViewWrapper(View base){
        this.base = base;
    }

    TextView getPid(){
        if(pid == null){
            pid = (TextView)base.findViewById(R.id.pid);
        }

        return(pid);
    }

    ProgressBar getProgressBar(){
        if(pb== null){
            pb= (ProgressBar)base.findViewById(R.id.progressbar);
        }

        return(pb);
    }
}

问题似乎与以下内容有关:

myProgressBar = wrapper.getProgressBar();

因为 ProgressBar 开始获取回收的 ProgressBar 的行为。但是,我希望它有自己的行为。

缓解这种情况最好的方法是什么?谢谢。

I'm working on an Android app that utilizes a ListView, in which each row is comprised of a text view and a progress bar. Things work smoothly unless the user has to scroll through a long list.

ProgressBars start taking on the progress of other ProgressBars not currently visible on the screen.

I understand that this is a common issue that stems from the implementation of GetView, but I'm wondering what the best course of action is to take with ProgressBars.

GetView:

public View getView(int position, View convertView, ViewGroup parent){

        View row = convertView;
        ViewWrapper wrapper;
        ProgressBar myProgressBar;
        int initProgress = myData.get(position).getProgress();

        if (row == null){
            LayoutInflater inflater = context.getLayoutInflater();
            row = inflater.inflate(R.layout.row, null); 

            wrapper = new ViewWrapper(row);
            row.setTag(wrapper);    
        }

        else{
            wrapper = (ViewWrapper)row.getTag();

        }

        RowModel model = getModel(position);        
        wrapper.getPid().setText(model.toString());
        myProgressBar = wrapper.getProgressBar();
        myProgressBar.setProgress(initProgress);
        myProgressBar.setMax(100);

        myProgressBar.setTag(new Integer(position));

        return row;
    }   

ViewWrapper:

public class ViewWrapper {
    View base;
    TextView pid = null;
    ProgressBar pb= null;

    ViewWrapper(View base){
        this.base = base;
    }

    TextView getPid(){
        if(pid == null){
            pid = (TextView)base.findViewById(R.id.pid);
        }

        return(pid);
    }

    ProgressBar getProgressBar(){
        if(pb== null){
            pb= (ProgressBar)base.findViewById(R.id.progressbar);
        }

        return(pb);
    }
}

It seems that the issue is related to:

myProgressBar = wrapper.getProgressBar();

because that ProgressBar starts getting the behavior of a recycled ProgressBar. However, I want it to have its own behavior.

What's the best way to alleviate this? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放手` 2024-11-23 20:22:30

您可能需要每次都膨胀布局,而不是重新使用传入的convertView。除非您有很多行,否则这应该不是问题。

You may need to inflate the layout each time and not re-use the convertView that's passed in. This shouldn't be a problem unless you have A LOT of rows.

两个我 2024-11-23 20:22:30

我必须实现类似的功能,这就是我所做的。 getview 外部实现了以下

  • OnTouchListener - 侦听搜索栏触摸事件
  • OnKeyListener - 侦听搜索栏的 dpad 和 trakball 事件

我为 getView 中的搜索栏设置了这些侦听器

我在 监听器被调用,我会找到seekbars父级,然后从父级到textview执行findviewbyid

现在我有了必须更新的文本视图和搜索栏。我需要做的就是设置数组中的文本。

这是一些可以帮助您的代码。

私有 OnTouchListener touchListener = new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
            View parent = (View) v.getParent();
            TextView textView = (TextView) parent
                    .findViewById(R.id.id_grade_tooltip);
            if (textView == null) {
            } else {
                SeekBar seekBar = (SeekBar) v;
                textView.setText(String.valueOf(seekBar.getProgress())
                        + "%");
            }
            return false;
    }
};

I had to implement a similar feature , here is what I did . I implemented the following outside getview

  • OnTouchListener - to listen to seekbar touch events
  • OnKeyListener - to listen to dpad and trakball event for the seekbar

I set these listeners for the seekbars from getView

Whenever the listeners were called , I would find the seekbars parent , then do the findviewbyid from the parent to the textview.

So now I have the textview which I have to update, and the seekbar. All I need to do was set the text from the array.

here is some code to help you.

private OnTouchListener touchListener = new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
            View parent = (View) v.getParent();
            TextView textView = (TextView) parent
                    .findViewById(R.id.id_grade_tooltip);
            if (textView == null) {
            } else {
                SeekBar seekBar = (SeekBar) v;
                textView.setText(String.valueOf(seekBar.getProgress())
                        + "%");
            }
            return false;
    }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文