Kas De Durpel Kas De Durpel - 16 days ago 4
Android Question

Drag and drop from custom slider doesn't work

I'm trying to make a drag and drop from a custom slider.
The slider is in a viewPager and is shows Imageviews.
I don't know witch ID I should set to my longClickListener.

I tried the Imagview (swip_image_view) but it return null.
& it doesn't work with the viewpager.

Why is my longClickListener not working?

public class Activity_Kind extends Activity {

ViewPager viewPager;
CustomHorizontalSwip customHorizontalSwip;
private GridView gridDieren;
private ImageView bus;
private ImageView slider;
private TextView text;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_kies_dier);

gridDieren = (GridView) findViewById(R.id.gridSelectDieren);
text = (TextView) findViewById(R.id.kiesdiertext);
bus = (ImageView) findViewById(R.id.imageBus);

viewPager = (ViewPager) findViewById(R.id.viewPager);
customHorizontalSwip = new CustomHorizontalSwip(this);
viewPager.setAdapter(customHorizontalSwip);

viewPager.setLongClickable(true);
viewPager.setOnLongClickListener(longClickListener);

//slider = (ImageView) findViewById(R.id.swip_image_view);
//slider.setOnLongClickListener(longClickListener);

bus.setOnDragListener(dragListener);
}

View.OnLongClickListener longClickListener = new View.OnLongClickListener() {

@Override
public boolean onLongClick(View v) {
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder myShadowBuilder = new View.DragShadowBuilder(v);
v.startDrag(data, myShadowBuilder, v, 0);
return true;
}
};

View.OnDragListener dragListener = new View.OnDragListener() {

@Override
public boolean onDrag(View v, DragEvent event) {
int dragEvent = event.getAction();

switch (dragEvent) {
case DragEvent.ACTION_DRAG_ENTERED:
final View view = (View) event.getLocalState(); //Krijgt alle informatie van die view die wordt gedraged
if (view.getId() == R.id.imageBus);
text.setText("Werkt");
//gridDieren.addView(view);
break;
case DragEvent.ACTION_DRAG_ENDED:
break;
case DragEvent.ACTION_DROP:
break;
}
return true;
}
};
}


public class CustomHorizontalSwip extends PagerAdapter {
private int [] imageResources ={R.drawable.beer,R.drawable.bever, R.drawable.kip, R.drawable.leeuw, R.drawable.nijlpaard, R.drawable.poes
,R.drawable.ezel,R.drawable.hond, R.drawable.ooievaar, R.drawable.varken, R.drawable.wormpje};
private Context ctx;
private LayoutInflater layoutInflater;

public CustomHorizontalSwip(Context c) {
ctx=c;
}

@Override
public int getCount() {

return imageResources.length;
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater= (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView=layoutInflater.inflate(R.layout.custom_swipe_dieren,container,false);
ImageView imageView=(ImageView) itemView.findViewById(R.id.swip_image_view);
imageView.setImageResource(imageResources[position]);
container.addView(itemView);
return itemView;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {

}

@Override
public boolean isViewFromObject(View view, Object object) {

return (view==object);
}
}

Answer

Your image swip_image_view is inside layout custom_swipe_dieren.xml. You should set onLongClickListener inside CustomHorizontalSwip -> instantiateItem() method like this :

public class CustomHorizontalSwip extends PagerAdapter {

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        layoutInflater= (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView=layoutInflater.inflate(R.layout.custom_swipe_dieren,container,false);
        ImageView imageView=(ImageView) itemView.findViewById(R.id.swip_image_view);
        imageView.setImageResource(imageResources[position]);
        imageView.setOnLongClickListener(longClickListener);
        container.addView(itemView);
        return itemView;
    }

    /* Declare and Initialise longClickListener in this file also */

    /* Also do same for dragListener in this file */

}

This will work for you

Thanks