gpt4 book ai didi

java - 从自定义 slider 拖放不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 12:02:35 27 4
gpt4 key购买 nike

我正在尝试从自定义 slider 进行拖放。 slider 位于 viewPager 中并显示 ImageView 。我不知道应该将女巫 ID 设置为 longClickListener。

我尝试了 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);
}
}

最佳答案

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

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

}

这对你有用

谢谢

关于java - 从自定义 slider 拖放不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40762917/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com