我一直在寻找一种在我的标签主机内向左/向右滑动标签的方法(必须与 Android 2.1 兼容,因此没有 FragmentManagers 等)以显示单独的布局。有很多使用 ViewPager 的示例,所以我放弃了 Tabhost 并选择实现此代码。我在下面有一些使用两个“屏幕”的测试代码。当用户输入查询(此处为 screen1)时,我需要能够以编程方式在屏幕之间切换,然后显示结果页面(screen2)。用户还可以在查询页面和结果页面之间来回滑动。
我遇到的问题是如何为按钮实现 onClickListener。通常,我会在 screen1.java(如下)中实现它,但是当我单击按钮时,onClickListener 似乎永远不会获得控制权。我猜 ViewPager 工作 screen1 布局的方式以某种方式捆绑在 ViewPager 对象中。我尝试放大视图以获取 screen1 中的按钮,但事情变得一团糟而且并不简单。
我正在工作的是 FlingActivity 中我的 ViewPager (myPager) 的静态成员变量。从 PagerAdapter 中,当 screen1 被实例化时,我可以在视图膨胀时将 onClickListener 设置为按钮对象,然后在单击按钮时将静态变量 FlingActivity.myPager 引用到 setCurrentItem() (更改视图)。
事情是,这一切看起来真的很笨拙。我需要做什么才能让我的 onClickListener 回到我的 screen1.java 类中?
主要的.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);
}
}