我可以通过网络上的各种示例使用viewpager构建活动,但我是否可以不再使用原始活动中的视图?举例来说:
public class SimpleViewPagerActivity extends Activity {
LinearLayout thisIsWhatBreaks;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyPagerAdapter adapter = new MyPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(2);
thisIsWhatBreaks = (LinearLayout)findViewById(R.id.layoutThatsAlwaysNull);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
thisIsWhatBreaks.setBackgroundColor(Color.BLACK);
}
else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
thisIsWhatBreaks.setBackgroundColor(Color.RED);
}
}
public void farLeftButtonClick(View v)
{
Toast.makeText(this, "Far Left Button Clicked", Toast.LENGTH_SHORT).show();
}
public void farRightButtonClick(View v)
{
Toast.makeText(this, "Far Right Elephant Button Clicked", Toast.LENGTH_SHORT).show();
}
private class MyPagerAdapter extends PagerAdapter {
public int getCount() {
return 5;
}
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.farleft;
break;
case 1:
resId = R.layout.left;
break;
case 2:
resId = R.layout.middle;
break;
case 3:
resId = R.layout.right;
break;
case 4:
resId = R.layout.farright;
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 void finishUpdate(View arg0) {
// TODO Auto-generated method stub
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@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
}
}
}
我最初在thisIsWhatBreaks
中拥有main.xml
LinearLayout,但现在已将其移至farleft.xml
,这是PagerAdapter中充气的布局之一。
因此,我再也无法使用了
LinearLayout thisIsWhatBreaks = (LinearLayout)findViewById(R.id.layoutThatsAlwaysNull);
为LinearLayout赋值。我在哪里以及如何做到这一点?
我怎样才能再次为LinearLayout赋值,以便可以在OnConfurationChanged()方法中修改它,如此活动类中所示?由于我已将大部分视图从main.xml移至farleft.xml,并且无法找到使其工作的方法,因此我一直在我的活动中获得nullpointerexceptions。
答案 0 :(得分:3)
在ViewPager上,有一个名为getCurrentItem()
的方法,它将返回ViewPager中当前活动的View位置。
现在,创建自己的ViewPagerAdapter,制作视图列表,覆盖instantiateItem(pagerView, position)
,将视图添加到pagerView并根据请求的位置从列表中返回视图。
然后,您创建了一个名为getView(int)
的方法,该方法将根据您从getCurrentItem()
获得的位置为您提供视图。
最后,您现在可以拨打currentView.findViewById()
destroyItem()
。我认为没有更简单的方法可以做到这一点,因为看起来他们似乎没有提供任何getView()
方法,所以你基本上必须自己制作一个。但相比之下,你现在也可以很好地控制你的观点。
答案 1 :(得分:3)
我需要做的就是搬家:
thisIsWhatBreaks = (LinearLayout)findViewById(R.id.layoutThatsAlwaysNull);
在PagerAdapter的instantiateItem()内部扩展当前layout.xml的情况。
我对nullpointerexceptions的问题是我的所有观点都在onCreate()中被采取行动。我必须将onCreate()中的所有视图移动到instantiateItem()中的case语句;
请参阅下面的更正代码....以防万一其他人遇到此问题....
public class SimpleViewPagerActivity extends Activity {
LinearLayout thisIsWhatBreaks;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyPagerAdapter adapter = new MyPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(2);
//REMOVE THE LINEAR LAYOUT FROM BEING INSTANTIATED HERE!
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
thisIsWhatBreaks.setBackgroundColor(Color.BLACK);
}
else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
thisIsWhatBreaks.setBackgroundColor(Color.RED);
}
}
private class MyPagerAdapter extends PagerAdapter {
public int getCount() {
return 5;
}
public Object instantiateItem(View collection, int position) {
view = new View(MainHome.this);
final LayoutInflater inflater = (LayoutInflater) MainHome.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
switch(position){
case 0 :
view = inflater.inflate(R.layout.farleft, null);
thisIsWhatBreaks= (LinearLayout)v.findViewById(R.id.layoutThatUsedToAlwaysBeNull);
***Now you can modify thisIsWhatBreaks***
thisIsWhatBreaks.setBackgroundColor(Color.black);
break;
case 1:
view = inflater.inflate(R.layout.farright, null);
break;
default :
break;
}
((ViewPager) collection).addView(v,0);
return v;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public void finishUpdate(View arg0) {
// TODO Auto-generated method stub
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@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
}
}
}