我正在尝试使用其中的一个按钮创建滑动视图,并且无论何时单击该按钮都将打开第二个活动,但是我无法在on click监听器中设置setActivity这是我的代码。我的寻呼机适配器
package com.example.neelaysrivastava.imageswipe;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class CustomeSwipeAdapter extends PagerAdapter {
private int[] image_resource={R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4,R.drawable.img5,R.drawable.img6} ;
private Context ctx;
private LayoutInflater layoutInflater;
public CustomeSwipeAdapter(Context ctx){
this.ctx=ctx;
}
@Override
public int getCount() {
return image_resource.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return (view==(LinearLayout)object );
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater =(LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item_view =layoutInflater.inflate(R.layout.second,container,false);
ImageView imageView =(ImageView)item_view.findViewById(R.id.imageView);
Button button=(Button)item_view.findViewById(R.id.button);
imageView.setImageResource(image_resource[position]);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
container.addView(item_view);
return item_view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout)object);
}
}
最佳答案
只需实现interface
并将其注册到按钮单击侦听器中,然后在您的活动中实现该接口,然后将该接口的实例传递给您的CustomeSwipeAdapter
构造函数即可。
关于android - 在swipeview中设置StartActivity,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35913799/