使用滑动和按钮实现ViewPager

时间:2012-06-19 19:26:49

标签: android android-viewpager

我一直在寻找一种方法来在我的tabhost中左/右滑动我的标签(必须与Android 2.1兼容,因此没有FragmentManagers等)来显示单独的布局。有很多使用ViewPager的例子,所以我放弃了Tabhost并选择实现这个code。我在下面有一些使用两个“屏幕”的测试代码。当用户输入查询(此处为screen1),然后显示结果页面(screen2)时,我需要能够在屏幕之间进行切换。用户还可以在查询页面和结果页面之间来回滑动。

我遇到的问题是如何为按钮实现onClickListener。通常,我会在screen1.java(下面)中实现它,但是当我点击按钮时,onClickListener似乎永远无法控制。我想ViewPager工作的方式是screen1布局以某种方式捆绑在ViewPager对象中。我试图让视图膨胀以获得screen1中的按钮,但事情变得混乱而且并不简单。

我工作的是FlingActivity中我的ViewPager(myPager)的静态成员变量。从PagerAdapter,当实例化screen1时,我可以在视图膨胀时将onClickListener设置为按钮对象,然后在单击按钮时引用静态变量FlingActivity.myPager to setCurrentItem()(更改视图)。 / p> 事实上,这一切看起来都非常糟糕。我需要做什么才能将我的onClickListener放回screen1.java类中?

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<android.support.v4.view.ViewPager
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/myfivepanelpager"/>
</LinearLayout>

screen1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tab1_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CheckBox" />

</LinearLayout>

screen2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tab2_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/iv_2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

screen1.java

package test.com.vfling;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class screen1 extends Activity
{
    private final String TAG = "** screen1 **";

    @Override
    protected void onCreate (Bundle savedInstanceState)
    {
        super.onCreate (savedInstanceState);

        Log.d (TAG, "oncreate");
        setContentView (R.layout.screen1);
    }

}

screen2.java

package test.com.vfling;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class screen2 extends Activity 
{
    private final String TAG = "** screen2 **";

    @Override
    protected void onCreate (Bundle savedInstanceState)
    {
         super.onCreate (savedInstanceState);
        Log.d (TAG, "oncreate");
        setContentView (R.layout.screen2);      
    }


}

MyPagerAdapter.java

package test.com.vfling;

import android.content.Context;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MyPagerAdapter extends PagerAdapter
{
    private String TAG = "** MyPagerAdapter **";

    @Override
    public int getCount ()
    {
        //Log.d (TAG, "getCount");
        return 2;
    }

    public Object instantiateItem (View collection, int position)
    {
        Log.d (TAG, "instantiateItem");

        LayoutInflater inflater = (LayoutInflater) collection.getContext ().getSystemService (Context.LAYOUT_INFLATER_SERVICE);
        int resId = 0;

        switch (position)
        {
            case 0:
                                resId = R.layout.screen1;
                                Log.d (TAG, "screen1");
            break;
            case 1:
                                resId = R.layout.screen2;                               
                                Log.d (TAG, "screen2");
            break;
        }

        View view = inflater.inflate (resId, null);
        ((ViewPager) collection).addView (view, 0);


        switch (position)
        {
            case 0:
                                Button b = (Button) view.findViewById (R.id.button1);
                                b.setOnClickListener (ocl);
                                Log.d (TAG, "screen1");
            break;
        }


        return view;
    }



    @Override
    public void destroyItem (View arg0, int arg1, Object arg2)
    {
        Log.d (TAG, "destroyitem");
        ((ViewPager) arg0).removeView ((View) arg2);
    }

    @Override
    public boolean isViewFromObject (View arg0, Object arg1)
    {
        Log.d (TAG, "isviewfromobject");
        return arg0 == ((View) arg1);
    }

    @Override
    public Parcelable saveState ()
    {
        return null;
    }


    OnClickListener ocl = new OnClickListener()
    {

        @Override
        public void onClick (View v)
        {
            switch (v.getId ())
            {
                        case R.id.button1:
                    Log.d (TAG, "button 1 click");
                        FlingActivity.myPager.setCurrentItem (1);
                        break;
            }

        }
    };
}

FlingActivity.java

package test.com.vfling;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;

public class FlingActivity extends Activity
{
    private final String TAG = "** test_vfling **";
    public static ViewPager myPager;

    @Override
    protected void onCreate (Bundle savedInstanceState)
    {
       super.onCreate (savedInstanceState);
       setContentView (R.layout.main);

       MyPagerAdapter adapter = new MyPagerAdapter ();
       myPager = (ViewPager) findViewById (R.id.myfivepanelpager);
       myPager.setAdapter (adapter);
       myPager.setCurrentItem (1);
    }
}

0 个答案:

没有答案