我正在学习Android编程,并进行了大量的搜索,但仍然找不到适合我的问题的正确答案。在ViewPager下的片段访问按钮
我正在做一个项目,在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;
}
}
fragment_layout.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" />
设置您在初始化按钮的位置单击侦听器。试一试。 – Raghunandan
感谢您的回答。我是多么愚蠢以至于错过简单的解决方案......这正是我需要的。 – Heobien