我正在用一个按钮在视图分页中显示图像。当我单击图像时,我会尝试为每个图像打开一个不同的活动。
package com.union.pr26;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import java.util.ArrayList;
public class MainActivity extends Activity {
ViewPager viewPager;
CustomeSwipeAdapter adapter;
ArrayList<Class>list=new ArrayList<Class>();
list.add(Class(Main2Activity.class))
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager=(ViewPager)findViewById(R.id.viewpager);
adapter=new CustomeSwipeAdapter(this);
viewPager.setAdapter(adapter);
}
public void activity(View v){
Class activityClass=list.get(1);
Intent intent =new Intent(this,activityClass);
startActivity(intent);
}
}适配器:
package com.union.pr26;
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.ImageView;
import android.widget.LinearLayout;
public class CustomeSwipeAdapter extends PagerAdapter {
private Context ctx;
final int position = 0;
private LayoutInflater layoutInflater;
private int []imgID ={R.drawable.img1,R.drawable.img2,R.drawable.img3};
public CustomeSwipeAdapter(Context ctx) {
this.ctx=ctx;
}
@Override
public int getCount() {
return imgID.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.extra,container,false);
ImageView imageView =(ImageView)item_view.findViewById(R.id.imageView);
imageView.setImageResource(imgID[position]);
container.addView(item_view);
return item_view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout)object);
}
}发布于 2016-03-17 17:10:54
制作此文件,复制并粘贴多次,然后将其更改为您想要的活动。确保你不要打开多个布局,否则会把它搞乱的。希望这能有所帮助。
Intent intent = new Intent(MainComp_Banner.this, MainComp_Banner_edit.class);
startActivity(intent);将它添加到你的清单中,这样它就不会让你崩溃,对你想要开始的每个活动都要这样做。
<activity
android:name=".MainComp_Banner_edit"
android:label="EditBanner"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar"/>https://stackoverflow.com/questions/36055393
复制相似问题