我正在使用按钮在视图中显示图像。 当我点击它时,我试图为每个图像打开一个不同的活动。
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);
}
}
答案 0 :(得分:0)
制作此内容,根据需要多次复制并粘贴,并将其更改为您想要的活动。确保你不要打开多个布局,否则会弄乱它。希望这会有所帮助。
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"/>