动态创建搜索栏并为其提供听众,但它无法正常工作

时间:2014-12-05 15:20:40

标签: android android-inflate android-seekbar

我正在动态扩充搜索栏,但是如果我按下第一个搜索栏,则监听器仅更改上一个搜索栏中的值。

for (int i = 0; i < controlList.size(); i++) {
            view1 = seekbar_inflate.inflate(R.layout.mysxm_control_coloumn, null, false);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            FrameLayout frame = (FrameLayout) view1
                    .findViewById(R.id.sliderLayout);
            vertical_seekbar=new VerticalSeekBar(MyApplication.getAppContext());
            vertical_seekbar.setId(i);
            final int id = vertical_seekbar.getId();
            vertical_seekbar.setProgressDrawable(getResources().getDrawable(R.drawable.sxm_selector_column));
            frame.addView(vertical_seekbar, params);
            vertical_seekbar=(VerticalSeekBar)view1.findViewById(id);
            equalizer_view.addView(view1);
            vertical_seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

                @Override
                public void onProgressChanged(SeekBar seekBar,
                        int progress, boolean arg2) {


                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {

                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {


                }
            });
        }

如何为每个搜索栏提供听众?请提出一些答案

2 个答案:

答案 0 :(得分:1)

将vertical_seekbar和view1设为局部变量。你已将它们作为实例,因此它们只保留最后一次迭代的值,这就是为什么你的最后一个搜索栏只能工作。

for (int i = 0; i < controlList.size(); i++) {
            View view1 = seekbar_inflate.inflate(R.layout.mysxm_control_coloumn, null, false);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            FrameLayout frame = (FrameLayout) view1
                    .findViewById(R.id.sliderLayout);
            VerticalSeekBar vertical_seekbar=new VerticalSeekBar(MyApplication.getAppContext());
            vertical_seekbar.setId(i);
            final int id = vertical_seekbar.getId();
            vertical_seekbar.setProgressDrawable(getResources().getDrawable(R.drawable.sxm_selector_column));
            frame.addView(vertical_seekbar, params);
            vertical_seekbar=(VerticalSeekBar)view1.findViewById(id);
            equalizer_view.addView(view1);
            vertical_seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

                @Override
                public void onProgressChanged(SeekBar seekBar,
                        int progress, boolean arg2) {


                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {

                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {


                }
            });
        }

答案 1 :(得分:1)

我的代码只是略微修改,在这里我已经实现了android每个基于id工作的小部件。在这个代码中你可以区分seekbar变量如vertical_seekbar和vertical_seekbar1对我来说这个解决方案。这个解决方案很奇怪我

for (int i = 0; i < controlList.size(); i++) {
                view1 = seekbar_inflate.inflate(R.layout.mysxm_control_coloumn, null, false);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);
                FrameLayout frame = (FrameLayout) view1
                        .findViewById(R.id.sliderLayout);
                vertical_seekbar=new VerticalSeekBar(MyApplication.getAppContext());
                vertical_seekbar.setId(i);
                final int id = vertical_seekbar.getId();
                vertical_seekbar.setProgressDrawable(getResources().getDrawable(R.drawable.sxm_selector_column));
                frame.addView(vertical_seekbar, params);
                vertical_seekbar1=(VerticalSeekBar)view1.findViewById(id);
                equalizer_view.addView(view1);
                vertical_seekbar1.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

                    @Override
                    public void onProgressChanged(SeekBar seekBar,
                            int progress, boolean arg2) {


                    }

                    @Override
                    public void onStartTrackingTouch(SeekBar seekBar) {

                    }

                    @Override
                    public void onStopTrackingTouch(SeekBar seekBar) {


                    }
                });
            }