zoukankan      html  css  js  c++  java
  • 宋体效果ExpandableListView模拟QQ好友列表

    本篇文章朋友在上海逛街的时候突然想到的...这两天就有想写几篇关于宋体效果的笔记,所以回家到之后就奋笔疾书的写出来发表了

        

        ExpandableListView可开展的列表视图,本文我们要学习的是,如何应用这个件控,实现QQ挚友列表这样的效果。

         

        一、先学习理论知识

        public class

        ExpandableListView

        extends ListView

        

    java.lang.Object

     

    android.view.View

     

     

    android.view.ViewGroup

     

     

     

    android.widget.AdapterView<T extends android.widget.Adapter>

     

     

     

     

    android.widget.AbsListView

     

     

     

     

     

    android.widget.ListView

     

     

     

     

     

     

    android.widget.ExpandableListView

        

    ClassOverview

        Aview that shows items in a vertically scrolling two-level list. This differsfrom the ListViewby allowing two levels: groups which can individually be expanded to show itschildren. The items come from the ExpandableListAdapterassociated with this view.

        Expandablelists are able to show an indicator beside each item to display the item'scurrent state (the states are usually one of expanded group, collapsed group,child, or last child). Use setChildIndicator(Drawable) or setGroupIndicator(Drawable) (or the corresponding XML attributes) to set these indicators (see thedocs for each method to see additional state that each Drawable can have). Thedefault style for anExpandableListViewprovides indicators which will be shown next to Views given to the ExpandableListView.The layouts android.R.layout.simple_expandable_list_item_1 andandroid.R.layout.simple_expandable_list_item_2 (which should be used withSimpleCursorTreeAdapter) contain the preferred position information for indicators.

        Thecontext menu information set by an ExpandableListViewwill be a ExpandableListView.ExpandableListContextMenuInfo object withpackedPositionbeing a packed position that can be used with getPackedPositionType(long) and the other similar methods.

        Note: You cannot usethe value wrap_contentfor the android:layout_height attribute of a ExpandableListView in XML if the parent's size is also notstrictly specified (for example, if the parent were ScrollView you could notspecify wrap_content since it also can be any length. However, you can usewrap_content if the ExpandableListView parent has a specific size, such as 100pixels.

         

        二、 实际

         

        

        第一步计设面页

         

        因为我们继承了ExpandableList,所以我们不必要另外再计设一个序程界面,如果只是继承了Activity,那就必须计设一个面页,里头放上android:id="@+id/android:list"ExpandableListView。这里我们用到了自定义的子项目面页视图evchildview.xml

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

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

            android:orientation="vertical"

            android:layout_width="fill_parent"

            android:layout_height="fill_parent"

            >

            <ImageView android:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/imgHead"android:src="@drawable/icon" android:layout_alignParentTop="true"android:layout_alignParentLeft="true"></ImageView>

            <TextView android:layout_width="wrap_content"android:text="name"android:layout_height="wrap_content"android:id="@+id/tvName" android:layout_alignBottom="@+id/imgHead"android:layout_toRightOf="@+id/imgHead" android:layout_marginLeft="16dp" android:layout_marginBottom="15dp"></TextView>

        </RelativeLayout>

         

        第二步、计设Activity ExpandableListViewTest

        /**

         * ExpandableListView example

         */

        package com.figo.helloworld;

        import android.app.ExpandableListActivity;

        import android.os.Bundle;

        import android.view.View;

        import android.widget.ExpandableListView;

        import android.widget.TextView;

        import android.widget.Toast;

         

        /**

         * @author zhuzhifei

         * @版权所有

         */

        public class ExpandableListViewTest extends ExpandableListActivity {

         

           private ExpandableListView expandableListView;

         

           @Override

           protected void onCreate(Bundle savedInstanceState) {

              // TODO Auto-generatedmethod stub

              super.onCreate(savedInstanceState);

              //如果没有继承ExpandableListActivity采取法方一

              //setContentView(R.layout.expandablelistviewtest);

              //expandableListView=(ExpandableListView)findViewById(R.id.expandableListView1);

              //expandableListView.setAdapter(expandableListAdapterTest);

              //继承了ExpandableListActivity采取法方二

              ExpandableListAdapterTest expandableListAdapterTest = newExpandableListAdapterTest(

                   ExpandableListViewTest.this);

              setListAdapter(expandableListAdapterTest);

         

           }

         

           /**

            * 点击子项时,触发点击事件

            */

           @Override

           public boolean onChildClick(ExpandableListView parent, View v,

                int groupPosition, int childPosition, long id) {

              TextView tvName = (TextView) v.findViewById(R.id.tvName);

              Toast.makeText(ExpandableListViewTest.this, "name:" + tvName.getText(),

                   Toast.LENGTH_LONG).show();

              return super.onChildClick(parent, v, groupPosition, childPosition,id);

         

           }

         

           /**

            * 组合拢事件

            */

           @Override

           public void onGroupCollapse(int groupPosition) {

              // TODO Auto-generatedmethod stub

              super.onGroupCollapse(groupPosition);

           }

         

           /**

            * 组开展事件

            */

           @Override

           public void onGroupExpand(int groupPosition) {

              // TODO Auto-generatedmethod stub

              super.onGroupExpand(groupPosition);

           }

        }

         

         

            第三步、计设ExpandableListView的Adapter

        

        /**

         * ExpandableListView据数适配器

         */

        package com.figo.helloworld;

         

        import android.content.Context;

        import android.view.Gravity;

        import android.view.LayoutInflater;

        import android.view.View;

        import android.view.ViewGroup;

        import android.widget.AbsListView;

        import android.widget.BaseExpandableListAdapter;

        import android.widget.ImageView;

        import android.widget.TextView;

         

        /**

        每日一道理
    谁说人与人隔着遥远的重洋,谁说心与心设着坚固的堤防?十六岁的鸟儿飞上天空,总会找到落脚的枝头。

         * @author zhuzhifei

         * @版权所有

         */

        public class ExpandableListAdapterTest extends BaseExpandableListAdapter {

         

           public String[] groups = { "我的挚友", "复旦", "南大", "事同" };

           public String[][] children = { { "刘备", "曹操", "孙权", "奥巴马" },

                { "复复", "旦旦", "日月", "华光" }, { "阿南", "大大" }, { "马超", "赵云", "林丹" } };

           public int[][] figures = {

                { R.drawable.lb, R.drawable.cc, R.drawable.sq, R.drawable.abm },

                { R.drawable.ff, R.drawable.dd, R.drawable.ry, R.drawable.gh },

                { R.drawable.an, R.drawable.dada },

                { R.drawable.mc, R.drawable.zy, R.drawable.ld } };

           Context context;

         

           public ExpandableListAdapterTest(Context ct) {

              context = ct;

           }

         

           /*

            * 获得组数

            */

           @Override

           public int getGroupCount() {

              return groups.length;

           }

         

           /*

            * 获得以后组的子项

            */

           @Override

           public int getChildrenCount(int groupPosition) {

              return children[groupPosition].length;

           }

         

           /*

            * 获得以后组据数

            */

           @Override

           public Object getGroup(int groupPosition) {

              return groups[groupPosition];

           }

         

           /*

            * 获得以后子项据数

            */

           @Override

           public Object getChild(int groupPosition, int childPosition) {

              return children[groupPosition][childPosition];

           }

         

           /*

            * 获得组编号

            */

           @Override

           public long getGroupId(int groupPosition) {

              return groupPosition;

           }

         

           /*

            * 获得子项编号

            */

           @Override

           public long getChildId(int groupPosition, int childPosition) {

              return childPosition;

           }

         

           /*

            * 编号否是定稳

            */

           @Override

           public boolean hasStableIds() {

              return true;

           }

         

           /*

            * 获得组视图

            */

           @Override

           public View getGroupView(int groupPosition, boolean isExpanded,

                View convertView, ViewGroup parent) {

              TextView textView = getGenericView();

              textView.setText(getGroup(groupPosition).toString());

              return textView;

         

           }

         

           /*

            * 获得子视图,这里采取自定义的xml面页,含包一个图片和一个文本框

            *

            */

           @Override

           public View getChildView(int groupPosition, int childPosition,

                boolean isLastChild, View convertView, ViewGroup parent) {

              LayoutInflater inflater = LayoutInflater.from(context);

              View layout = inflater.inflate(R.layout.evchildview, null);

              TextView tvName = (TextView) layout.findViewById(R.id.tvName);

              tvName.setText(getChild(groupPosition,childPosition).toString());

              ImageView imgHead = (ImageView) layout.findViewById(R.id.imgHead);

              imgHead.setImageResource(getFigure(groupPosition,childPosition));

              return layout;

           }

         

           /*

            * 子项否是可选

            */

           @Override

           public boolean isChildSelectable(int groupPosition, int childPosition) {

              return true;

           }

         

           /**

            * 组视图,这里只显示一个单简的文本框

            *

            * @return

            */

           public TextView getGenericView() {

              AbsListView.LayoutParams lp = new AbsListView.LayoutParams(

                   ViewGroup.LayoutParams.MATCH_PARENT, 64);

              TextView textView = new TextView(context);

              textView.setLayoutParams(lp);

              textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);

              textView.setPadding(36, 0, 0, 0);

              return textView;

           }

            /**

             * 获得肖像编号

             * @param groupPosition

             * @param childPosition

             * @return

             */

           public int getFigure(int groupPosition, int childPosition) {

              return figures[groupPosition][childPosition];

        

           }

        }

         

         

        第四步、AndroidManifest.xml注册Activity

            <activity android:name=".ExpandableListViewTest"

                   android:label="@string/app_name">

                   <intent-filter>

                      <action android:name="android.intent.action.MAIN"/>

                      <category android:name="android.intent.category.LAUNCHER"/>

                   </intent-filter>

               </activity>

         

        第五步、运行效果

        合拢效果

        

        开展效果

        

        点击某项效果

         

    文章结束给大家分享下程序员的一些笑话语录: 苹果与谷歌之争就是封闭收费与自由免费思想之争。(别急着把google来膜拜哦?那可是一家公司,以赚钱为目标的公司!当年我Party就是这样把广大劳动人民吸引过来的。今天的结果你们都看到了。)

  • 相关阅读:
    向iphone模拟器中导入图片
    键盘样式风格设置及隐藏
    (转)iPhone图片处理:摄像头/相册获取图片,压缩图片,上传服务器,下载,拉伸,方法总结
    python通过get方式,post方式发送http请求和接收http响应-urllib urllib2
    Python中 如何使用telnet 检测端口是否通
    Linux运维人员共用root帐户权限审计
    python ssh登录
    欢迎来到 Flask 的世界
    大杂烩
    随笔
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/3050695.html
Copyright © 2011-2022 走看看