I'm new to Android development and I'm studying to code & design an Android project. I have problem with ViewPager
and can't find answer on this site or via a Google search.
Problem:
I can't make a Button
execute its action while it's in ViewPager
.
You can see my project & apk file here: https://dl.dropbox.com/u/9820517/ForOneTimeDownload/TestPagerView.rar
Here is my 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;
}
}
}
My layout content here:
<?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>
Here is an easy solution.
In your layout XML, in the button tag, set android:onClick="AnyName"
, then on your PagerDemo.java
, place:
public void AnyName(View v) {
// Do your stuff
}
That sets onClickListener
in a ViewPager
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With