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); } }