我是 Android 开发的新手,我正在学习编码和设计一个 Android 项目。我有问题,ViewPager
无法在本网站或谷歌搜索中找到答案。
问题:Button
当它在ViewPager
.
您可以在此处查看我的项目和 apk 文件:https ://dl.dropbox.com/u/9820517/ForOneTimeDownload/TestPagerView.rar
这是我的 PagerDemo.java:
public class PagerDemo extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_pager_demo);
myPagerAdapter adapter = new myPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(2);
SetAllBtnFunc();
}
public void SetAllBtnFunc() {
//all func
View.OnTouchListener touch = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Log.d("hlv_trinh", "Button be Touched!");
return false;
}
};
View.OnClickListener click = new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d("hlv_trinh", "Button be Clicked!");
}
};
//Get view
View v = getLayoutInflater().inflate(R.layout.middle, null);
Button b = (Button) v.findViewById(R.id.myBtn);
b.setOnClickListener(click);
b.setOnTouchListener(touch);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_pager_demo, menu);
return true;
}
private class myPagerAdapter extends PagerAdapter {
public int getCount() {
return 5;
}
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position) {
case 0:
resId = R.layout.farleft;
break;
case 1:
resId = R.layout.left;
break;
case 2:
resId = R.layout.middle;
break;
case 3:
resId = R.layout.right;
break;
case 4:
resId = R.layout.farright;
break;
}
View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
}
}
我的布局内容在这里:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Middle"
android:textSize="30dp" >
</TextView>
<Button
android:id="@+id/myBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test Click" />
</LinearLayout>