0

it's been several days that I try to solve it I have try this exemple: http://www.mkyong.com/android/android-gridview-example/

i want to detect an onStopTrackingTouch on a seekbar in a customadapter. This exemple is working fine with OnItemClickListener, but if i add a seekbar in the layout mobile.xml, I can't detect the StopTrackingTouch and get the seek bar value ! Damned how to solve it?

the layout : mobile.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dp" >

    <ImageView
        android:id="@+id/grid_item_image"
        android:layout_width="50px"
        android:layout_height="50px"
        android:layout_marginRight="10px"
        android:src="@drawable/android_logo" >
    </ImageView>

    <TextView
        android:id="@+id/grid_item_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:layout_marginTop="5px"
        android:textSize="15px" >
    </TextView>

    <SeekBar
        android:layout_width="267dp"
        android:layout_height="wrap_content"
        android:id="@+id/seekBar2"
        android:focusable="false"/>

</LinearLayout>

The Adapter:

package com.mkyong.android.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.mkyong.android.R;
    public class ImageAdapter extends BaseAdapter {
    private Context context;
    private final String[] mobileValues;

    public ImageAdapter(Context context, String[] mobileValues) {
        this.context = context;
        this.mobileValues = mobileValues;
    }

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

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View gridView;

        if (convertView == null) {
            gridView = new View(context);
            // get layout from mobile.xml
            gridView = inflater.inflate(R.layout.mobile, null);
            // set value into textview
            TextView textView = (TextView) gridView.findViewById(R.id.grid_item_label);


            textView.setText(mobileValues[position]);
            // set image based on selected text
            ImageView imageView = (ImageView) gridView.findViewById(R.id.grid_item_image);
            String mobile = mobileValues[position];

            if (mobile.equals("Windows")) {
                imageView.setImageResource(R.drawable.windows_logo);
            } else if (mobile.equals("iOS")) {
                imageView.setImageResource(R.drawable.ios_logo);
            } else if (mobile.equals("Blackberry")) {
                imageView.setImageResource(R.drawable.blackberry_logo);
            } else {
                imageView.setImageResource(R.drawable.android_logo);
            }
        } else {
            gridView = (View) convertView;
        }
        return gridView;
    }

    @Override
    public int getCount() {
        return mobileValues.length;
    }

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

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

}

The Activity:

package com.mkyong.android;

import com.mkyong.android.adapter.ImageAdapter;
import android.app.Activity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;

public class GridViewActivity extends Activity {
    GridView gridView;

    static final String[] MOBILE_OS = new String[] { "Android", "iOS","Windows", "Blackberry" };
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        gridView = (GridView) findViewById(R.id.gridView1);
        gridView.setAdapter(new ImageAdapter(this, MOBILE_OS));
        gridView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                position=position;
                int a=3;
                //               Toast.makeText(getApplicationContext(),((TextView) v.findViewById(R.id.grid_item_label)).getText(), Toast.LENGTH_SHORT).show();
                //               Toast.makeText(getApplicationContext(),((TextView) v.findViewById(R.id.From)).getText(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}

I've try to add this but not working (of course):

gridView.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }

    });

How can solve this?

Metehan
  • 729
  • 5
  • 22
morbak
  • 317
  • 4
  • 17
  • 1
    Does `onProgressChanged` not work also? – Simas Sep 03 '14 at 13:10
  • Your girdview onitemclick overrides the row item views onclick, clear the itemclick from the gridview or handle the clicks under gridview itemclick. – jitain sharma Sep 03 '14 at 13:12
  • Okay, Using the guide at[SeekBar in a ListActivity using an ArrayAdapter][1] [1]: http://stackoverflow.com/questions/6211339/seekbar-in-a-listactivity-using-an-arrayadapter – Nabasree Suvasourya Sep 03 '14 at 13:17

2 Answers2

1

You should add setOnSeekBarChangeListener to the seekbar1 not to the gridView. So delete

gridView.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }

});` 

from MainActivity and add the following to your Adapter's getView method:

SeekBar seekbar= (SeekBar) gridView.findViewById(R.id.seekBar2);
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }

});
Metehan
  • 729
  • 5
  • 22
  • I put this in my adapter's getview, i can get the seekbar value, but how to catch the seek bar value in the Main Activity? – morbak Sep 04 '14 at 06:48
  • Use a public variable in the Adapter class, like `final public int seekBarValue;` then set it in OnSeekBarChangeListener. Now you can reach it from the Main Activity by `gridView.seekBarValue` – Metehan Sep 04 '14 at 07:10
  • i use `public int seekBarValue;` in my adapter, but in the main activity, ther's an error, `"cannot resolve seekBarValue"` – morbak Sep 04 '14 at 08:20
  • Oh try this: `ImageAdapter adp = new ImageAdapter(this, MOBILE_OS);` in your main Activity and call `adp.seekBarValue` since it's adapter's variable. And reformat this line `gridView.setAdapter(adp);` – Metehan Sep 04 '14 at 08:35
  • meanwhile i've done this: [link]http://stackoverflow.com/questions/5659879/global-declaration-of-variable-in-android[link], and this is working. But your code is working too, thx. – morbak Sep 04 '14 at 13:01
0

Add following line in your linear layout.

android:descendantFocusability="blocksDescendants"

Now list item will be clickable.

Mohit Rajput
  • 439
  • 3
  • 18