【蘑菇街安卓版代码研究】漂亮的tabhost分类

先上图

                                              【蘑菇街安卓版代码研究】漂亮的tabhost分类_第1张图片

sidebar_category.xml

MGCategoryView.java

/**
 * 
 * @author YangJz
 *
 */
public class MGCategoryView extends TabHost{

	public MGCategoryView(Context context) {
		this(context, null);
	}

	public MGCategoryView(Context paramContext, AttributeSet paramAttributeSet) {
	    super(paramContext, paramAttributeSet);
	    
	    
	    
	}
	  public void setup()
	  {
	    super.setup();
	    getTabWidget().setOrientation(LinearLayout.VERTICAL);//设置布局方式为垂直布局
	  }	
}


MainActivity.java

package com.mogujie.view;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.TabHost;
import android.widget.TextView;

import com.mogujie.adapter.MGCategoryGridAdapter;
import com.mogujie.test.R;

public class MainActivity extends Activity {
	private SidebarView mSlidView;
	private FrameLayout mContentView;
	 public MGCategoryView mCatagoryView;
	 private List datas = new ArrayList();
	 
	 private Map mMapAdapter = new HashMap();
	 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        this.mSlidView = new SidebarView(this);
        setContentView(R.layout.sidebar_category);
//        LayoutInflater localLayoutInflater = LayoutInflater.from(this);
//        this.mContentView = this.mSlidView.getContent();
//        localLayoutInflater.inflate(2130903085, this.mContentView, true);
        this.mCatagoryView = ((MGCategoryView)this.findViewById(R.id.sidebar_category));
	    this.mCatagoryView.setup();
	    this.mCatagoryView.setOnTabChangedListener(new TabHost.OnTabChangeListener()
	    {
	      public void onTabChanged(String paramAnonymousString)
	      {
	        ((GridView)mCatagoryView.getCurrentView()).setAdapter((ListAdapter)mMapAdapter.get(paramAnonymousString));

	      }
	    });
	    
        MGCategoryItemData mCategoryData =new MGCategoryItemData();
	    mCategoryData.setTitle("JAVA");
	    mCategoryData.setStyle("shopping");
	    
	    List mListData1 = new ArrayList();
	    MGDetailCategoryData mm1 = new MGDetailCategoryData();
	    mm1.setTitle("高跟1");
	    mm1.setImageUrl(R.drawable.category1_50x50);
	    mListData1.add(mm1);
	    
	    MGDetailCategoryData mm2 = new MGDetailCategoryData();
	    mm2.setTitle("高跟2");
	    mm2.setImageUrl(R.drawable.category5_50x50);
	    mListData1.add(mm2);
	    MGDetailCategoryData mm3= new MGDetailCategoryData();
	    mm3.setTitle("高跟3");
	    mm3.setImageUrl(R.drawable.category2_50x50);
	    mListData1.add(mm3);
	    mm1 = new MGDetailCategoryData();
	    mm1.setTitle("高跟4");
	    mm1.setImageUrl(R.drawable.category3_50x50);
	    mListData1.add(mm1);
	    mm1 = new MGDetailCategoryData();
	    mm1.setTitle("高跟5");
	    mm1.setImageUrl(R.drawable.category4_50x50);
	    mListData1.add(mm1);
	    mCategoryData.setDetails(mListData1);
	    
	    datas.add(mCategoryData);
	    List mListData3 = new ArrayList();
	    mCategoryData =new MGCategoryItemData();
	    mCategoryData.setTitle("C++");
	    mCategoryData.setStyle("clothing");
	    mm1 = new MGDetailCategoryData();
	    mm1.setTitle("高跟6");
	    mm1.setImageUrl(R.drawable.category4_50x50);
	    mListData3.add(mm1);
	    mCategoryData.setDetails(mListData3);
	    
	    datas.add(mCategoryData);
	    mCategoryData =new MGCategoryItemData();
	    mCategoryData.setTitle("ASP");
	    mCategoryData.setStyle("shoes");
	    datas.add(mCategoryData);
	    mCategoryData =new MGCategoryItemData();
	    mCategoryData.setTitle(".NET");
	    mCategoryData.setStyle("bags");
	    datas.add(mCategoryData);
	    mCategoryData =new MGCategoryItemData();
	    mCategoryData.setTitle("Object");
	    mCategoryData.setStyle("accessories");
	    datas.add(mCategoryData);
	    mCategoryData =new MGCategoryItemData();
	    mCategoryData.setTitle("SHELL");
	    mCategoryData.setStyle("home");
	    datas.add(mCategoryData);
	    mCategoryData =new MGCategoryItemData();
	    mCategoryData.setTitle("SQL");
	    mCategoryData.setStyle("beauties");
	    datas.add(mCategoryData);
	    int i = 512;
	    for( MGCategoryItemData item : datas){
	    	initColor(item);
	        String str = item.getType();
	        i++;
	        initTab(str, item, i);
	    }
	  }
	  private void initTab(String paramString, MGCategoryItemData paramMGCategoryItemData, int paramInt)
	  {
		  Log.v("tddd", paramString);
	    TabHost.TabSpec localTabSpec =  mCatagoryView.newTabSpec(paramString);
	    TextView localTextView = new TextView(this);
	    localTextView.setText(paramMGCategoryItemData.getTitle());
	    localTextView.setTextColor(paramMGCategoryItemData.mFontColor);
	    localTextView.setBackgroundColor(paramMGCategoryItemData.mBackgroundColor);
	    localTextView.setGravity(ViewGroup.FOCUS_LEFT);
	    FrameLayout localFrameLayout = new FrameLayout(this);
	    localFrameLayout.setLayoutParams(new LinearLayout.LayoutParams(-1, -1, 1.0F));
	    View localView = new View(this);
	    localView.setId(16);
	    localView.setBackgroundResource(R.drawable.category_tab_bg);
	    localFrameLayout.addView(localTextView);
	    localFrameLayout.addView(localView);
	    localTabSpec.setIndicator(localFrameLayout);
	    
	    MGCategoryGridAdapter localMGCategoryGridAdapter = new MGCategoryGridAdapter(this, paramMGCategoryItemData.getDetails(), paramMGCategoryItemData.mFontColor);
	    
	    final GridView localGridView = new GridView(this);
	    localGridView.setSelector(new ColorDrawable(0));
	    localGridView.setOnItemClickListener(new AdapterView.OnItemClickListener()
	    {
	      public void onItemClick(AdapterView paramAnonymousAdapterView, View paramAnonymousView, int paramAnonymousInt, long paramAnonymousLong)
	      {
//	        MGDetailCategoryData localMGDetailCategoryData = (MGDetailCategoryData)localGridView.getAdapter().getItem(paramAnonymousInt);
//	        if (((Activity)MGCategoryView.this.getContext() instanceof MGIndexAct));
//	        for (String str1 = "首页右侧栏:"; ; str1 = "图墙右侧栏:")
//	        {
//	          String str2 = str1 + localMGDetailCategoryData.getTitle();
//	          MGCounter.instance(MGCategoryView.this.getContext()).addEvent("hh", str2);
//	          MGUri2Act.instance().toAct(MGCategoryView.this.getContext(), localMGDetailCategoryData.getLink());
//	          return;
//	        }
	      }
	    });
	    localGridView.setPadding(0, MGScreenTools.instance(this).dip2px(10), 0, 0);
	    localGridView.setBackgroundColor(paramMGCategoryItemData.mBackgroundColor);
	    localGridView.setNumColumns(3);
	    localGridView.setVerticalSpacing(MGScreenTools.instance(this).dip2px(10));
	    localGridView.setHorizontalSpacing(MGScreenTools.instance(this).dip2px(5));
	    localGridView.setId(paramInt);
	    Log.v("tddd", String.valueOf(localGridView.getId()));
	    mCatagoryView.getTabContentView().addView(localGridView);
	    this.mMapAdapter.put(paramString, localMGCategoryGridAdapter);
	    localTabSpec.setContent(localGridView.getId());
	    mCatagoryView.addTab(localTabSpec);
	  }
	private void initColor(MGCategoryItemData paramMGCategoryItemData)
	  {
	    String str = paramMGCategoryItemData.getType();
	    if ("shopping".equals(str))
	    {
	      paramMGCategoryItemData.mBackgroundColor = Color.parseColor("#ffdfd9");
	      paramMGCategoryItemData.mFontColor = Color.parseColor("#e65e5e");
	    }
	    else{
	      if ("clothing".equals(str))
	      {
	        paramMGCategoryItemData.mBackgroundColor = Color.parseColor("#ffecd9");
	        paramMGCategoryItemData.mFontColor = Color.parseColor("#e67a45");
	      }
	      else if ("shoes".equals(str))
	      {
	        paramMGCategoryItemData.mBackgroundColor = Color.parseColor("#fff9d9");
	        paramMGCategoryItemData.mFontColor = Color.parseColor("#dca445");
	      }
	      else if ("bags".equals(str))
	      {
	        paramMGCategoryItemData.mBackgroundColor = Color.parseColor("#ecffd9");
	        paramMGCategoryItemData.mFontColor = Color.parseColor("#88b52b");
	      }
	      else if ("accessories".equals(str))
	      {
	        paramMGCategoryItemData.mBackgroundColor = Color.parseColor("#d9ffec");
	        paramMGCategoryItemData.mFontColor = Color.parseColor("#46b769");
	      }
	      else if ("home".equals(str))
	      {
	        paramMGCategoryItemData.mBackgroundColor = Color.parseColor("#d9f2ff");
	        paramMGCategoryItemData.mFontColor = Color.parseColor("#40ade5");
	      }
	      else if ("beauties".equals(str))
	      {
	        paramMGCategoryItemData.mBackgroundColor = Color.parseColor("#d9dfff");
	        paramMGCategoryItemData.mFontColor = Color.parseColor("#5266cc");
	      }
	    }
	    Log.v("tddd", "color"+paramMGCategoryItemData.mBackgroundColor);
	  }
}

MGCategoryGridAdapter.java

package com.mogujie.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;

import com.mogujie.test.R;
import com.mogujie.view.MGDetailCategoryData;

public class MGCategoryGridAdapter extends BaseAdapter
{
  private Context mCtx;
  public List mListData = new ArrayList();
  private int mTitleColor;

  public MGCategoryGridAdapter(Context paramContext, List paramList, int paramInt)
  {
    this.mListData = paramList;
    this.mTitleColor = paramInt;
    this.mCtx = paramContext;
  }

  public int getCount()
  {
    return this.mListData.size();
  }

  public Object getItem(int paramInt)
  {
    return this.mListData.get(paramInt);
  }

  public long getItemId(int paramInt)
  {
    return paramInt;
  }

  public View getView(int paramInt, View paramView, ViewGroup paramViewGroup)
  {
    HandleView localHandleView;
    if (paramView == null)
    {
      paramView = LayoutInflater.from(this.mCtx).inflate(R.layout.category_item, null);
      localHandleView = new HandleView();
      localHandleView.mIconIv = ((ImageView)paramView.findViewById(R.id.category_icon));
      localHandleView.mTitleTv = ((TextView)paramView.findViewById(R.id.category_title));
      paramView.setTag(localHandleView);
      
      MGDetailCategoryData localMGDetailCategoryData = (MGDetailCategoryData)this.mListData.get(paramInt);
      localHandleView.mIconIv.setBackgroundResource(localMGDetailCategoryData.getImageUrl());
      localHandleView.mTitleTv.setText(localMGDetailCategoryData.getTitle());
      localHandleView.mTitleTv.setTextColor(this.mTitleColor);
    }
    else
    {
      localHandleView = (HandleView)paramView.getTag();
     
    }
    return paramView;
  }

  private static class HandleView
  {
    public ImageView mIconIv;
    public TextView mTitleTv;
  }
}

由于图片有点大,请需要的同学自行破解蘑菇街的apk



你可能感兴趣的:(android系列,蘑菇街安卓版代码研究,仿蘑菇街分类,仿蘑菇街)