与viewpager的误解

时间:2013-12-10 09:33:38

标签: android android-viewpager

我可以在instantiateItem()中执行不同的活动吗? 请查看我的截图链接..

在此屏幕截图中,红色TextView在instantiateItem()中写入....当拖动屏幕时,它会跟随...

所以,我想插入不同的布局和活动,而不是这个TextView ..我可以在instantiateItem()中编码吗?

  

http://tinypic.com/view.php?pic=zlagwn&s=5#.UqbeWye_80k

这是MyPageAdapter.java

public class MyPageAdapter extends PagerAdapter {

private Context ctx;

public MyPageAdapter(Context ctx) {
    this.ctx = ctx;
}

@Override
public Object instantiateItem(ViewGroup container, int position) {

        LayoutInflater vi=(LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    v=vi.inflate(R.layout.main, null);
    Button btnRefresh=(Button) v.findViewById(R.id.btnRefreshGold);
    btnRefresh.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            // my operation
        }
    });

    ((ViewPager)container).addView(v,0);
    return v;
}

@Override
public int getCount() {
    return 3;
}

@Override
public void destroyItem(View container, int position, Object object) {
    // TODO Auto-generated method stub
    ((ViewPager) container).removeView((View) object);
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return (view == object);
}

这是main.xml

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </TabWidget>

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:id="@+id/Tab1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <Button
                android:id="@+id/btnRefreshGold"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/btnRefresh" />

            <ListView
                android:id="@+id/myListView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >
            </ListView>
        </LinearLayout>

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="fill_parent" >
        </android.support.v4.view.ViewPager>
    </FrameLayout>
</LinearLayout>

2 个答案:

答案 0 :(得分:0)

将此添加到您的instantiateItem()

   Button button = (Button) container.findViewById(R.id.btnRefreshGold);
   button.setOnClickListener(new View.OnClickListener(){

   @override
   public void onClick(View v){
        //do what you want your button to do when clicked here
  });

您可以按照this link查看您的viewpager应该是什么样子。

答案 1 :(得分:0)

这是我在instantiateItem()

的答案

@覆盖 public Object instantiateItem(ViewGroup container,int position){

    LayoutInflater vi=(LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v=vi.inflate(R.layout.main, null);
Button btnRefresh=(Button) v.findViewById(R.id.btnRefreshGold);
btnRefresh.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        // TODO Auto-generated method stub
        // my operation
    }
});

((ViewPager)container).addView(v,0);
return v;

}