我正在学习 Android 编程并进行了大量搜索,但仍然无法为我的问题找到正确的答案。
我正在做一个项目来在 ViewPager 下实现一定数量的页面(或片段)。所有页面都具有相同的布局,带有 04 个按钮。
我的问题是我不知道在哪里放置按钮的 setOnClickListener 以检查在哪个页面上单击了哪个按钮?
感谢您提供解决问题的任何建议或线索。
我的项目示例如下:
MainActivity.java
public class MainActivity extends FragmentActivity {
private ViewPager pager;
private MyPagerAdapter pageAdapter;
private int numPages = 10; //number of pages, could be any number..
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pager = (ViewPager)findViewById(R.id.pager);
pageAdapter = new MyPagerAdapter(getSupportFragmentManager());
pager.setAdapter(pageAdapter);
}
private class MyPagerAdapter extends FragmentStatePagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
return MyFragment.newInstance(arg0);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return numPages;
}
}
activity_main.xml
<?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:gravity="center_horizontal"
android:orientation="vertical"
android:padding="4dip" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1" >
</android.support.v4.view.ViewPager>
MyFragment.java
ublic class MyFragment extends Fragment {
private Button btn1;
private Button btn2;
private Button btn3;
private Button btn4;
private TextView currentPage;
public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";
public static Fragment newInstance(int arg0) {
// TODO Auto-generated method stub
MyFragment f = new MyFragment();
Bundle bdl = new Bundle();
bdl.putInt(EXTRA_MESSAGE, arg0);
f.setArguments(bdl);
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
int i = getArguments().getInt(EXTRA_MESSAGE); //for later use
View v = inflater.inflate(R.layout.fragment_layout, container, false);
btn1 = (Button)v.findViewById(R.id.button1);
btn2 = (Button)v.findViewById(R.id.button2);
btn3 = (Button)v.findViewById(R.id.button3);
btn4 = (Button)v.findViewById(R.id.button4);
currentPage = (TextView)v.findViewById(R.id.pagenumber);
currentPage.setText("Page number #" + i);
return v;
}
}
片段布局.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="82dp"
android:text="Button 1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="40dp"
android:text="Button 2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/button2"
android:layout_below="@id/button2"
android:layout_centerVertical="true"
android:layout_marginTop="40dp"
android:text="Button 3" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/button3"
android:layout_below="@id/button3"
android:layout_marginBottom="98dp"
android:layout_marginTop="40dp"
android:text="Button 4" />
<TextView
android:id="@+id/pagenumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />