时光轴三之 ExpandableListView版时光轴效果

本文介绍如何利用Android中的ExpandableListView实现时光轴布局。通过详细的代码示例,包括XML布局文件、自定义适配器及实体类的创建过程,帮助读者快速掌握实现技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

             上两篇讲到了用listViewrecyclerView来实现时光轴,这一篇我们用ExpandableListView来实现时光轴,废话不多说,直接来代码。

      还是先activity_main.xml

<?

xml version = "1.0" encoding = "utf-8" ?> < RelativeLayout xmlns :android = "http://schemas.android.com/apk/res/android" android :layout_width = "fill_parent" android :layout_height = "fill_parent" android :background = "#f7f7f7" > <View android :id = "@+id/top_line" android :layout_width = "match_parent" android :layout_height = "1dp" android :background = "@color/head_line_bg" /> <RelativeLayout android :layout_width = "fill_parent" android :layout_height = "fill_parent" android :layout_below = "@id/top_line" > <View android :id = "@+id/group_tiao" android :layout_width = "1dp" android :layout_height = "fill_parent" android :layout_marginLeft = "55dp" android :background = "@color/time_line_bg" /> <ExpandableListView android :id = "@+id/expandlist" android :layout_width = "fill_parent" android :layout_height = "fill_parent" android :layout_below = "@+id/courses_title" android :cacheColorHint = "#00000000" android :divider = "@null" /> </RelativeLayout > </RelativeLayout >
<?

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

>

<LinearLayout xmlns :android = "http://schemas.android.com/apk/res/android" android :layout_width = "match_parent" android :layout_height = "match_parent" android :gravity = "center_vertical" > <ImageView android :id = "@+id/imageView1" android :layout_width = "20dp" android :layout_height = "20dp" android :layout_gravity = "center_vertical" android :layout_marginLeft = "45dp" android :layout_marginRight = "5dp" android :background = "@drawable/img_line_point" android :contentDescription = "@string/app_name" /> <LinearLayout android :id = "@+id/linearLayout1" android :layout_width = "fill_parent" android :layout_height = "wrap_content" android :layout_marginBottom = "20dip" android :layout_marginTop = "20dp" android :gravity = "center_vertical" android :orientation = "vertical" > < TextView android :id = "@+id/one_status_name" android :layout_width = "wrap_content" android :layout_height = "wrap_content" android :text = "@string/app_name" android :textColor = "#000000" android :textSize = "18sp" /> </LinearLayout > </LinearLayout >

非常easy的布局也就是一个组标题。

接着是child_status_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:gravity="center_vertical" >
<ImageView
         android:padding="8dp"
        android:id="@+id/img"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_marginLeft="50dp"
        android:scaleType="fitXY" />

<TextView
         android:id="@+id/content_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="6dp"
        android:layout_marginLeft="70dp"
        android:layout_marginTop="6dp"
        android:textColor="#999999" />
</LinearLayout>

非常easy就是左边图片右边文字。

然后来看代码,先来数据适配器

       package com.zy.adapter;
        import java.util.List;
        import android.content.Context;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.view.ViewGroup;
        import android.widget.BaseExpandableListAdapter;
        import android.widget.ImageView;
        import android.widget.TextView;
        import com.zy.R;
        import com.zy.entity.ChildStatusEntity;
        import com.zy.entity.GroupStatusEntity;
        import com.zy.entity.TimeFormat;
public class StatusExpandAdapter extends BaseExpandableListAdapter {
    private LayoutInflater inflater = null;
    private List<GroupStatusEntity> groupList;


    /**
     * 构造方法
     *
     * @param context
     * @param oneList
     */
    public StatusExpandAdapter(Context context,
                               List<GroupStatusEntity> group_list) {
        this.groupList = group_list;
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }


    /**
     * 返回一级Item总数
     */
    @Override
    public int getGroupCount() {

        return groupList == null ? 0 : groupList.size();
    }


    /**
     * 返回二级Item总数
     */
    @Override
    public int getChildrenCount(int groupPosition) {
        return groupList == null ? 0
                : (groupList.get(groupPosition) == null ?

0 : (groupList .get(groupPosition) .getChildList() == null ? 0 : groupList .get(groupPosition) .getChildList() .size())) ; } /** * 获取一级Item内容 */ @Override public Object getGroup( int groupPosition) { // TODO Auto-generated method stub return groupList .get(groupPosition) ; } /** * 获取二级Item内容 */ @Override public Object getChild( int groupPosition , int childPosition) { return groupList .get(groupPosition) .getChildList() .get(childPosition) ; } @Override public long getGroupId( int groupPosition) { // TODO Auto-generated method stub return groupPosition ; } @Override public long getChildId( int groupPosition , int childPosition) { return childPosition ; } @Override public boolean hasStableIds() { return false ; } @Override public View getGroupView( int groupPosition , boolean isExpanded , View convertView , ViewGroup parent) { GroupViewHolder holder = new GroupViewHolder() ; if (convertView == null) { convertView = inflater .inflate(R .layout .group_status_item , null) ; } holder .groupName = (TextView) convertView .findViewById(R .id .one_status_name) ; holder .groupName .setText(TimeFormat .format( "yyyy.MM.dd" ,groupList .get(groupPosition) .getGroupName())) ; return convertView ; } @Override public View getChildView( int groupPosition , int childPosition , boolean isLastChild , View convertView , ViewGroup parent) { ChildViewHolder viewHolder = null ; ChildStatusEntity entity = (ChildStatusEntity) getChild(groupPosition , childPosition) ; if (convertView != null) { viewHolder = (ChildViewHolder) convertView .getTag() ; } else { viewHolder = new ChildViewHolder() ; convertView = inflater .inflate(R .layout .child_status_item , null) ; viewHolder .content_text = (TextView) convertView .findViewById(R .id .content_text) ; viewHolder .img =(ImageView) convertView .findViewById(R .id .img) ; } viewHolder .content_text .setText(entity .getContentText()) ; viewHolder .img .setImageResource(entity .getImgSrc()) ; convertView .setTag(viewHolder) ; return convertView ; } @Override public boolean isChildSelectable( int groupPosition , int childPosition) { return true ; } private class GroupViewHolder { TextView groupName ; } private class ChildViewHolder { public TextView content_text ; public ImageView img ; } } 这就是简单的ExpandableListView的适配器的写法,不明确的自己去查api咯。 然后里面封装了2个实体对象,当然也就是group和child的类对象: package com .zy .entity ; import java .util .List ; /** * 一级Item实体类 * * */ public class GroupStatusEntity { private String groupName ; /** 二级Item数据列表 **/ private List <ChildStatusEntity > childList ; public String getGroupName() { return groupName ; } public void setGroupName(String groupName) { this .groupName = groupName ; } public List <ChildStatusEntity > getChildList() { return childList ; } public void setChildList(List <ChildStatusEntity > childList) { this .childList = childList ; } } package com .zy .entity ; /** * 二级Item实体类 * * @author zihao * */ public class ChildStatusEntity { /** 估计完毕时间 **/ private String contentText ; /** 是否已完毕 **/ private boolean isfinished ; private int imgSrc ; public int getImgSrc() { return imgSrc ; } public void setImgSrc( int imgSrc) { this .imgSrc = imgSrc ; } public String getContentText() { return contentText ; } public void setContentText( String contentText) { this . contentText = contentText ; } public boolean isIsfinished() { return isfinished ; } public void setIsfinished( boolean isfinished) { this . isfinished = isfinished ; } }

哈哈接下来是mainActivity咯;

       package com.zy;
        import java.util.ArrayList;
        import java.util.Collections;
        import java.util.List;
        import android.app.Activity;
        import android.content.Context;
        import android.os.Bundle;
        import android.view.View;
        import android.widget.ExpandableListView;
        import android.widget.ExpandableListView.OnGroupClickListener;
        import android.widget.ExpandableListView.OnGroupExpandListener;
        import com.zy.R;
        import com.zy.adapter.StatusExpandAdapter;
        import com.zy.entity.ChildStatusEntity;
        import com.zy.entity.DateComparator;
        import com.zy.entity.GroupStatusEntity;
public class MainActivity extends Activity {
    private ExpandableListView expandlistView;
    private StatusExpandAdapter statusAdapter;
    private Context context;
    private int count = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        expandlistView = (ExpandableListView) findViewById(R.id.expandlist);
        initExpandListView();
    }

    /**
     * 初始化可拓展列表
     */
    private void initExpandListView() {
        statusAdapter = new StatusExpandAdapter(context, getListData());
        expandlistView.setAdapter(statusAdapter);
        expandlistView.setGroupIndicator(null); // 去掉默认带的箭头
// expandlistView.setSelection(0);// 设置默认选中项

// 遍历所有group
        int groupCount = expandlistView.getCount();
        expandlistView.expandGroup(0);
        for (int i = 0; i < groupCount; i++) {
            if (i <= 1) {
                expandlistView.expandGroup(i);
            }
        }

        expandlistView.setOnGroupClickListener(new OnGroupClickListener() {


            @Override
            public boolean onGroupClick(ExpandableListView parent, View v,
                                        int groupPosition, long id) {
                return false;
            }
        });
        expandlistView.setOnGroupExpandListener(new OnGroupExpandListener() {

            @Override
            public void onGroupExpand(int groupPosition) {
                count++;
                for (int i = 0, count = expandlistView
                        .getExpandableListAdapter().getGroupCount(); i < count; i++) {
                    if (groupPosition != i && count > 2) {// 关闭其它分组
                        expandlistView.collapseGroup(i);
                        count = 1;
                    }
                }
            }
        });
    }

    private List<GroupStatusEntity> getListData() {
        List<GroupStatusEntity> groupList;
        String[] strArray = new String[] { "20140710", "20081201", "20150809" };
        String[][] childTimeArray = new String[][] {
                {
                        "敬往事一杯酒,再爱也不回头",
                        "择一城终老,遇一人白首。",
                        "有时候邀女生出来玩她拒绝你的原因仅仅有两个,一是她懒得洗头,二是你的邀请不值得她洗头。

女生非要约人出来也有两个原因,一是她洗了头不出来玩不甘心,二是突然非常想吃某家的东西。"

, "我见过千万人像你的发像你的眼却都不是你的脸。" } , { "你说长相不重要,是由于你长了一张就算刚睡醒也敢自拍的脸。你说成绩不重要,是由于你随随便便又不小心考了次年级前五。你说恋爱不重要。是由于你身边备胎多的能够摆四五桌麻将了。

你说家境不重要,是由于你有一个看你皱一下眉就给你买新款的父母。

你说健康不重要,是由于你不会半夜由于疼痛而翻来覆去咳得撕心裂肺。

你说不重要只是是由于你已经拥有了,你说不重要只是是由于你从来不知道别人的努力和挣扎。"

, "你永远不知道在你发了个“嗯”或者“哦”还能继续回复你的人。是有多在乎你!" , "最想说的话在眼睛里,草稿箱里。还有梦里" } , { "那些花了好久才想明确的事,总是会被偶尔的情绪失控所有推翻。

"

, "折磨人的不是离别,而是感动的回顾,让人非常easy站在原地还以为回得去" , "敬往事一杯酒,再爱也不回头!" , "能够一杯滚水烫死我,也能够一杯冰水冷死我,但不能一杯温水耗着我,我要的是黑白分明直接利落" } } ; groupList = new ArrayList <GroupStatusEntity >() ; for ( int i = 0 ; i < strArray .length ; i ++) { GroupStatusEntity groupStatusEntity = new GroupStatusEntity() ; groupStatusEntity .setGroupName(strArray[i]) ; List <ChildStatusEntity > childList = new ArrayList <ChildStatusEntity >() ; for ( int j = 0 ; j < childTimeArray[i] .length ; j ++) { ChildStatusEntity childStatusEntity = new ChildStatusEntity() ; childStatusEntity .setContentText(childTimeArray[i][j]) ; if (j % 3 == 0) { childStatusEntity .setImgSrc(R .drawable .one) ; } if (j % 3 == 1) { childStatusEntity .setImgSrc(R .drawable .two) ; } if (j % 3 == 2) { childStatusEntity .setImgSrc(R .drawable .three) ; } childStatusEntity .setIsfinished( true) ; childList .add( childStatusEntity) ; } groupStatusEntity .setChildList( childList) ; groupList .add( groupStatusEntity) ; } // 将数据依照时间排序 DateComparator comparator = new DateComparator() ; Collections .sort( groupList , comparator) ; return groupList ; } }

哈哈写完了,看下效果图:

              

马蛋。手机差截图不用愁了,用asm.jar来显示生成图片传上来清晰多了。

执行asm.jar后是这种效果:


asm.jar的下载地址http://download.youkuaiyun.com/download/lxq_xsyu/6666965,我但是下了好几个不能用的,说什么清单文件为空,醉了,步骤呢

1、将其copy到platform-tools文件夹下

2、执行java -jar asm.jar就可以启动

按右键就出现如此菜单选项:



图片就能够直接保存了(save image),zoom呢是设置屏幕的大小。哈哈哈。讲完了,又要去写代码了,最后附上源代码,想看下效果的能够去下一下。不要积分的http://download.youkuaiyun.com/detail/u013278099/8994581,哈哈时光轴系列的文章就写完了,认为能够的话就点个赞,不足之处给我指出,让我也多学习大家的知识。


转载于:https://www.cnblogs.com/zsychanpin/p/6952746.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值