¿Cómo puedo cambiar el valor del elemento de ListView en los cambios de progreso de Seekbar?

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <SeekBar
        android:id="@+id/seekValue"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:max="100" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_margin="10dp">


        <ListView
            android:id="@+id/listSelector"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"></ListView>


    </LinearLayout>

</LinearLayout>

Y ListView Item es:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <RelativeLayout
        android:id="@+id/layoutSelector"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:background="@android:color/darker_gray">


        <TextView
            android:id="@+id/txtItemName"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@+id/txtValueChanges"
            android:padding="5dp"
            android:text="Item Name"
            android:textAppearance="?android:textAppearanceLarge"
            android:textStyle="bold" />


        <TextView
            android:id="@+id/txtValueChanges"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:padding="5dp"
            android:visibility="gone"
            android:text="0"
            android:textAppearance="?android:textAppearanceMedium"
            android:textStyle="bold" />


    </RelativeLayout>


</LinearLayout>

Ahora bien, esta es mi clase mailActivity se ve así:

    public class ListActivitys extends Activity {

    ArrayList listItemName;
    public static ListView listSelector;
    SeekBar seekValue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        listItemName = new ArrayList();



        for (int i = 0; i < 50; i++) {
            listItemName.add("Item : " + (i + 1));
        }

        seekValue = (SeekBar) findViewById(R.id.seekValue);
        listSelector = (ListView) findViewById(R.id.listSelector);
        listSelector.setAdapter(new AdapterList(ListActivitys.this, listItemName,seekValue));


    }


}

Y finalmente mi adaptador personalizado es:

    public class AdapterList extends BaseAdapter {

    Context mcContext;
    ArrayList listItems, TempListItems;
    int selectedPos = 0;
    SeekBar seekValue;


    public AdapterList(Context listActivitys, ArrayList listItemName, SeekBar seek) {
        listItems = listItemName;
        mcContext = listActivitys;
        seekValue = seek;


        createATemList(listItemName.size());
    }

    private void createATemList(int size) {
        TempListItems = new ArrayList();

        for (int i=0; i<size; i++){
            TempListItems.add("0");
        }
    }

    @Override
    public int getCount() {
        return listItems.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        LayoutInflater mInflater = (LayoutInflater) mcContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.adapter_list_item, null);
            holder = new ViewHolder();
            holder.txtItemName = (TextView) convertView.findViewById(R.id.txtItemName);
            holder.txtValueChanges = (TextView) convertView.findViewById(R.id.txtValueChanges);
            holder.layoutSelector = (RelativeLayout) convertView.findViewById(R.id.layoutSelector);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (selectedPos == position) {
            holder.layoutSelector.setBackgroundColor(mcContext.getResources().getColor(R.color.colorAccent));
        } else {
            holder.layoutSelector.setBackgroundColor(mcContext.getResources().getColor(R.color.transparent));
        }

        if (!TempListItems.get(selectedPos).equals("0")){
            holder.txtValueChanges.setVisibility(View.VISIBLE);
        }else{
            holder.txtValueChanges.setVisibility(View.GONE);
        }

        holder.txtItemName.setText(listItems.get(position).toString());
        holder.layoutSelector.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectedPos = position;
                notifyDataSetChanged();
            }
        });


        seekValue.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

                /*View view = (View) seekBar.getParent();
                if(view != null){
                    TextView tvProgress = (TextView)view.findViewById(R.id.txtValueChanges);
                    tvProgress.setVisibility(View.VISIBLE);
                    tvProgress.setText(progress + "%");
                }*/

                updateView(selectedPos, progress);
            }


            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        return convertView;
    }


    private void updateView(int selectedPos, int progress) {
        View v = ListActivitys.listSelector.getChildAt(selectedPos -
                ListActivitys.listSelector.getFirstVisiblePosition());

        if (v == null)
            return;


        TextView txtValueChanges = (TextView) v.findViewById(R.id.txtValueChanges);
        TempListItems.set(selectedPos,progress+"");
        if (!TempListItems.get(selectedPos).equals("0")){
            System.out.println("=== Progress : " + progress);
            txtValueChanges.setText(TempListItems.get(selectedPos)+"");
            notifyDataSetChanged();
        }
    }



    private class ViewHolder {
        TextView txtValueChanges;
        TextView txtItemName;
        RelativeLayout layoutSelector;
    }
}

¿Es posible cambiar el valor del elemento ListView cuando se cambia el valor de la barra de búsqueda? Si es así, entonces cómo ? por favor, ayúdame.

Gracias.

1
Gaurav Mandlik 15 dic. 2016 a las 10:20

2 respuestas

La mejor respuesta

En tu Activity

seekValue.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        // change the adapter datasource
        listItemName.get(adapter.selectedPos) = "value you want to change";  

        // notify update
        adapter.notifyItemChanged(adapter.selectedPos);
    }
}
1
Phan Van Linh 15 dic. 2016 a las 07:59

Creo que puedes hacerlo sin pasar la barra de búsqueda al adaptador.

  1. Modifique getView () así:

    if (selectedPos == position) {
        holder.layoutSelector.setBackgroundColor(mcContext.getResources().getColor(R.color.colorAccent));
        // Add the line below
        holder.txtValueChanges.setText(TempListItems.get(selectedPos));
    } else {
        holder.layoutSelector.setBackgroundColor(mcContext.getResources().getColor(R.color.transparent));
    }
    
  2. Cambie el updateView () [Nota: es público, no privado] así:

    public void updateView(int progress) {
        TempListItems.set(selectedPos, progress+"");
        System.out.println("=== Progress : " + progress);
        notifyDataSetChanged();
    }
    
  3. Mueva el OnSeekbarChangeListener () de nuevo a Activity onCreate ().

  4. Modifique OnSeekbarChangeListener () de esta manera:

    seekValue.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            AdapterList tmpAdapter = (AdapterList) listSelector.getAdapter();
            tmpAdapter.updateView(progress);
        }
        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {}
        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {}
    });
    

¡Espero que esto ayude!

1
i_A_mok 16 dic. 2016 a las 08:00