I make custom ListView on android in which there are three textviews, ImageView and SeekBar . I can’t figure out how to make the SeekBara move in each separate ListView item.

Below is the markup of the ListView .

activity_main.xml

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <ImageView android:id="@+id/imageView1" android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/ic_launcher" /> <SeekBar android:id="@+id/seekBar1" android:layout_width="97dp" android:layout_height="50dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="26dp" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_marginLeft="36dp" android:textSize="22dp" android:layout_toRightOf="@+id/imageView1" android:text="TextView" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/imageView1" android:layout_alignLeft="@+id/textView1" android:text="TextView" /> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginRight="14dp" android:layout_marginTop="18dp" android:text="TextView" /> <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/textView3" android:layout_alignBottom="@+id/textView3" android:layout_alignParentLeft="true" /> </RelativeLayout> 

Layout main layout.

list.xml

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" > </ListView> </RelativeLayout> 

And the main code is MainActivity.java

 package com.ofs.mif.slidebar; import java.util.ArrayList; import java.util.HashMap; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.graphics.BitmapFactory; import android.graphics.Color; import android.view.LayoutInflater; import android.view.Menu; import android.view.View; import android.widget.ImageView; import android.widget.ListView; import android.widget.SeekBar; import android.widget.SimpleAdapter; import android.widget.TextView; public class MainActivity extends Activity { private static final String IMAGE_ITEM = "image"; private static final String SUB_ITEM = "sub_item"; String names[] = {"BEERЛОЖА","ХМЕЛЬНАЯ №1","ITALIANO","КАПИТАЛ","ШТАТ 51","ЙОХО","ПИВНОЙ ДВОР", "FERZ","ПЕТЦОЛЬДЪ","ПАРУС","МАМАДЖАН","РАШПЕР" }; private ArrayList<HashMap<String, Object>> myList; TextView txt1,txt2,txt3,txt4; ImageView img; int pos; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.list); Create_List(); LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View rowView = inflater.inflate(R.layout.activity_main, null); final SeekBar seekbar = (SeekBar)rowView.findViewById(R.id.seekBar1); seekbar.setOnSeekBarChangeListener(seekBarChangeListener); txt1 = (TextView)rowView.findViewById(R.id.textView1); txt2 = (TextView)rowView.findViewById(R.id.textView2); txt3 = (TextView)rowView.findViewById(R.id.textView3); txt4 = (TextView)rowView.findViewById(R.id.textView4); img = (ImageView)rowView.findViewById(R.id.imageView1); seekbar.setMax(6); } private SeekBar.OnSeekBarChangeListener seekBarChangeListener = new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { //seekBar.setClickable(false); // TODO Auto-generated method stub if(seekBar.getProgress() > 0) { img.setBackgroundColor(Color.GREEN); seekBar.setBackgroundColor(Color.GREEN); txt1.setX(126 + 9*seekBar.getProgress()); txt2.setX(126 + 9*seekBar.getProgress()); txt4.setText(String.valueOf(seekBar.getProgress()) + " шт."); img.setImageBitmap(BitmapFactory.decodeResource (getResources(), Color.GREEN)); } else { img.setBackgroundColor(Color.TRANSPARENT); img.setImageResource(R.drawable.ic_launcher); txt4.setText(""); seekBar.setBackgroundColor(Color.TRANSPARENT); } } @Override public void onStartTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } @Override public void onStopTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } }; @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } private void Create_List(){ ListView list = (ListView)findViewById(R.id.list); myList = new ArrayList<HashMap<String,Object>>(); HashMap<String, Object> hashmap; for(int i=0; i<=names.length-1;i++){ hashmap = new HashMap<String, Object>(); hashmap.put(IMAGE_ITEM, R.drawable.ic_launcher); hashmap.put(SUB_ITEM, names[i]); myList.add(hashmap); } SimpleAdapter adapter = new SimpleAdapter(this,myList,R.layout.activity_main, new String[]{IMAGE_ITEM,SUB_ITEM}, new int[]{R.id.imageView1, R.id.textView1} ); list.setAdapter(adapter); } } 
  • change the data in the adapter with the leaves, and say the adapter "redraw your list, the data has changed" - Vladyslav Matviienko
  • How? Something does not work? - vanyamelikov

2 answers 2

I recommend using your adapter for this purpose. In this adapter, you need to make a handler for each cell!

Adapter example

 public class MySimpleArrayAdapter extends ArrayAdapter<String> { private final Context context; private final String[] values; public MySimpleArrayAdapter(Context context, String[] values) { super(context, R.layout.rowlayout, values); this.context = context; this.values = values; } @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View rowView = inflater.inflate(R.layout.rowlayout, parent, false); TextView textView = (TextView) rowView.findViewById(R.id.label); ImageView imageView = (ImageView) rowView.findViewById(R.id.icon); textView.setText(values[position]); // Изменение иконки для Windows и iPhone String s = values[position]; if (s.startsWith("Windows7") || s.startsWith("iPhone") || s.startsWith("Solaris")) { imageView.setImageResource(R.drawable.no); } else { imageView.setImageResource(R.drawable.ok); } imageView.setOnclickListener(**ВАШ ОБРАБОТЧИК**) return rowView; } 

}

call to Activity

 public class MyListActivity extends ListActivity { public void onCreate(Bundle icicle) { super.onCreate(icicle); String[] values = new String[] { "Android", "iPhone", "WindowsMobile", "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2" }; MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(this, values); setListAdapter(adapter); } } 
  • Can you tell me how? - vanyamelikov
  • Added! You can see above! - Andrii Stakhov
  • I tried your offer, but it does not work very well! Could you please give your class to my sources? - vanyamelikov
  • And how can you ensure that when scrolling through the list, the scrolled elements are not updated? - vanyamelikov
 public class MySimpleArrayAdapter extends ArrayAdapter<String> implements SeekBar.OnSeekBarChangeListener { private final Context context; private final String[] values; private final LayoutInflater inflater; public MySimpleArrayAdapter(Context context, String[] values) { super(context, R.layout.rowlayout, values); this.context = context; this.values = values; inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public View getView(int position, View convertView, ViewGroup parent) { final DataHolder dataHolder; if (view == null) { view = inflater.inflate(R.layout.лэйаут_с_текствью_и_сикбаром,null); dataHolder = new DataHolder(); dataHolder.tv1 = (TextView)view.findViewById(R.id.textView1); dataHolder.tv2 = (TextView)view.findViewById(R.id.textView2); dataHolder.tv3 = (TextView)view.findViewById(R.id.textView3); dataHolder.tv4 = (TextView)view.findViewById(R.id.textView4); dataHolder.seekBar = (SeekBar)view.findViewById(R.id.seekBar1); view.setTag(dataHolder); } else dataHolder = (DataHolder)view.getTag(); String s = StringArray[position]; dataHolder.tv1.setText(s); dataHolder.tv2.setText(s); dataHolder.tv3.setText(s); dataHolder.tv4.setText(s); dataHolder.seekBar.setOnSeekBarChangeListener(this); return view; } public void onProgressChanged(SeekBar seekBar, int position, boolean fromUser) { if (fromUser){ //Пользователь тянет слайдер, чето делаем } } public void onStartTrackingTouch(SeekBar seekBar) { //Пользователь начал тянуть слайдер } public void onStopTrackingTouch(SeekBar seekBar) { //Пользователь окончил перетягивание слайдера } private class DataHolder { TextView tv1; TextView tv2; TextView tv3; TextView tv4; SeekBar seekBar; } 
  • Yes everything is correct! - Andrii Stakhov
  • I can not understand! But something does not work for me! Could you according to my source code from above show how to implement the whole project? - vanyamelikov