我正在尝试使用其中的按钮进行滑动视图,当点击该按钮时,它将打开第二个活动但我无法在点击监听器中设置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);
}
}
答案 0 :(得分:1)
只需实现interface
并在按钮单击侦听器中注册它,并在您的活动中实现该接口,并将该接口的实例传递给您的CustomeSwipeAdapter
构造函数。
答案 1 :(得分:0)
我在xml中为按钮设置onclick并调用它
public void anything(View v){
startActivity(new Intent(MainActivity.this,Some.class));
}
它将完成工作.Thx寻求帮助。