带header的Adapter

2 篇文章 0 订阅
2 篇文章 0 订阅
package com.upTask.ut.uptask;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.BaseAdapter;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * header也能修改,数据部分也能修改
 */
public class ContactAdapter extends BaseAdapter {

    public final List<BaseAdapter> sections = new LinkedList<BaseAdapter>();
    public final List<String> headers=new LinkedList<>();
    public final static int TYPE_SECTION_HEADER = 0;
    private Context context;
    private LayoutInflater mInflater;

    public ContactAdapter(Context context) {
        this.context=context;
        mInflater = LayoutInflater.from(context);
    }

    public void addSection(String section, BaseAdapter adapter) {
        this.headers.add(section);
        this.sections.add(adapter);
    }
    public void insertSectionBefore(int i,String section, BaseAdapter adapter) {
        this.headers.add(i,section);
        this.sections.add(i,adapter);
    }
    public void replaceSectionData(int i, BaseAdapter adapter) {
        this.sections.remove(i);
        this.sections.add(i,adapter);
    }
    public void removeSection(int i) {
        this.sections.remove(i);
        this.headers.remove(i);
    }
    public void removeAllSections() {
        this.headers.clear();
        this.sections.clear();
    }
    public Object getItem(int position) {
        for (int i=0;i<sections.size();i++) {
            Adapter adapter = sections.get(i);
            String section = headers.get(i);
            int size = adapter.getCount();
            if(!"".equals(section)){
                size += 1;
            }
            // check if position inside this section
            if (position == 0&&!"".equals(section)) {
                return section;
            }
            if (position == 0){
                return adapter.getItem(0);
            }
            if (position < size&&!"".equals(section)) {
                return adapter.getItem(position - 1);
            }
            if (position < size&&"".equals(section)) {
                return adapter.getItem(position);
            }
            // otherwise jump into next section
            position -= size;
        }
        return null;
    }

    public int getCount() {
        // total together all sections, plus one for each section header
        int total = 0;
        for (int i=0;i<sections.size();i++) {
            Adapter adapter = sections.get(i);
            String section = headers.get(i);
            total += adapter.getCount();
            if(!"".equals(section)){
                total += 1;
            }
        }
        return total;
    }
    @Override
    public int getViewTypeCount() {
        // assume that headers count as one, then total all sections
        return 4;
    }

    public int getItemViewType(int position) {
        if(position==getCount()-1){
            return 3;
        }
        for (int i=0;i<sections.size();i++) {
            Adapter adapter = sections.get(i);
            String section = headers.get(i);
            int size = adapter.getCount();
            if(!"".equals(section)){
                size += 1;
            }
            // check if position inside this section
            if (position == 0&&!"".equals(section)) {
                return TYPE_SECTION_HEADER;
            }
            if(position == 0){
                if(i==0){
                    return 1;
                }else{
                    return 2;
                }
            }
            if (position < size&&!"".equals(section)) {
                if(i==0){
                    return 1;
                }else{
                    return 2;
                }
            }
            if (position < size&&"".equals(section)) {
                if(i==0){
                    return 1;
                }else{
                    return 2;
                }
            }

            // otherwise jump into next section
            position -= size;
        }
        return -1;
    }

    public boolean areAllItemsSelectable() {
        return false;
    }

    public boolean isEnabled(int position) {
        return (getItemViewType(position) != TYPE_SECTION_HEADER);
    }
    @Override
    public void notifyDataSetChanged(){
        for(BaseAdapter section:sections){
            section.notifyDataSetChanged();
        }
        super.notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(position==getCount()-1&&position>0){
            return mInflater.inflate(R.layout.contact_list_footer, parent, false);
        }
        for (int i=0;i<sections.size();i++) {
            BaseAdapter secAdapter = sections.get(i);
            String section = headers.get(i);
            int size = secAdapter.getCount();
            if(!"".equals(section)){
                size += 1;
            }
            // check if position inside this section
            if (position == 0&&!"".equals(section)) {
//                List<HashMap<String,Object>>datas=new LinkedList<>();
//                HashMap<String,Object>data=new HashMap<>();
//                data.put("title",section);
//                datas.add(data);
//                return new SimpleAdapter(context,datas,R.layout.contact_list_header,new String[]{"title"},new int[]{R.id.contact_list_header_title}).getView(0,convertView,parent);
                View view = mInflater.inflate(R.layout.contact_list_header, parent, false);
                TextView title=view.findViewById(R.id.contact_list_header_title);
                title.setText(section);
                return view;
            }
            if(position == 0){
                if(secAdapter.getCount()==0){
                    continue;
                }
                View view=secAdapter.getView(0, convertView, parent);
                setOnClickListener(view);
                return view;
            }
            if (position < size&&!"".equals(section)) {
                View view=secAdapter.getView(position - 1, convertView, parent);
                setOnClickListener(view);
                return view;
            }
            if (position < size&&"".equals(section)) {
                View view=secAdapter.getView(position, convertView, parent);
                setOnClickListener(view);
                return view;
            }
            // otherwise jump into next section
            position -= size;
        }
        return null;
    }
    private void setOnClickListener(View view){
        TextView sec1Title=view.findViewById(R.id.contact_sec1_title);
        if(sec1Title!=null&&"添加好友".equals(sec1Title.getText())){
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent=new Intent(view.getContext(),ChatActivity.class);
                    view.getContext().startActivity(intent);
                }
            });
            return;
        }
        if(sec1Title!=null&&"新建群组".equals(sec1Title.getText())){
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent=new Intent(view.getContext(),ChatActivity.class);
                    view.getContext().startActivity(intent);
                }
            });
            return;
        }
        if(sec1Title!=null&&"好友请求".equals(sec1Title.getText())){
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent=new Intent(view.getContext(),ChatActivity.class);
                    view.getContext().startActivity(intent);
                }
            });
            return;
        }
        final TextView friendId=view.findViewById(R.id.contact_sec2_friend_id);
        final TextView friendType=view.findViewById(R.id.contact_sec2_friend_type);
        final TextView sessionKey=view.findViewById(R.id.contact_sec2_session_key);
        if(friendId!=null){
            Toast.makeText(context,"点击了通讯录中的好友",Toast.LENGTH_SHORT);
            view.setOnClickListener(new View.OnClickListener() {
                private boolean clicked=false;
                @Override
                public void onClick(View view) {
                    if(clicked){
                        return;
                    }
                    clicked=true;
                    Intent intent=new Intent(view.getContext(),ChatActivity.class);
                    intent.putExtra("friendId",friendId.getText());
                    intent.putExtra("friendType",friendType.getText());
                    intent.putExtra("sessionKey",sessionKey.getText());
                    view.getContext().startActivity(intent);
                    new Timer().schedule(new TimerTask() {
                        @Override
                        public void run() {
                            clicked=false;
                        }
                    },1000);
                }
            });
            return;
        }

    }
    @Override
    public long getItemId(int position) {
        return position;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值