您好,登录后才能下订单哦!
大家好,今天主要是实现仿大众点评引导页和主页面以及城市定位的实现,主要使用ViewPager+Fragment+SharedPreferences,实现了第一次打开程序出现引导页,再次打开跳过引导页,这也是一般应用常用的应用基本架构方式。下面首先来看最终实现效果如下图:
1.布局文件说明
1)欢迎页布局文件welcome.xml
2) 引导页布局文件welcome_guide.xml
3)首页布局文件main_home.xml
4)团购布局文件main_tuan.xml
5) 发现布局文件main_search.xml
6) 我的布局文件main_my.xml
布局文件内容较多,布局代码就不一一贴出来了,请看附件详细源码。
2.Activity说明
1)欢迎页Activity(WelcomeStartActivity)
package com.sunny.dianping_client; import java.util.Timer; import java.util.TimerTask; import com.sunny.dianping_client.utils.SharedUtils; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.Message; public class WelcomeStartActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.welcome); //使用Handler实现延时跳转 // new Handler(new Handler.Callback() { // // @Override // public boolean handleMessage(Message msg) { // // 页面跳转 // startActivity(new Intent(getApplicationContext(), MainActivity.class)); // return false; // } // }).sendEmptyMessageDelayed(0, 3000);//表示任务延时三秒执行 //使用Java定时器实现延时跳转 Timer timer = new Timer(); timer.schedule(new Task(), 3000);//定时器延时执行任务的方法 } class Task extends TimerTask{ @Override public void run() { // 实现页面的跳转 //不是第一次启动 if (SharedUtils.getWelcomeFlag(getBaseContext())) { startActivity(new Intent(getBaseContext(),MainActivity.class)); }else { startActivity(new Intent(WelcomeStartActivity.this, WelcomeGuideActivity.class)); //保存访问记录 SharedUtils.putWelcomeFlag(getBaseContext(), true); } finish(); } } }
2) 引导页Activity(WelcomeGuideActivity)
package com.sunny.dianping_client; import java.util.ArrayList; import java.util.List; import com.lidroid.xutils.ViewUtils; import com.lidroid.xutils.view.annotation.ViewInject; import com.lidroid.xutils.view.annotation.event.OnClick; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.ImageView; public class WelcomeGuideActivity extends Activity { @ViewInject(R.id.btn_welcome_guide) private Button mComeIn; @ViewInject(R.id.id_welcome_pager) private ViewPager mViewPager; private List<View> list; @Override protected void onCreate(Bundle savedInstanceState) { // TODO 自动生成的方法存根 super.onCreate(savedInstanceState); setContentView(R.layout.welcome_guide); ViewUtils.inject(this); initViewPager(); } @OnClick(R.id.btn_welcome_guide) public void click(View view){ // 实现页面的跳转 startActivity(new Intent(getBaseContext(), MainActivity.class)); finish(); } //初始化ViewPager public void initViewPager(){ list = new ArrayList<View>(); ImageView iv = new ImageView(this); iv.setImageResource(R.drawable.guide_01); list.add(iv); ImageView iv1 = new ImageView(this); iv1.setImageResource(R.drawable.guide_02); list.add(iv1); ImageView iv2 = new ImageView(this); iv2.setImageResource(R.drawable.guide_03); list.add(iv2); mViewPager.setAdapter(new MyPagerAdapter()); //监听ViewPager滑动效果 mViewPager.setOnPageChangeListener(new OnPageChangeListener() { //页卡被选中的效果 @Override public void onPageSelected(int arg0) { if (arg0==2) { mComeIn.setVisibility(View.VISIBLE); }else { mComeIn.setVisibility(View.GONE); } } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageScrollStateChanged(int arg0) { } }); } //定义ViewPager的适配器 class MyPagerAdapter extends PagerAdapter{ //计算需要多少Items显示 @Override public int getCount() { return list.size(); } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0==arg1; } //初始化Item实例的方法 @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(list.get(position)); return list.get(position); } //Item销毁的方法 @Override public void destroyItem(ViewGroup container, int position, Object object) { /** * 销毁Item的方法不使用系统定义销毁Item的方法,而是使用我们自己定义的形式,将一个View对象从ViewPager中移除 */ //super.destroyItem(container, position, object); container.removeView(list.get(position)); } } }
3) 主页面Activity(MainActivity)
package com.sunny.dianping_client; import com.lidroid.xutils.ViewUtils; import com.lidroid.xutils.view.annotation.ViewInject; import com.lidroid.xutils.view.annotation.event.OnClick; import com.sunny.dianping_client.fragment.FragmentHome; import com.sunny.dianping_client.fragment.FragmentMy; import com.sunny.dianping_client.fragment.FragmentSearch; import com.sunny.dianping_client.fragment.FragmentTuan; import android.os.Bundle; import android.app.Activity; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.Button; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; import android.widget.Toast; public class MainActivity extends FragmentActivity implements OnCheckedChangeListener{ @ViewInject(R.id.main_bottom_tabs) private RadioGroup mGroup; @ViewInject(R.id.main_home) private RadioButton main_home; private FragmentManager fragmentManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); ViewUtils.inject(this); //初始化FragmentManager fragmentManager = getSupportFragmentManager(); //设置默认选中 main_home.setChecked(true); mGroup.setOnCheckedChangeListener(this); changeFragment(new FragmentHome(), false); } @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // 选中不同的Fragment switch (checkedId) { case R.id.main_home://首页 changeFragment(new FragmentHome(), true); break; case R.id.main_tuan://团购 changeFragment(new FragmentTuan(), true); break; case R.id.main_search://发现 changeFragment(new FragmentSearch(), true); break; case R.id.main_my://我的 changeFragment(new FragmentMy(), true); break; default: break; } } //切换不同的Fragment public void changeFragment(Fragment fragment,boolean isInit){ //开启事物 FragmentTransaction beginTransaction = fragmentManager.beginTransaction(); beginTransaction.replace(R.id.main_content, fragment); if (!isInit) { //加上该段代码Fragment就不会出现重影的效果 beginTransaction.addToBackStack(null); } beginTransaction.commit(); } }
3.主界面Fragment的实现
1)首页Fragment(FragmentHome)
package com.sunny.dianping_client.fragment; import java.util.List; import com.lidroid.xutils.ViewUtils; import com.lidroid.xutils.view.annotation.ViewInject; import com.sunny.dianping_client.R; import com.sunny.dianping_client.utils.SharedUtils; import android.support.v4.app.Fragment; import android.content.Context; import android.content.Intent; import android.location.Address; import android.location.Geocoder; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.provider.Settings; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class FragmentHome extends Fragment implements LocationListener{ @ViewInject(R.id.main_top_city) private TextView mTopcity; private String cityName; private LocationManager locationManager; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.main_home, container, false); ViewUtils.inject(this, view); //获取数据并显示 mTopcity.setText(SharedUtils.getCityName(getActivity())); return view; } @Override public void onStart() { super.onStart(); // 检查GPS是否打开 checkGPSisOpen(); } //检查Gps是否打开 private void checkGPSisOpen() { // 获取当前LocationManager对象 locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE); boolean isOpen = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); if (!isOpen) { Intent intent = new Intent(); intent.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivityForResult(intent, 0); } //开始定位 startLocation(); } //使用GPS定位的方法 private void startLocation() { locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, this); } //接收并处理消息 private Handler handler = new Handler(new Handler.Callback() { @Override public boolean handleMessage(Message msg) { if (msg.what==1) { mTopcity.setText(cityName); } return false; } }); //获取对应位置的经纬度并且定位城市 private void updateWithNewLocation(Location location) { double lat = 0.0,lng = 0.0; if (location!=null) { lat = location.getLatitude(); lng = location.getLongitude(); Log.i("TAG", "经度是"+lat+"纬度是"+lng); }else { cityName = "无法获取城市信息"; } //通过经纬度获取地址 List<Address> list = null; Geocoder geocoder = new Geocoder(getActivity()); try { list = geocoder.getFromLocation(lat, lng, 2); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } if (list!=null&&list.size()>0) { for (int i = 0; i < list.size(); i++) { Address address = list.get(i); cityName = address.getLocality();//获取城市 } } //发送空消息 handler.sendEmptyMessage(1); } //位置信息更改执行方法 @Override public void onLocationChanged(Location location) { // 更新当前的位置信息 updateWithNewLocation(location); } //状态信息改变 @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } @Override public void onDestroy() { super.onDestroy(); //保存城市 SharedUtils.putCityName(getActivity(), cityName); //停止定位 stopLocation(); } //停止定位 private void stopLocation(){ locationManager.removeUpdates(this); } }
2)团购页Fragment(FragmentTuan)
package com.sunny.dianping_client.fragment; import com.sunny.dianping_client.R; import android.support.v4.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class FragmentTuan extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.main_tuan, container, false); } }
3)发现页Fragment(FragmentSearch)
package com.sunny.dianping_client.fragment; import com.sunny.dianping_client.R; import android.support.v4.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class FragmentSearch extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.main_search, container, false); } }
4)我的页Fragment(FragmentMy)
package com.sunny.dianping_client.fragment; import com.sunny.dianping_client.R; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class FragmentMy extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.main_my, container, false); } }
4.使用SharedPreferences实现标记的写入与读取
package com.sunny.dianping_client.utils; import android.R.bool; import android.content.Context; import android.content.SharedPreferences.Editor; import android.widget.EditText; /** * 实现标记的写入读取 * @author sunnyhack */ public class SharedUtils { private static final String FILE_NAME = "dianping"; private static final String MODE_NAME = "welcome"; // 获取flag标记值 public static boolean getWelcomeFlag(Context context) { return context.getSharedPreferences(FILE_NAME, context.MODE_PRIVATE) .getBoolean(MODE_NAME, false); } // 写入flag标记值 public static void putWelcomeFlag(Context context, boolean isFirst) { Editor editor = context.getSharedPreferences(FILE_NAME, context.MODE_APPEND).edit(); editor.putBoolean(MODE_NAME, isFirst); editor.commit(); } // 写入一个String类型的数据 public static void putCityName(Context context, String cityName) { Editor editor = context.getSharedPreferences(FILE_NAME, Context.MODE_APPEND).edit(); editor.putString("cityName", cityName); editor.commit(); } // 获取String类型的值 public static String getCityName(Context context) { return context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE) .getString("cityName", "选择城市"); } }
5.主界面按钮图片和文字的变色(主要使用选择器实现)
1)字体颜色选择器main_color_selector.xml
2)首页图片变色选择器main_home_selector.xml
2)团购图片变色选择器main_tuan_selector.xml
2)发现图片变色选择器main_search_selector.xml
2)我的图片变色选择器main_my_selector.xml
以上就是引导页和主界面的实现,记录下来给大家分享,一起交流学习~~
×××地址:http://download.csdn.net/detail/fysl1314/8802707
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。