package com.example.viewpagerok; import java.util.ArrayList; import android.annotation.SuppressLint; import android.app.Activity; import android.os.Bundle; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; import android.widget.Toast; @SuppressLint({ "InflateParams", "ShowToast" }) public class MainActivity extends Activity { ArrayList<View> arrayList; ViewPager viewPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView textView = (TextView) findViewById(R.id.textView1); TextView textView1 = (TextView) findViewById(R.id.textView2); TextView textView2 = (TextView) findViewById(R.id.textView3); textView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub viewPager.setCurrentItem(0); Toast.makeText(MainActivity.this, "1", 1).show(); } }); textView1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub viewPager.setCurrentItem(1); Toast.makeText(MainActivity.this, "2", 1).show(); } }); textView2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub viewPager.setCurrentItem(2); Toast.makeText(MainActivity.this, "3", 1).show(); } }); 为什么在viewpager实例化之前可以调用呢?因为,oncreate函数不是一行一行按照顺序,首先寻找的实例的代码,所以不会报错,第一步加载的层次是实例化层次,所有实例化都会在第一批加载,Java是按次序的,安卓是按层次,然后再按次序的。 arrayList = new ArrayList<View>(); arrayList.add(LayoutInflater.from(MainActivity.this).inflate( R.layout.page1, null)); arrayList.add(LayoutInflater.from(MainActivity.this).inflate( R.layout.page2, null)); arrayList.add(LayoutInflater.from(MainActivity.this).inflate( R.layout.page3, null)); viewPager = (ViewPager) findViewById(R.id.viewPager); viewPager.setAdapter(new dxc()); } class dxc extends PagerAdapter { @Override public int getCount() { // TODO Auto-generated method stub return arrayList.size(); } @Override public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return arg0 == arg1; } @Override public void destroyItem(ViewGroup container, int position, Object object) { // TODO Auto-generated method stub ((ViewPager) container).removeView(arrayList.get(position)); } @Override public Object instantiateItem(ViewGroup container, int position) { // TODO Auto-generated method stub ((ViewPager) container).addView(arrayList.get(position), 0); return arrayList.get(position); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } } public class MainActivity extends Activity { Button button; String a; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button=(Button)findViewById(R.id.button1); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub button.setText(a);//第二层,执行button行为,第一层加载的a已经实例化 } }); a=new String("1");//实例化,第一层加载 } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
安卓加载顺序
最新推荐文章于 2023-10-09 15:51:11 发布