我最近又开始为android编程,我正在开发一个我希望实现水平视图寻呼机的应用程序,因此一个实例中的用户可以在两个页面之间滑动以输入/更新信息,在稍后的实例中滑动在四页之间查看他们输入/更新的信息。我基本上是为角色扮演游戏制作电子角色表。
我一直在处理水平视图寻呼机的这些教程: http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-horizontal-view-paging/
http://manishkpr.webheavens.com/android-viewpager-example/
我的问题是我见过的所有教程,h。视图寻呼机在主活动屏幕之外使用,有没有办法在后续屏幕上实现水平视图寻呼机?每当我尝试实现代码来处理主屏幕以外的页面时,它一旦到达该页面就会崩溃。
所以,长话短说,有没有人在非主页上成功实现水平视图寻呼机,如果是这样,怎么样?
我希望我有道理,但如果您有任何其他问题,请告诉我们!
08-24 01:44:34.310:I / ActivityManager(144):从pid 15115开始{cmp = com.echaractersheet / .CharacterStats1} 08-24 01:44:34.360:D / AndroidRuntime(15115):关闭VM 08-24 01:44:34.360:W / dalvikvm(15115):threadid = 1:线程退出未捕获异常(组= 0x40a581f8) 08-24 01:44:34.370:E / AndroidRuntime(15115):致命异常:主要 08-24 01:44:34.370:E / AndroidRuntime(15115):java.lang.RuntimeException:无法启动活动ComponentInfo {com.echaractersheet / com.echaractersheet.CharacterStats1}:java.lang.NullPointerException
characterstats.xml:
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/characterstatspager" />
characterstats1.xml和characterstats2.xml是我要在
之间滑动的两个页面CharacterStatsPagerAdapter.java: ...
public class CharacterStatsPagerAdapter extends PagerAdapter {
public int getCount() {
return 2;
}
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId =0;
switch (position) {
case 0:
resId = R.layout.characterstats1;
break;
case 1:
resId = R.layout.characterstats2;
break;
}
View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
}
CharacterStats1.java:
CharacterStatsPagerAdapter adapter = new CharacterStatsPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.characterstatspager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
答案 0 :(得分:0)
好的寻呼机将使用适配器中的数据,因此只要您将对象保留在适配器中,就可以了。所以也许在进入下一个屏幕之前,您应该将对象保存在适配器中(可能是另一个类中的静态字段,只是片刻),当您到达新页面时,您可以检索对象并将它们放入适配器中新的viewpager并添加新页面。这就是我将要做的事情。
<强>更新强>
这只是一个例子,这需要更多的代码行,但这是一个良好的开端。
public class Fields {
private String name;
private String lastName;
private int age;
private boolean male;
public Fields(){
this.name = "";
this.lastName = "";
}
public Fields(String name, String lastName, int age, boolean male) {
this.name = name;
this.lastName = lastName;
this.age = age;
this.male = male;
}
public View getRepresentation(Context mContext){
/*Create the view that ViewPager will display*/
LinearLayout layout = new LinearLayout(mContext);
layout.addView(new TextView(mContext)); //Name
layout.addView(new TextView(mContext)); //LastName
layout.addView(new TextView(mContext)); //Age
layout.addView(new CheckBox(mContext)); //Male/Female
return layout;
}
}
查看寻呼机适配器
public class ViewPagerAdapter extends PagerAdapter {
private List<Fields> pages;
private Context mContext;
public ViewPagerAdapter( Context mContext )
{
this.mContext = mContext;
}
public void setPages(List<Fields> pages){
this.pages = pages;
}
public List<Fields> getPages(){
return this.pages;
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
@Override
public int getCount()
{
return pages.size();
}
@Override
public Object instantiateItem( View pager, int position )
{
View view = pages.get(position).getRepresentation(mContext);
view.setId(position);
return view;
}
@Override
public void destroyItem( View pager, int position, Object view )
{
((ViewPager)pager).removeViewInLayout( (View) view );
}
@Override
public boolean isViewFromObject( View view, Object object )
{
return view.equals( object );
}
}
主类
public class MainActivity extends Activity {
private Context mContext;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.mContext = getApplicationContext();
setContentView(R.layout.viewpager_layout);
final List<Fields> fields = new ArrayList<Fields>();
fields.add(new Fields());
ViewPager mPager = (ViewPager) this.findViewById(R.id.viewpager);
Button nextActivity = (Button) this.findViewById(R.id.nextAct);
ViewPagerAdapter mAdapter = new ViewPagerAdapter(mContext);
mAdapter.setPages(fields);
mPager.setAdapter(mAdapter);
nextActivity.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SecondActivity.pages = fields;
Intent intent = new Intent(mContext, SecondActivity.class);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
第二课
public class SecondActivity extends Activity {
public static List<Fields> pages;
private Context mContext;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.mContext = getApplicationContext();
setContentView(R.layout.viewpager_layout);
final List<Fields> fields = pages;
pages = null;
fields.add(new Fields()); //Add the new Pages
ViewPager mPager = (ViewPager) this.findViewById(R.id.viewpager);
Button nextActivity = (Button) this.findViewById(R.id.nextAct);
ViewPagerAdapter mAdapter = new ViewPagerAdapter(mContext);
mAdapter.setPages(fields);
mPager.setAdapter(mAdapter);
nextActivity.setVisibility(View.GONE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
正如我所说,这只是一个例子,应该为你的对象改变Fields类,ViewPagerAdapter是你保存你将传递给下一个活动的页面列表的地方。 第二个活动的静态场只是用作活动和活动之间的桥梁 不应该过度使用,这就是为什么在获取数据后需要设置为NULL才能知道你 已经抓住了价值。希望它有所帮助。
注意:此代码不会按原样运行,需要更多代码。