SeekBar in a ListActivity using an ArrayAdapter

半腔热情 提交于 2019-12-08 09:06:56

问题


I'm not tied to using an ArrayAdapter but, what I want is a list where each item contains an ImageView, a TextView, and a SeekBar to where I can update the images/texts as well as receive callbacks from each slider (obviously, I need to be able to add and delete rows dynamically).

I've no trouble creating a list that contains these items that is static -> i.e. defined in an xml layout for each row of my list. However, with this, I'm unable to "overwrite the 'onStartTrackingTouch()', 'onProgressChanged()', etc. callback methods". I also can create a list that "contains only seekbars" and I can get the callbacks. The problem is when I bring in the LayoutInflater and then try to add my seekbars to the layout.

After several days of mucking around with it and searching on the internet, I still can't see a solution to have a listactivity that uses a layoutinflator to create composite list rows consisting of an imageview, textview, and seekbar that I can communicate properly with.

Does anyone have a solution? Code?

What I've got so far goes something like:

public class MyListActivity extends ListActivity
{   
    private class MyView {
        public ImageView myImageView = null;
        public TextView myTextView = null;
        public SeekBar mySeekBar = null;        
    }

  private class MyAdapter extends ArrayAdapter<MyView> {                
    private ArrayList<MyView> arrayList;
    private MyListActivity theContext = null;

    public MyAdapter(MyListActivity context, 
                  int textViewResourceId,
                  ArrayList<MyView> arrayListPassedIn) {
        super(context, textViewResourceId, arrayListPassedIn);
        this.volumeControls = arrayListPassedIn;
        theContext = context;
    } 

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;

        if (v == null) {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);                
            v = vi.inflate(R.layout.dont_think_i_should_need_to_use_a_resource_here_cuz_it_should_be_generated_or_dynamic, parent, false);
        }

        arrayList.get(position).myImageView = new ImageView(theContext);
        arrayList.get(position).myImageView.setBackgroundResource(R.drawable.generic_image);

        if(position == 0)
        {   
            arrayList.get(position).mySeekBar = new SeekBar(theContext);        
            arrayList.get(position).mySeekBar.setMax(100);
            arrayList.get(position).mySeekBar.setEnabled(true);
            arrayList.get(position).mySeekBar.setOnSeekBarChangeListener(
                new SeekBar.OnSeekBarChangeListener() {                         
                    @Override
                    public void onStopTrackingTouch(SeekBar seekBar) {
                        Log.d("test", "0th seekbar touched");                
                    }

                    @Override
                    public void onStartTrackingTouch(SeekBar seekBar) {
                        Log.d("test", "0th seekbar touch lifted");
                    }

                    @Override
                    public void onProgressChanged(SeekBar seekBar, int arg1, boolean fromUser) {
                        Log.d("test", "0th seekbar progress changed");                                         
                    }
                }
            );                                  
        }
        else
        {               
            arrayList.get(position).mySeekBar = new SeekBar(theContext);        
            arrayList.get(position).mySeekBar.setMax(100);
            arrayList.get(position).mySeekBar.setEnabled(true);
            arrayList.get(position).mySeekBar.setOnSeekBarChangeListener(
                new SeekBar.OnSeekBarChangeListener() {                         
                    @Override
                    public void onStopTrackingTouch(SeekBar seekBar) {
                        Log.d("test", "*not* 0th seekbar touched");                
                    }

                    @Override
                    public void onStartTrackingTouch(SeekBar seekBar) {
                        Log.d("test", "*not* 0th seekbar touch lifted");
                    }

                    @Override
                    public void onProgressChanged(SeekBar seekBar, int arg1, boolean fromUser) {
                        Log.d("test", "*not* 0th seekbar progress changed");                                           
                    }
                }
            );
        }

        //***this is the fuzzy part -> I need to be able to obtain the layout for the 'parent ViewGroup' or something
        //to where I can somehow add my seekbar (and textview and imageview) and then return the 'parent view'
        //that is to say that, this method is required to return a View and I want to return a View that composits
        //my imageview, textview, and seekbar.  One main problem seems to be that an ArrayAdapter won't allow me to add
    //child views dynamically.
        final View viewToAdd = arrayList.get(position). mySeekBar;            
        LinearLayout layout = ???;//new LinearLayout(theContext);             
        layout.post(new Runnable() {
            public void run() {
                layout.addView(viewToAdd);
                layout.destroyDrawingCache();
                layout.buildDrawingCache();
            }
        });

      return ???;   //layout.view??  Can I obtain the 'parent view' (the view for the entire row) somehow to return?
    }        
}

private ArrayList<MyView> myArrayList;
    private MyAdapter myAdapter;

@Override
    public void onCreate(Bundle savedInstanceState) {
    try {           
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_for_the_list_activity_all_it_contains_is_a_linearlayout_containing_the_listview);
        myArrayList = new ArrayList<MyView>();
        myArrayList.add(new MyView());
        myArrayList.add(new MyView());
            mAdapter = new SettingsListViewArrayAdapter(this, R.layout.wtf_not_sure_what_I_should_put_here, this.myArrayList);
            setListAdapter(mAdapter);           
    } catch (NullPointerException e) {
        Log.v(getString(R.string.app_name), e.getMessage());
    }
  } 
}

For all I know I'm wayyy off track. From the looks of it, it's not even possible? Someone please help!? (and thanks in advance =-D )

P.S. I really don't care how to solve the problem, I just need to have a 'dynamically resizeable' list where each item contains an imageview, textview, and seekbar and to where I can modify each and receive callbacks for each individual seekbar. (if the solution scales, that would be cool but, most certainly not a requirement as the list will likely usually only contain less than 20 items)

来源:https://stackoverflow.com/questions/6211339/seekbar-in-a-listactivity-using-an-arrayadapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!