2013-07-01 53 views
0

我正在学习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" /> 

+0

设置您在初始化按钮的位置单击侦听器。试一试。 – Raghunandan

+0

感谢您的回答。我是多么愚蠢以至于错过简单的解决方案......这正是我需要的。 – Heobien

回答

0

我想你可以从我

getArguments获取页面信息()。getInt(EXTRA_MESSAGE)

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

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

     } 
    }); 
+0

感谢您的帮助......这是正确的解决方案。 – Heobien

0

你的onclick听众应该在片段。

public class MyFragment extends Fragment implements OnClickListener{ 

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 void onClick(View v) { 
    //TODO do stuff here with the clicked buttons. 
} 

@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; 
} 
+0

至于它被点击的页面将取决于你点击的内容。如果您切换视图,您应该启动另一个意图。在这种情况下,只需将页码与意图一起作为额外的。 – doubleA

+0

感谢您快速回答,我只是通过在onClick(View v)中放置Toast消息来重新测试,但没有看到任何弹出窗口。我错过了什么? – Heobien

+0

在你的Toast消息中,确保你把getFragmentActivity()作为上下文。 – doubleA