简体   繁体   中英

ListView of SeekBars

I wonder how I can make a ListView of ArrayList of SeekBar (which i can 'add' more SeekBars to the list on button click ) I am a beginner so I've been trying for hours. that is the best I could do but I don't know whether I can even do something with that :

    ArrayList<SeekBar> seekbarlist=new ArrayList<SeekBar>();
    ListView listseekbars = (ListView) findViewById(R.id.listprog);
    final ArrayAdapter<SeekBar> seekbardapter = new 
    ArrayAdapter<SeekBar>
    (this,android.R.layout.simple_list_item_1,android.R.id.text1,seekbarlist);
    listseekbars.setAdapter(seekbardapter);

i'll be more than glad to get some help :) thank you in advance.

You probably don't want to display a list of SeekBars. You want to display a list of values AS SeekBars.

You'll need to make a custom adapter, and a custom list item layout.

the custom list item is just a seekbar:

seekbar_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<SeekBar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/seekBar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:max="100">

</SeekBar>

Custom Adapter:

    private class SeekBarAdapter extends ArrayAdapter<Integer>{

    List<Integer> data;
    int resource;

    SeekBarAdapter(Context context, int layoutResource, List<Integer> data){
        super(context, layoutResource, data);
        this.data = data;
        this.resource = layoutResource;
    }

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

        Integer value = data.get(position);

        if (convertView == null){
            convertView = LayoutInflater.from(getContext()).inflate(resource, parent);
        }

        SeekBar sb = convertView.findViewById(R.id.seekBar);
        sb.setProgress(value);

        sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean wasInitiatedByUser) {
                // do what you want with the edited values here.
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        return convertView;
    }
}

then to implement:

ArrayList<Integer> yourvalues = new ArrayList<>();
ListView listseekbars = (ListView) findViewById(R.id.listprog);
SeekBarAdapter seekBarAdapter = new SeekBarAdapter(this, R.layout.seekbar_list_item, yourvalues);
listseekbars.setAdapter(seekBarAdapter);

我认为您需要做的是使用RecyclerView而不是ListView 。区别在于,您可以为每个项目创建一个布局文件,然后将这些项目添加到RecyclerView

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM