已经研究了一段时间,似乎无法找到任何解决方案。 我有一个5页图像采用PagerLayout滑动格式。 我想要做的是点击图片,我想去另一个XML文件/类。
代码:(我已经评论过我尝试过的方法。)
public class CustomSwipeAdapter extends PagerAdapter {
Context context;
private int[] image_resources = {R.drawable.chestpress,R.drawable.deadlift,R.drawable.squat,R.drawable.pullups,R.drawable.dips};
private Context ctx; //Gets the context of a current state or application. Tells program what is going on somewhere else.
private LayoutInflater layoutInflater; //USed to instantiate layout XML files to View Objct.
private LayoutInflater inflater;
private String[] names = {"Dumbell Press","Deadlift","Squats","Pullups","Tricep Dips"};
public ImageView imageView;
Activity activity;
View views = null;
public CustomSwipeAdapter(Context ctx){
this.ctx = ctx;
}
@Override
public int getCount() {
return image_resources.length;
}
@Override
public boolean isViewFromObject(View view, Object o) {
return view==(LinearLayout)o;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item_view = layoutInflater.inflate(R.layout.swipe_layout,container,false);
imageView = (ImageView)item_view.findViewById(R.id.image_view);
TextView textView = (TextView)item_view.findViewById(R.id.image_count);
imageView.setImageResource(image_resources[position]);
textView.setText(names[position]);
container.addView(item_view);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(position == 0) {
Log.d("Exercise","Dumbell Press");
//Intent intent = new Intent(ctx,MyActivity.class);
//Intent intent = new Intent(CustomSwipeAdapter.this,MyActivity.class);
//Intent intent = new Intent(context,MyActivity.class);
//views = inflater.inflate(R.layout.main,null);
//context.startActivity(intent);
//MyActivity koo = new MyActivity(); (Tried just instantiating another class (Im so lost)
}
if(position == 1){
Log.d("Exercise","Deadlift!");
}
if(position == 2){
Log.d("Exercise","Squats");
}
}
});
return item_view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout)object);
}
}
非常新的android / java程序员。谢谢你的阅读。
答案 0 :(得分:1)
试试这个:
Intent intent = new Intent(CustomSwipeAdapter.this.ctx, MyActivity.class);
CustomSwipeAdapter.this.ctx.startActivity(intent);
还要确保将正确的上下文传递给Adapter构造函数。您将传递的上下文可以是活动本身。
因此,在您的Activity上,当您需要创建新适配器时,请执行以下操作:
CustomSwipeAdapter myAdapter = new CustomSwipeAdapter(this);
其中this
是您要在其上创建适配器的活动。
如果需要在片段内创建适配器,只需使用以下命令获取活动:
MyFragment.getActivity();
最后,如果你想知道:"为什么会这样?它期望一个Context和我传递一个活动!"。
这是一个简短的回答:
活动继承上下文。因此,如果您正在参加某项活动,则只能参加 需要传递自己来使用上下文。它还包含一个指针 getBaseContext()。您可能偶尔需要参考,如果 你需要整个应用程序的上下文,但很可能你不会 一会儿。
希望这会有所帮助。 如果您再收到任何错误,请告诉我(不要忘记发布您的logcat,以便我们为您提供更好的帮助)。
答案 1 :(得分:1)
理想情况下你应该做的就是这个,
1:创建一个名为public class CustomSwipeAdapter extends PagerAdapter {
Context context;
private int[] image_resources = {R.drawable.chestpress, R.drawable.deadlift, R.drawable.squat, R.drawable.pullups, R.drawable.dips};
private Context ctx; //Gets the context of a current state or application. Tells program what is going on somewhere else.
private LayoutInflater layoutInflater; //USed to instantiate layout XML files to View Objct.
private LayoutInflater inflater;
private String[] names = {"Dumbell Press", "Deadlift", "Squats", "Pullups", "Tricep Dips"};
public ImageView imageView;
Activity activity;
View views = null;
private swipeAdapterCallback mCallback;
public CustomSwipeAdapter(Context ctx,swipeAdapterCallback callback) {
this.ctx = ctx;
this.mCallback = callback;
}
@Override
public int getCount() {
return image_resources.length;
}
@Override
public boolean isViewFromObject(View view, Object o) {
return view == (LinearLayout) o;
}
@Override
public Object instantiateItem(ViewGroup container, final int position) {
inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item_view = layoutInflater.inflate(R.layout.swipe_layout, container, false);
imageView = (ImageView) item_view.findViewById(R.id.image_view);
TextView textView = (TextView) item_view.findViewById(R.id.image_count);
imageView.setImageResource(image_resources[position]);
textView.setText(names[position]);
container.addView(item_view);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mCallback.onImageClick(position);
}
});
return item_view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
public interface swipeAdapterCallback {
void onImageClick(int position);
}
}
之类的接口,声明一个处理onClick的方法
2:确保在构造函数中获取swipeAdapterCallback实例。
3:然后你要做的就是调用回调实例中的onClick方法。
我在这里发布了适配器代码:
@Override
public void onImageClick(int position) {
//handle intents and launching activities here.
switch(position){
case 0:
Log.d("Exercise","Dumbell Press");
Intent intent = new Intent(this,exampleAcitivity.class);
startActivity(intent);
break;
case 1:
Log.d("Exercise","Deadlift!");
break;
case 3:
Log.d("Exercise","Squats");
break;
}
}
然后在您的活动中,您所做的是实现此接口并在onImageClick函数中处理onClick,如此
Add-Type -AssemblyName System.Windows.Forms
$Form = New-Object system.Windows.Forms.Form
$Form.Text = 'ALERT!'
$form.ControlBox = $false;
$Image = [system.drawing.image]::FromFile('\\filepath')
$Form.BackgroundImage = $Image
$Form.BackgroundImageLayout = 'Stretch'
$Form.Width = (680)
$Form.Height = (550)
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(500,445)
$OKButton.Size = New-Object System.Drawing.Size(100,50)
$OKButton.Text = 'Accept'
$OKButton.Font = New-Object System.Drawing.Font('Times New Roman',18)
$OKButton.Add_Click({$Form.Close()})
$Form.Controls.Add($OKButton)
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()
由于这段代码将在您的Activity类中创建意图并开始新活动,因此处理这些活动的结果变得非常简单