从自定义滑块拖放不起作用

时间:2016-11-23 11:13:15

标签: java android

我正在尝试从自定义滑块拖放。 滑块位于viewPager中,并显示Imageviews。 我不知道我应该设置为我的longClickListener的女巫ID。

我尝试了Imagview(swip_image_view),但它返回null。 &安培;它不适用于viewpager。

为什么我的longClickListener无效?

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

1 个答案:

答案 0 :(得分:0)

您的图片swip_image_view位于布局custom_swipe_dieren.xml内。您应该在onLongClickListener方法中设置CustomHorizontalSwip -> instantiateItem(),如下所示:

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 */

}

这对你有用

由于