package com.example.dongdong_frist_app.fragment;
import java.util.List;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;
import com.example.dongdong_frist_app.R;
public class HomePagerFragmnet extends Fragment implements OnClickListener {
private View view;
private ImageView iv_home;
private ImageView iv_cartgory;
private ImageView iv_cart;
private ImageView iv_personal;
private List<ImageView> vlist;
private HomeFragment mTab01;
private CartgoryFragment mTab02;
private CardFragment mTab03;
private PersonalFragment mTab04;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
view = inflater.inflate(R.layout.homepager, null);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
// 初始化控键对象
initView();
// 添加监听
setOnclickByZJ();
//选择Fragment视图
selectSet(0);
}
private void selectSet(int i) {
FragmentManager fm = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
hideFragment(transaction);
// 把图片设置为亮的
// 设置内容区域
switch (i)
{
case 0:
if (mTab01 == null)
{
mTab01 = new HomeFragment();
transaction.replace(R.id.fl_home, mTab01);
} else
{
transaction.show(mTab01);
}
iv_home.setImageResource(R.drawable.main_bottom_tab_home_focus);
break;
case 1:
if (mTab02 == null)
{
mTab02 = new CartgoryFragment();
transaction.replace(R.id.fl_home, mTab02);
} else
{
transaction.show(mTab02);
}
iv_cartgory.setImageResource(R.drawable.main_bottom_tab_category_focus);
break;
case 2:
if (mTab03 == null)
{
mTab03 = new CardFragment();
transaction.replace(R.id.fl_home, mTab03);
} else
{
transaction.show(mTab03);
}
iv_cart.setImageResource(R.drawable.main_bottom_tab_cart_focus);
break;
case 3:
if (mTab04 == null)
{
mTab04 = new PersonalFragment();
transaction.replace(R.id.fl_home, mTab04);
} else
{
transaction.show(mTab04);
}
iv_personal.setImageResource(R.drawable.main_bottom_tab_personal_focus);
break;
default:
break;
}
transaction.commit();
}
private void hideFragment(FragmentTransaction transaction) {
if (mTab01 != null)
{
transaction.hide(mTab01);
}
if (mTab02 != null)
{
transaction.hide(mTab02);
}
if (mTab03 != null)
{
transaction.hide(mTab03);
}
if (mTab04 != null)
{
transaction.hide(mTab04);
}
}
private void initView() {
iv_home = (ImageView) view.findViewById(R.id.iv_home);
iv_cartgory = (ImageView) view.findViewById(R.id.iv_home);
iv_cart = (ImageView) view.findViewById(R.id.iv_home);
iv_personal = (ImageView) view.findViewById(R.id.iv_home);
}
private void setOnclickByZJ() {
iv_home.setOnClickListener(this);
iv_cartgory.setOnClickListener(this);
iv_cart.setOnClickListener(this);
iv_personal.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switchDrawable();
switch (v.getId()) {
// 主页页面
case R.id.iv_home:
Toast.makeText(getActivity(), "点击了iv_home", 0).show();
selectSet(0);
break;
case R.id.iv_cartgory:
Toast.makeText(getActivity(), "点击了iv_cartgory", 0).show();
selectSet(1);
break;
case R.id.iv_cart:
selectSet(2);
break;
case R.id.iv_personal:
selectSet(3);
break;
}
}
private void switchDrawable() {
iv_home.setImageResource(R.drawable.main_bottom_tab_home_normal);
iv_cartgory.setImageResource(R.drawable.main_bottom_tab_category_normal);
iv_cart.setImageResource(R.drawable.main_bottom_tab_cart_normal);
iv_personal.setImageResource(R.drawable.main_bottom_tab_personal_normal);
}
}
相关分类