my HomeActivity.java:
ViewPager pager = (ViewPager) findViewById(R.id.pager);
FragmentManager fm = getSupportFragmentManager();
SlidingImage_Adapter pagerAdapter = new SlidingImage_Adapter(fm);
pager.setAdapter(pagerAdapter);
}
private Boolean exit=false;
@Override
public void onBackPressed() {
if (exit) {
finish();
} else {
Toast.makeText(this, "Tekan lagi untuk keluar.",
Toast.LENGTH_SHORT).show();
exit = true;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
exit = false;
}
}, 3 * 1000);
}
;
}
这是用于连接Homeact和image_model
我的SlidingImage_adapter.java:
@Override
public Fragment getItem(int arg0) {
ImageModel myFragment = new ImageModel();
Bundle data = new Bundle();
data.putInt("current_page", arg0+1);
myFragment.setArguments(data);
return myFragment;
}
@Override
public int getCount() {
return Page_Count;
}
这是从我制作的.xml中获取图片(layout1.xml,layout2.xml等)
my Image_Model.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle data = getArguments();
mCurrentPage = data.getInt("current_page", 0);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = null;
if (mCurrentPage == 1) {
v = inflater.inflate(R.layout.layout1, container, false);
} else if (mCurrentPage == 2) {
v = inflater.inflate(R.layout.layout2, container, false);
} else if (mCurrentPage == 3) {
v = inflater.inflate(R.layout.layout3, container, false);
}
return v;
}
答案 0 :(得分:0)
在 Image_Model.java
内 @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle data = getArguments();
mCurrentPage = data.getInt("current_page", 0);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = null;
if (mCurrentPage == 1) {
v = inflater.inflate(R.layout.layout1, container, false);
} else if (mCurrentPage == 2) {
v = inflater.inflate(R.layout.layout2, container, false);
} else if (mCurrentPage == 3) {
v = inflater.inflate(R.layout.layout3, container, false);
}
v.setOnClickListener(this);
return v;
}
@Override
public void onClick(View v){
Intent i=new Intent(getContext(), YouActivity.class);
getContext.startActivity(i);
}
答案 1 :(得分:0)
设置viewPager.clicable = true;在xml文件上或动态的,如果你想在viewpager上使用onClickListener方法。
答案 2 :(得分:0)
试试这个
pager.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent(getContext(), YouActivity.class);
getContext.startActivity(i);
}
});