快捷搜索:  as  test  1111  test aNd 8=8  test++aNd+8=8  as++aNd+8=8  as aNd 8=8

亚虎官网登录:实战Android仿人人客户端之应用主界面之左侧面板UI实现



一、效果图比较

各人官方的主界面截图:

我仿的主界面之左侧面板(仿,不必然要一样,自己感觉看着不惬意的可以随意率性改,呵呵)截图:

二、编码详细实现

1、Layout结构文件(left_panel.xml)

xml version="1.0" encoding="utf-8"?>

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"

android:layout_height="fill_parent"android:orientation="vertical" >

LinearLayout

android:layout_width="fill_parent"android:layout_height="wrap_content"

android:background="@drawable/left_panel_top_bg"android:gravity="center_vertical"

android:orientation="horizontal"android:padding="7dip" >

ImageView

android:id="@+id/iv_user_icon"android:layout_width="48dip"

android:layout_height="48dip"android:scaleType="centerCrop"

android:src="@drawable/v5_0_1_widget_default_head" />

TextViewandroid:id="@+id/tv_nickname"

android:layout_width="wrap_content"android:layout_height="wrap_content"

android:layout_marginLeft="10dip"android:text="逐鹿。。。"

android:textColor="#FFFFF0"android:textSize="17sp" />

LinearLayout>

ExpandableListViewandroid:id="@+id/elv_list_view"

android:layout_width="fill_parent"android:layout_height="wrap_content"

android:layout_weight="1.0"android:cacheColorHint="#FFFFFF"

android:childIndicator="@null"android:divider="@drawable/v5_0_1_flipper_head_popup_menu_black_divider"

android:dividerHeight="1px"android:fadingEdge="none"

android:footerDividersEnabled="false"android:groupIndicator="@null"

android:headerDividersEnabled="false"android:listSelector="#00000000"

android:scrollbars="none" />

LinearLayout>

2、初始化组件,构建数据

初始化组件

ivUserIcon = (ImageView) findViewById(R.id.iv_user_icon);

tvNickname = (TextView) findViewById(R.id.tv_nickname);mExpandableListView = (ExpandableListView) findViewById(R.id.elv_list_view);

构建分组数据

Resources resources = this.getResources();

mGroupNames = resources.getStringArray(R.array.left_panel_group_names);

String[] firstGr亚虎官网登录oupNames = resources.getStringArray(R.array.left_panel_first_group_names);String[] secondGroupNames = resources.getStringArray(R.array.left_panel_second_group_names);

String[] threeGroupNames = resources.getStringArray(R.array.left_panel_group_three_names);

int[] firstGroupIcons = {R.drawable.left_panel_item_newsfeed_icon_selector,

R.drawable.left_panel_item_message_icon_selector,R.drawable.left_panel_item_chat_icon_selector,

R.drawable.left_panel_item_friends_icon_selector,R.drawable.left_panel_item_search_icon_selector};

int[] secondGroupIcons = {

R.drawable.left_panel_item_location_icon_selector,R.drawable.left_panel_item_mainpage_icon_selector,

R.drawable.left_panel_item_hot_icon_selector,R.drawable.left_panel_item_apps_icon_selector };

int[] threeGroupIcons = {

R.drawable.left_panel_item_settings_icon_selector,R.drawable.left_panel_item_layout_icon_selector };

addGroup(0, firstGroupNames, firstGroupIcons);

addGroup(1, secondGroupNames, secondGroupIcons);addGroup(2, threeGroupNames, threeGroupIcons);

3、自定类承袭BaseExpandableListAdapter,实现左侧面板ExpandableListView组件的数据适配器

package com.everyone.android.adapter;

import java.util.List;

import android.content.Context;

import android.graphics.Color;import android.util.TypedValue;

import android.view.Gravity;import android.view.View;

import android.view.ViewGroup;import android.widget.AbsListView;

import android.widget.BaseExpandableListAdapter;import android.widget.LinearLayout;

import android.widget.TextView;

import com.everyone.android.R;import com.everyone.android.entity.LeftPanelListItem;

/**

* 功能描述:左侧面板ExpandableListView组件数据适配器* @author android_ls

**/

public class LeftPanelExListViewAdapter extends BaseExpandableListAdapter {

private Context mContext;

private List mListItems;

public LeftPanelExListViewAdapter(Context context, List listItems) {mContext = context;

mListItems = listItems;}

public LeftPanelListItem getChild(int groupPosition, int childPosition) {

return mListItems.get(groupPosition).getGroups().get(childPosition);}

public long getChildId(int groupPosition, int childPosition) {

return childPosition;}

public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

ViewHolder viewHolder = null;if (convertView == n亚虎官网登录ull) {

convertView = getItemLayout(80, R.drawable.left_panel_item_selector, 18, Color.GRAY, 30);

viewHolder = new ViewHolder();viewHolder.groupN亚虎官网登录ame = (TextView) convertView.findViewById(0);

convertView.setTag(viewHolder);

} else {viewHolder = (ViewHolder) convertView.getTag();

}

LeftPanelListItem listItem = getChild(groupPosition, childPosition);viewHolder.groupName.setCompoundDrawablesWithIntrinsicBounds(listItem.getDrawableId(), 0, 0, 0);

viewHolder.groupName.setCompoundDrawablePadding(10);viewHolder.groupName.setText(listItem.getName());

return convertView;

}

public int getChildrenCount(int groupPosition) {return mListItems.get(groupPosition).getGroups().size();

}

public LeftPanelListItem getGroup(int groupPosition) {return mListItems.get(groupPosition);

}

public int getGroupCount() {return mListItems.size();

}

public long getGroupId(int groupPosition) {return groupPosition;

}

public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {ViewHolder viewHolder = null;

if (convertView == null) {convertView = getItemLayout(60, R.drawable.v5_0_1_desktop_list_item, 15, Color.WHITE, 20);

viewHolder = new ViewHolder();

viewHolder.groupName = (TextView) convertView.findViewById(0);

convertView.setTag(viewHolder);} else {

viewHolder = (ViewHolder) convertView.getTag();}

viewHolder.groupName.setText(getGroup(groupPosition).getName());

return convertView;}

public boolean hasStableIds() {

return false;}

public boolean isChildSelectable(int groupPosition, int childPosition) {

return true;}

static class ViewHolder {

public TextView groupName;}

/**

* 根据参数设置设置设备摆设摆设获取响应的Layout* @param height Layout高度

* @param backgroundId Layout的背景图片ID* @param textSize 字体大年夜小

* @param txetColor 字体颜色* @param padding 翰墨间隔左边的大年夜小(间距)

* @return LinearLayout*/

private LinearLayout getItemLayout(int height, int backgroundId, int textSize, int txetColor, int padding) {LinearLayout layout = new LinearLayout(mContext);

AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, height);layout.setBackgroundResource(backgroundId);

layout.setGravity(Gravity.CENTER_VERTICAL);layout.setLayoutParams(lp);

TextView textView = new TextView(mContext);

textView.setId(0);textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);textView.setTextColor(txetColor);

textView.setPadding(padding, 0, 0, 0);layout.addView(textView);

return layout;

}

}

4、给ExpandableListView设置数据适配器,并默认让所有组都展开

mExListViewAdapter = new LeftPanelExListViewAdapter(this, mListItems);

mExpandableListView.setAdapter(mExListViewAdapter);

// 设置默认让所有组都展开for (int i = 0; i

mExpandableListView.expandGroup(i);}

四、完备代码

strings.xml文件

xml version="1.0" encoding="utf-8"?>

resources>string name="app_name">CopyEveryonestring>

string-array name="left_panel_group_names">

item>常用item>item>更多item>

item>操作item>string-array>

string-array name="left_panel_first_group_names">item>新鲜事item>

item>消息item>item>谈天item>

item>石友item>item>找人item>

string-array>string-array name="left_panel_second_group_names">

item>相近item>item>公共主页item>

item>热门分享item>item>利用与游戏item>

string-array>string-array name="left_panel_group_three_names">

item>选项item>item>注销登录item>

string-array>

resources>

左侧面板的Item信息实体类

package com.everyone.android.entity;

import java.util.ArrayList;

/**

* 功能描述:左侧面板Item信息* @author android_ls

**/

public class LeftPanelListItem {

private int id; // 独一标识

private String name; // 操作项名称

private int drawableId; // 唆使图标 #R.drawable.ic_launcher

private ArrayList groups;

public int getId() {return id;

}

public void setId(int id) {this.id = id;

}

public String getName() {return name;

}

public void setName(String name) {this.name = name;

}

public int getDrawableId() {return drawableId;

}

public void setDrawableId(int drawableId) {this.drawableId = drawableId;

}

public ArrayList getGroups() {return groups;

}

public void setGroups(ArrayList groups) {this.groups = groups;

}

}

仿各人主界面之左侧面板(LeftPanelActivity类)源码:

package com.everyone.android.ui;

import java.util.ArrayList;

import java.util.List;

import android.content.res.Resources;import android.os.Bundle;

import android.widget.ExpandableListView;import android.widget.ImageView;

import android.widget.TextView;

import com.everyone.android.AppBaseActivity;import com.everyone.android.R;

import com.everyone.android.adapter.LeftPanelExListViewAdapter;import com.everyone.android.entity.LeftPanelListItem;

/奸淫 功能描述:仿各人主界面之左侧面板

* @author android_ls*

*/ public class LeftPanelActivity extends AppBaseActivity {

/**

* 用户图标显示组件*/

private ImageView ivUserIcon;

/奸淫 用户名称显示组件

*/private TextView tvNickname;

/**

* 可展开的ListView组件*/

private ExpandableListView mExpandableListView;

/奸淫 ExpandableListView组件的数据适配器

*/private LeftPanelExListViewAdapter mExListViewAdapter;

/**

* ExpandableListView组件的数据源*/

private List mListItems = new ArrayList();

/奸淫 分组名数组

*/private String[] mGroupNames;

@Override

public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);

}

@Override

protected void setupView() {ivUserIcon = (ImageView) findViewById(R.id.iv_user_icon);

tvNickname = (TextView) findViewById(R.id.tv_nickname);mExpandableListView = (ExpandableListView) findViewById(R.id.elv_list_view);

}

@Override

protected int getLayoutId() {return R.layout.left_panel;

}

@Overrideprotected void initializedData() {

Resources resources = this.getResources();mGroupNames = resources.getStringArray(R.array.left_panel_group_names);

String[] firstGroupNames = resources.getStringArray(R.array.left_panel_first_group_names);

String[] secondGroupNames = resources.getStringArray(R.array.left_panel_second_group_names);String[] threeGroupNames = resources.getStringArray(R.array.left_panel_group_three_names);

int[] firstGroupIcons = {

R.drawable.left_panel_item_newsfeed_icon_selecto亚虎官网登录r,R.drawable.left_panel_item_message_icon_selector,

R.drawable.left_panel_item_chat_icon_selector,R.drawable.left_panel_item_friends_icon_selector,

R.drawable.left_panel_item_search_icon_selector};

int[] secondGroupIcons = {R.drawable.left_panel_item_location_icon_selector,

R.drawable.left_panel_item_mainpage_icon_selector,R.drawable.left_panel_item_hot_icon_selector,

R.drawable.left_panel_item_apps_icon_selector };

int[] threeGroupIcons = {R.drawable.left_panel_item_settings_icon_selector,

R.drawable.left_panel_item_layout_icon_selector };

addGroup(0, firstGroupNames, firstGroupIcons);addGroup(1, secondGroupNames, secondGroupIcons);

addGroup(2, threeGroupNames, threeGroupIcons);

mExListViewAdapter = new LeftPanelExListViewAdapter(this, mListItems);mExpandableListView.setAdapter(mExListViewAdapter);

// 设置默认让所有组都展开

for (int i = 0; imEx亚虎官网登录pandableListView.expandGroup(i);

}

}

/奸淫 添加数据到指定的组

* @param groupId 组ID* @param names 子项的名字数组

* @param icons 子项的图标数组*/

private void addGroup(int groupId, String[] names, int[] icons) {LeftPanelListItem listItem = new LeftPanelListItem();

listItem.setId(groupId);listItem.setName(mGroupNames[groupId]);

// 组没有操作唆使图标// listItem.setDrawableId(drawableId);

ArrayList firstGroup = new ArrayList();

for (int i = 0; iLeftPanelListItem firstGroupItem = new LeftPanelListItem();

firstGroupItem.setId(i);firstGroupItem.setName(names[i]);

firstGroupItem.setDrawableId(icons[i]);

// 可以无限延伸// firstGroupItem.setGroups(null);

firstGroup.add(firstGroupItem);}

listItem.setGroups(firstGroup);

mListItems.add(listItem);}

}

转自:http://blog.csdn.net/android_ls/article/details/8749188

免责声明:以上内容源自网络,版权归原作者所有,如有侵犯您的原创版权请告知,我们将尽快删除相关内容。

您可能还会对下面的文章感兴趣: