gpt4 book ai didi

android - 在 Android 中为 ViewPager 设置默认页面

转载 作者:IT老高 更新时间:2023-10-28 13:18:45 37 4
gpt4 key购买 nike

我正在使用以下代码,MAX 是 2 页。默认情况下,位置为 0,并在右侧添加一个新页面。我膨胀了两个布局文件。

如何在应用启动时显示 page1 并在左侧添加新页面?谢谢。

ma​​in.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="wrap_content" android:layout_height="wrap_content"
android:id="@+id/pagerView" />


</LinearLayout>

Java 代码

public class MyPagerActivity extends Activity {
private Context context;
private int pageNumber;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = this;

ViewPager pagerView = (ViewPager)findViewById(R.id.pagerView);
pagerView.setAdapter(new AwesomePagerAdapter());

}


private class AwesomePagerAdapter extends PagerAdapter{

@Override
public void destroyItem(View collection, int position, Object view) {
((ViewPager) collection).removeView((View)view);
}

@Override
public void finishUpdate(View arg0) {
//setPageTitles(getPageNumber());
}

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

@Override
public Object instantiateItem(View collection, int position) {
/* TextView tv = new TextView(MyPagerActivity.this);
tv.setText("Bonjour PAUG " + position);
tv.setTextColor(Color.WHITE);
tv.setTextSize(30);

((ViewPager) collection).addView(tv,0);

return tv;*/

View view = getViewToShow(position);
((ViewPager) collection).addView(view,0);
return view;
}

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

@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
// TODO Auto-generated method stub

}

@Override
public Parcelable saveState() {
// TODO Auto-generated method stub
return null;
}

@Override
public void startUpdate(View arg0) {
// TODO Auto-generated method stub

}

}


private View getViewToShow(int position){
View view = null;
View layout;
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

switch(position){

case 0:
layout = mInflater.inflate(R.layout.elements, null);
view = layout;
break;
case 1: view =
layout = mInflater.inflate(R.layout.elements2, null);
view = layout;

break;
}
return view;
}

}

最佳答案

您是否尝试过使用 setCurrentItem 方法?

关于android - 在 Android 中为 ViewPager 设置默认页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7395655/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com