Android组件ExpandableListView及其适配器,线程中的runOnUiThread()的使用

工作内容:

1.ExpandableListView 的学习,及其适配器继承自BaseExpandableListAdapter的自定义适配器

2.学了runOnUiThread()的简单使用(可以直接操作ui界面,短时间的)

3.获取系统的信息

学习分享:

一、ExpandableListView是继承自ListView的一个组件,主要体现方式是2ListView,外层是group,内层是child.

二、BaseExpandableListAdapter它的子类适配器,主要为ExpandableListView服务,主要要搞清楚内层和外层的数据如何去取,如何使用。

三、onUiThread存在于Ui线程中的一条线程,作用类似于Handler,做系统的Ui界面的修改

四、获取系统信息时,需在manifest中注册权限

常用的信息有Build打点就可以使用

实例:

//ExpandableListView 的适配器

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 java.util.List;
import java.util.Map;
import azsecuer.androidy.com.mobelmanager.R;
import azsecuer.androidy.com.mobelmanager.activity.dataclass.MobelCheckingListChild;
import azsecuer.androidy.com.mobelmanager.activity.dataclass.MobelCheckingListGroup;

public class HomeMobelCheckingExpandAdapter extends BaseExpandableListAdapter {
    private List<MobelCheckingListGroup> grouplist = null;//外层数据
    private Map<String,List<MobelCheckingListChild>> childmap=null;//内层数据
    private int resourceID;
    private Context context;
    private int resourceIDChild;
    private List<Integer> backgroundColorList;

    /**
     * 构造函数
     * @param context
     * @param resourceID group布局文件id
     * @param resourceIDChild 子布局文件id
     * @param grouplist group数据
     * @param childmap 子项数据
     * @param backgroundColorList group背景色数据—每一条都不同颜色
     */
    public HomeMobelCheckingExpandAdapter(Context context, int resourceID, int resourceIDChild,
                                          List<MobelCheckingListGroup> grouplist,
                                          Map<String, List<MobelCheckingListChild>> childmap,
                                          List<Integer> backgroundColorList) {
        this.grouplist = grouplist;
        this.childmap = childmap;
        this.resourceID = resourceID;
        this.context = context;
        this.resourceIDChild = resourceIDChild;
        this.backgroundColorList = backgroundColorList;
    }
    @Override
    public int getGroupCount() {
        return grouplist.size();
    }
    @Override
    public int getChildrenCount(int groupPosition) {
        return childmap.get(grouplist.get(groupPosition).getTitle()).size();
    }
    @Override
    public MobelCheckingListGroup getGroup(int groupPosition) {
        return grouplist.get(groupPosition);
    }
    @Override
    public MobelCheckingListChild getChild(int groupPosition, int childPosition) {
        return childmap.get(grouplist.get(groupPosition).getTitle()).get(childPosition);
    }
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }
    @Override
    public boolean hasStableIds() {
        //设置为true表示数据源刷新
        return true;
    }
    //获取group布局,并填充内容
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        MobelCheckingListGroup mobelCheckingListGroup= getGroup(groupPosition);
        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(resourceID,null);
        }
        convertView.setBackgroundColor(backgroundColorList.get(groupPosition));
//        convertView.setBackgroundColor(context.getResources().getColor(backgroundColor));
        ImageView imageView = (ImageView)convertView.findViewById(R.id.iv_list_expand_parent);
        imageView.setBackgroundResource(mobelCheckingListGroup.getImageViewID());
        TextView textView = (TextView)convertView.findViewById(R.id.textView_list_expand);
        textView.setText(getGroup(groupPosition).getTitle());
        return convertView;
    }
    //获取子布局,并填充内容
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(resourceIDChild,null);
        }
        TextView textView = (TextView)convertView.findViewById(R.id.textView_list_expand_child);
        TextView textView1 = (TextView)convertView.findViewById(R.id.textView1_list_expand_child);
        textView.setText(getChild(groupPosition,childPosition).getContent());
        textView1.setText(getChild(groupPosition,childPosition).getTitle());
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        //最后一项是否可选
        return false;
    }
}

实例 2 :runOnUiThread(){}的应用

 /**
     * 1.给list添加数据
     * 2.在runOnUiThread(new Runnable(){run(){...}}中更新主线程的adapter
     * @throws IOException
     */
    public void createDatabase() throws IOException{
        fileList = new ArrayList<>();
        tagFile = new File("/data/data/"+this.getPackageName()+"/databases/clearpath.db");
        if (!tagFile.exists()){
            tagFile.getParentFile().mkdirs();
            tagFile.createNewFile();
        }
        if(tagFile.length() == 0){
            FileManager.copyFile(this,"clearpath.db",tagFile);
        }
        new Thread(){
            @Override
            public void run() {
                /**
                 * tagFile  传入数据库文件的File对象
                 * fileList 装有可能需要删除的File对象
                 * 注意:如果您的list已经初始化,并已经设置进了adapter
                 * 这步使list变成了另外一个list,直接使用会导致listView加载不了数据
                 */
                list = MySQLiteTool.select(ClearMobelActivity.this,tagFile,fileList);
                //提醒handler去list更新完成需更新适配器
//                handler.sendEmptyMessage(0);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter = new ClearMobelAdapter(ClearMobelActivity.this,R.layout.list_clear_mobel,list);
                        listView.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                        listView.setVisibility(View.VISIBLE);
                        progressbar.setVisibility(View.INVISIBLE);
                    }
                });
            }
        }.start();
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值