Java tutorial
/* * Flan.Zeng 2011-2016 http://git.oschina.net/signup?inviter=flan * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.flan.stock.activity; import java.util.ArrayList; import java.util.Timer; import java.util.TimerTask; import com.flan.stock.R; import com.flan.stock.adapter.NavigatorFragmentAdapter; import com.flan.stock.fragment.FragmentNavigatorA; import com.flan.stock.fragment.FragmentNavigatorB; import com.flan.stock.service.StockService; import com.flan.stock.view.FooterMenuView; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.Toast; /** * app? * @author flan * @date 2015105 */ public class HomeActivity extends FragmentActivity { private boolean isExit = false; private ViewPager viewPager; private FooterMenuView menu; private ImageView[] dots; //???? private int navCurrentIndex = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.activity_home); viewPager = (ViewPager) this.findViewById(R.id.vp_fragment); ArrayList<Fragment> fragmentList = new ArrayList<>(); FragmentNavigatorA fragmenta = new FragmentNavigatorA(this); FragmentNavigatorB fragmentb = new FragmentNavigatorB(this); fragmentList.add(fragmenta); fragmentList.add(fragmentb); viewPager.setAdapter(new NavigatorFragmentAdapter(getSupportFragmentManager(), fragmentList)); viewPager.addOnPageChangeListener(new NavigatorPageChangeListener()); initDot(); //??? menu = (FooterMenuView) this.findViewById(R.id.footer_menu); menu.setCurrentMenu(R.id.menu_home); menu.setOnTouchListener(menu.new MenuOnTouchListener()); } @Override protected void onRestart() { super.onRestart(); menu.setCurrentMenu(R.id.menu_home); } class NavigatorPageChangeListener implements OnPageChangeListener { @Override public void onPageScrollStateChanged(int arg0) { } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageSelected(int position) { setCurrentDot(position); } } //?? private void initDot() { LinearLayout ll = (LinearLayout) this.findViewById(R.id.lay_dot); int size = viewPager.getAdapter().getCount(); dots = new ImageView[size]; for (int i = 0; i < dots.length; i++) { dots[i] = (ImageView) ll.getChildAt(i); dots[i].setTag(i); //dots[i].setEnabled(true); //? dots[i].setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { int position = (int) v.getTag(); //?? setCurrentDot(position); setCurrentView(position); } }); } dots[navCurrentIndex].setEnabled(false); //?? } //?? private void setCurrentDot(int position) { if (position < 0 || position >= viewPager.getAdapter().getCount() || position == navCurrentIndex) { return; } dots[position].setEnabled(false); dots[navCurrentIndex].setEnabled(true); navCurrentIndex = position; } //????? private void setCurrentView(int position) { if (position < 0 || position > viewPager.getAdapter().getCount()) { return; } viewPager.setCurrentItem(position); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { exitBy2Click(); } return false; } /** * ?? */ private void exitBy2Click() { if (isExit == false) { isExit = true; Toast.makeText(this, "?", Toast.LENGTH_SHORT).show(); new Timer().schedule(new TimerTask() { @Override public void run() { } }, 2000);// 2??? } else { //??? this.stopService(new Intent(this, StockService.class)); this.finish(); System.exit(0); } } }