ViewPager下碎片访问按钮 [英] Access buttons in Fragments under ViewPager

查看:144
本文介绍了ViewPager下碎片访问按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习Android的编程和做大量的搜索,但还是没能找到我的问题得到妥善的答案。

I'm learning Android Programming and did lot of search but still could not find a proper answer for my issue.

我在做一个项目,以实现一定数量的下ViewPager页(或片段)。所有的页面有相同的布局与04的按钮。

I'm doing a project to implement a certain number of pages (or Fragments) under a ViewPager. All the pages had same layout with 04 buttons.

我的问题是我不知道放在哪里setOnClickListener的按钮,检查被点击在哪一页哪一个按钮?

My problem is I don't know where to put the setOnClickListener for the buttons to check which button is clicked at which page?

感谢您的任何建议或线索来解决它。

Thanks for any suggestion or clue to solve it.

我的项目样本如下:

MainActivity.java

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中

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

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

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" />

推荐答案

我觉得你可以从页面信息我

I think you can get the page information from i

getArguments()调用getInt(EXTRA_MESSAGE)

getArguments().getInt(EXTRA_MESSAGE)

,获得按钮实例按钮的信息。添加setOnClickListener每个按钮。例如:

, get button information from button instance. add setOnClickListener for each button. For example:

btn1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
     Toast.makeText(getApplicationContext(),  "Page:"+i+ " button:1",
     Toast.LENGTH_SHORT).show();

         }
     });

这篇关于ViewPager下碎片访问按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆