首页 > 代码库 > 第二次迭代感想
第二次迭代感想
这次我认领的任务是主界面功能的实现,这个项目相对前一个认领项目稍稍有些难度,所以花的时间和精力业更多了,而且有些地方跟本组同学发生了思想上擦出了火花,但是最后我们讨论并解决了。
这是这次我任务的java代码:
public class MainActivity extends ActionBarActivity implements View.OnClickListener{
private TextView txt_waimai;
private TextView txt_find;
private TextView txt_order;
private FrameLayout ly_content;
private HomeFragment homeFragment;
private FindFragment findFragment;
private OrderFragment orderFragment;
private android.app.FragmentManager fManager ;
private Toolbar toolbar;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private ListView lvLeftMenu;
private String[] lvs = {"账户信息", "我的余额", "我的收藏", "服务中心", "系统设置"};//可以放入String中,在拿取出来
private ArrayAdapter arrayAdapter;
private ActionBar actionBar ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fManager = getFragmentManager();
bindViews();
txt_waimai.performClick(); //模拟一次点击,既进去后选择第一项
findViews(); //获取控件
toolbar.setTitle("Foodie");//设置Toolbar标题
toolbar.setTitleTextColor(Color.parseColor("#FFFFFF")); //设置标题颜色
// actionBar= getActionBar();
setSupportActionBar(toolbar) ;//支持ActionBar
//创建返回键,并实现打开关/闭监听
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar,
R.string.open_drawer, R.string.close_drawer) {
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
};
mDrawerToggle.syncState();
mDrawerLayout.setDrawerListener(mDrawerToggle);
//设置菜单列表,写一个listView布局将其优化
arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, lvs);
lvLeftMenu.setAdapter(arrayAdapter);
lvLeftMenu.setOnItemClickListener(new DrawerItemClickListener());
// setStatusBar();//设置沉浸式状态栏,5.0.1系统有点Bug
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.action_search:
startActivity(new Intent(getApplicationContext(), Searchctivity.class));
break;
case R.id.toolBar_btn_login:
startActivity(new Intent(getApplicationContext(),LoginActivity.class));
break;
}
return false;
}
});
}
//将ActionBar添加进来
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = new MenuInflater(this) ;
inflater.inflate(R.menu.activity_main,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.isCheckable()){
item.setChecked(true);
}
return super.onOptionsItemSelected(item);
}
//UI组件初始化与事件绑定
private void bindViews() {
txt_waimai = (TextView) findViewById(R.id.txt_waimai);
txt_find = (TextView) findViewById(R.id.txt_find);
txt_order = (TextView) findViewById(R.id.txt_order);
ly_content = (FrameLayout) findViewById(R.id.ly_content);
txt_waimai.setOnClickListener(this);
txt_find.setOnClickListener(this);
txt_order.setOnClickListener(this);
}
//重置所有文本的选中状态
private void setSelected(){
txt_waimai.setSelected(false);
txt_find.setSelected(false);
txt_order.setSelected(false);
}
//隐藏所有Fragment
private void hideAllFragment(FragmentTransaction fragmentTransaction){
if(homeFragment != null)fragmentTransaction.hide(homeFragment);
if(findFragment != null)fragmentTransaction.hide(findFragment);
if(orderFragment != null)fragmentTransaction.hide(orderFragment);
}
@Override
public void onClick(View v) {
FragmentTransaction fTransaction = fManager.beginTransaction();
hideAllFragment(fTransaction);
switch (v.getId()){
case R.id.txt_waimai:
setSelected();
txt_waimai.setSelected(true);
if(homeFragment == null){
homeFragment = new HomeFragment();
fTransaction.add(R.id.ly_content,homeFragment);
}else{
fTransaction.show(homeFragment);
}
break;
case R.id.txt_find:
setSelected();
txt_find.setSelected(true);
if(findFragment == null){
findFragment = new FindFragment();
fTransaction.add(R.id.ly_content,findFragment);
}else{
fTransaction.show(findFragment);
}
break;
case R.id.txt_order:
setSelected();
txt_order.setSelected(true);
if(orderFragment == null){
orderFragment = new OrderFragment();
fTransaction.add(R.id.ly_content,orderFragment);
}else{
fTransaction.show(orderFragment);
}
break;
}
fTransaction.commit();
}
/**
* ListView上的Item点击事件
*
*/
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
/**
* 切换Item点击显示的页面
*
* @param position
*/
private void selectItem(int position) {
switch (position){
case 0:
startActivity(new Intent(this,AccountActivity.class));
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
startActivity(new Intent(this,SettingActivity.class));
break;
default:
break;
}
}
/**
* 获取控件
*/
private void findViews() {
toolbar = (Toolbar) findViewById(R.id.tl_custom);
mDrawerLayout = (DrawerLayout) findViewById(R.id.dl_left);
lvLeftMenu = (ListView) findViewById(R.id.lv_left_menu);
}
/**
* 设置沉浸式状态栏,需4.4版本以上
*/
// protected void setStatusBar() {
// StatusBarUtil.setColor(this, getResources().getColor(R.color.colorPrimary));
// StatusBarUtil.setColorForDrawerLayout(this, (DrawerLayout) findViewById(R.id.dl_left), getResources()
// .getColor(R.color.colorPrimary));
// }
/**
* 双击退出,根据时间来判断
*/
long waitTime = 2000;
long touchTime = 0;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN && KeyEvent.KEYCODE_BACK == keyCode) {
long currentTime = System.currentTimeMillis();
if((currentTime-touchTime)>=waitTime) {
//让Toast的显示时间和等待时间相同
Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
touchTime = currentTime;
}else {
System.exit(0);//会跳屏,关闭时会出现黑屏,需要优化,不能用finish()
}
return true;
}
return super.onKeyDown(keyCode, event);
}
}
第二次迭代感想
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。