91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

完美實現ExpandableListView二級分欄效果

發布時間:2020-09-28 03:04:04 來源:腳本之家 閱讀:272 作者:帝乙巖 欄目:移動開發

本文實例為大家分享了ExpandableListView二級分欄效果的具體代碼,供大家參考,具體內容如下

對ExpandableListView控件進行封裝(未自定義)直接上代碼:

通用ViewHolder類,僅在setImageResource中添加代碼

package com.svp.haoyan.expandablelistview.tool;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by haoyan on 2016/3/24.
 */
public class ViewHolder {
 //存儲數據
 private SparseArray<View> mViews;

 private int mPosition;
 private View mConvertView;
 private boolean misExpanded;
 public ViewHolder(Context context, ViewGroup parent, int layoutId, int position,boolean isExpanded) {

  this.mPosition=position;
  this.mViews= new SparseArray<View>();
  this.misExpanded=isExpanded;
  //三
  mConvertView= LayoutInflater.from(context).inflate(layoutId,parent,false);
  //一
  mConvertView.setTag(this);
 }
 public static ViewHolder get(Context context,View convertView,
         ViewGroup parent,int layoutId,int position,boolean isExpanded){

  if (convertView==null){
   return new ViewHolder(context,parent,layoutId,position,isExpanded);
  }else {
   //五 六
   ViewHolder holder=(ViewHolder)convertView.getTag();
   holder.mPosition=position;
   return holder;
  }
 }
 //通過viewId獲取控件
 //泛型T返回子類
 public <T extends View> T getView(int viewId){

  View view =mViews.get(viewId);
  if (view==null){
   //四
   view=mConvertView.findViewById(viewId);
   mViews.put(viewId,view);
  }
  return (T)view;
 }
 //八
 public View getmConvertView() {
  return mConvertView;
 }

 /**
  * 設置TextView的值
  * @param viewId
  * @param text
  * @return
  */
 //四 七 九
 public ViewHolder setText(int viewId,String text){
  TextView tv= getView(viewId);
  tv.setText(text);
  return this;
 }
 /**
  * 一參傳id,二參傳圖片地址,三參為true則調用點擊變化效果,默認為false.
  * */
 public ViewHolder setImageResource(int viewId,int resId,boolean expanded){
  ImageView view= getView(viewId);
  view.setImageResource(resId);
  if (expanded){
   if (misExpanded){
    view.setVisibility(View.GONE);
   }else {
    view.setVisibility(View.VISIBLE);
   }
  }
  return this;
 }
 public ViewHolder setImageBitamp(int viewId,Bitmap bitmap){
  ImageView view= getView(viewId);
  view.setImageBitmap(bitmap);
  return this;
 }
 public ViewHolder setImageURI(int viewId,String uri){
  ImageView view= getView(viewId);
//  Imageloader.getInstance().loadImg(view,uri);
  return this;
 }

}

實體類Expand_DTO:

package com.svp.haoyan.expandablelistview.dto;

/**
 * Created by haoyan on 2016/5/7.
 */
public class Expand_DTO {

 private String name;
 private String contents;
 private int pic;
 private int photo;

 public static class Expand_child {
  private String childname;
  private String childcont;

  public String getChildname() {
   return childname;
  }

  public void setChildname(String childname) {
   this.childname = childname;
  }

  public String getChildcont() {
   return childcont;
  }

  public void setChildcont(String childcont) {
   this.childcont = childcont;
  }
 }
 public String getName() {
  return name;
 }

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

 public String getContents() {
  return contents;
 }

 public void setContents(String contents) {
  this.contents = contents;
 }

 public int getPic() {
  return pic;
 }

 public void setPic(int pic) {
  this.pic = pic;
 }

 public int getPhoto() {
  return photo;
 }

 public void setPhoto(int photo) {
  this.photo = photo;
 }


}

接下來是最主要的Adapter:

package com.svp.haoyan.expandablelistview;

import android.content.Context;
import android.database.DataSetObserver;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListAdapter;

import com.svp.haoyan.expandablelistview.tool.ViewHolder;

import java.util.List;
import java.util.Map;

/**
 * Created by haoyan on 2016/4/28.
 */
public abstract class MyExpandableListAdapter<T,F>implements ExpandableListAdapter {
 private Context mcontext;
 private List<T> mexpand_dtos;
 private Map<Integer, List<F>> mmap = null;
 public MyExpandableListAdapter(Context context, List<T> expand_dtos,Map<Integer, List<F>> map) {
  this.mcontext = context;
  this.mexpand_dtos = expand_dtos;
  this.mmap=map;
 }
 /*注冊一個觀察者(observer),當此適配器數據修改時即調用此觀察者。*/
 @Override
 public void registerDataSetObserver(DataSetObserver observer) {

 }
 /*取消先前通過registerDataSetObserver(DataSetObserver)方式注冊進該適配器中的觀察者對象*/
 @Override
 public void unregisterDataSetObserver(DataSetObserver observer) {

 }
 /*獲取組的個數*/
 @Override
 public int getGroupCount() {
  return mexpand_dtos.size();
 }
 /*返回在指定Group的Child數目。*/
 @Override
 public int getChildrenCount(int groupPosition) {
  return mmap.get(groupPosition).size();
 }
 //獲取當前父item的數據
 @Override
 public T getGroup(int groupPosition) {
  return mexpand_dtos.get(groupPosition);
 }
 /*獲取與在指定group給予child相關的數據。*/
 @Override
 public F getChild(int groupPosition, int childPosition) {
  return (mmap.get(groupPosition).get(childPosition));
 }
 /*獲取指定組的ID*/
 @Override
 public long getGroupId(int groupPosition) {
  return groupPosition;
 }
 //得到子item的ID
 @Override
 public long getChildId(int groupPosition, int childPosition) {
  return childPosition;
 }
 //組和子元素是否持有穩定的ID,也就是底層數據的改變不會影響到它們。(沒效果)
 @Override
 public boolean hasStableIds() {
  return true;
 }
 //設置父item組件
 @Override
 public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
  ViewHolder viewHolder = ViewHolder.get(mcontext, convertView, parent, R.layout.expand_groupview, groupPosition,isExpanded);
  convert(viewHolder, getGroup(groupPosition));
  return viewHolder.getmConvertView();
 }
 //自己寫
 public abstract void convert(ViewHolder viewHolder, T t);

 public abstract void convertchild(ViewHolder viewHolder, F f);
 //設置子item的組件
 @Override
 public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
  ViewHolder viewHolder = ViewHolder.get(mcontext, convertView, parent, R.layout.expand_childview, groupPosition,isLastChild);
  convertchild(viewHolder, getChild(groupPosition,childPosition));
  return viewHolder.getmConvertView();
  /*子元素是否處于組中的最后一個(對每個組的最后一個進行操作)*/
  //boolean isLastChild
  /*下邊是未封裝代碼,提供參考*/
//  String Childname = (mmap.get(groupPosition).get(childPosition)).getChildname();
//  LayoutInflater inflater = (LayoutInflater)mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//  convertView = inflater.inflate(R.layout.expand_childview, null);
//  TextView tv_child = (TextView) convertView.findViewById(R.id.tv_child);
//  tv_child.setText(Childcont);
 }
 /*是否選中指定位置上的子元素。*/
 @Override
 public boolean isChildSelectable(int groupPosition, int childPosition) {
  return true;
 }
 /*true所有條目可以選擇和點擊*/
 @Override
 public boolean areAllItemsEnabled() {
  return false;
 }
 /*如果當前適配器不包含任何數據則返回True。經常用來決定一個空視圖是否應該被顯示。
 一個典型的實現將返回表達式getCount() == 0的結果,但是由于getCount()包含了頭部和尾部,適配器可能需要不同的行為。*/
 @Override
 public boolean isEmpty() {
  return false;
 }
 /*當組展開狀態的時候此方法被調用。*/
 @Override
 public void onGroupExpanded(int groupPosition) {

 }
 /*當組收縮狀態的時候此方法被調用。*/
 @Override
 public void onGroupCollapsed(int groupPosition) {

 }
 /*根據所給的子ID號和組ID號返回唯一的ID。此外,若hasStableIds()是true,那么必須要返回穩定的ID。*/
 @Override
 public long getCombinedChildId(long groupId, long childId) {
  return 0;
 }
 /*同上*/
 @Override
 public long getCombinedGroupId(long groupId) {
  return 0;
 }
}

最后是調用類Activity:

package com.svp.haoyan.expandablelistview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ExpandableListView;

import com.svp.haoyan.expandablelistview.dto.Expand_DTO;
import com.svp.haoyan.expandablelistview.tool.ViewHolder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MainActivity extends AppCompatActivity {
 private ExpandableListView elistview;
 private List<Expand_DTO> expanddata;
 private HashMap<Integer, List<Expand_DTO.Expand_child>> map = null;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  dataView();
  expandView();
 }

 private void expandView() {
  elistview = (ExpandableListView) findViewById(R.id.expandable_lv);
  elistview.setAdapter(new MyExpandableListAdapter<Expand_DTO,Expand_DTO.Expand_child>(this, expanddata,map) {
   @Override
   public void convert(ViewHolder viewHolder, Expand_DTO expand_dto) {
    viewHolder.setText(R.id.tv_expand, expand_dto.getName())
      .setText(R.id.tv_expand_two, expand_dto.getContents())
      .setImageResource(R.id.img_expand, expand_dto.getPic(),false)
      .setImageResource(R.id.img_expand_two, expand_dto.getPhoto(),false);
   }

   @Override
   public void convertchild(ViewHolder viewHolder, Expand_DTO.Expand_child expand_child) {
    viewHolder.setText(R.id.tv_child,expand_child.getChildname())
       .setText(R.id.tv_child2,expand_child.getChildcont());
   }
  });
  //去掉自帶箭頭
  elistview.setGroupIndicator(null);
 }

 private void dataView() {
  expanddata = new ArrayList<Expand_DTO>();
  Expand_DTO bean_zero = new Expand_DTO();
  bean_zero.setName("android新技能1");
  bean_zero.setContents("1");
  bean_zero.setPic(R.drawable.icon_check);
  bean_zero.setPhoto(R.drawable.travel_airfare_cn_position);
  expanddata.add(bean_zero);

  Expand_DTO bean_one = new Expand_DTO();
  bean_one.setName("android新技能2");
  bean_one.setContents("2");
  bean_one.setPic(R.drawable.icon_check);
  bean_one.setPhoto(R.drawable.travel_airfare_cn_position);
  expanddata.add(bean_one);

  Expand_DTO bean_two = new Expand_DTO();
  bean_two.setName("android新技能3");
  bean_two.setContents("3");
  bean_two.setPic(R.drawable.icon_check);
  bean_two.setPhoto(R.drawable.travel_airfare_cn_position);
  expanddata.add(bean_two);

  //這里可以使用SparseArray優化性能
  map = new HashMap<Integer, List<Expand_DTO.Expand_child>>();
  List<Expand_DTO.Expand_child> expand_children_zero=new ArrayList<Expand_DTO.Expand_child>();
  Expand_DTO.Expand_child child1=new Expand_DTO.Expand_child();
  child1.setChildname("11111");
  child1.setChildcont("22222");
  expand_children_zero.add(child1);

  Expand_DTO.Expand_child child2=new Expand_DTO.Expand_child();
  child2.setChildname("3333333");
  child2.setChildcont("4444444");
  expand_children_zero.add(child2);
  map.put(0,expand_children_zero);

  List<Expand_DTO.Expand_child> expand_children_one=new ArrayList<Expand_DTO.Expand_child>();
  Expand_DTO.Expand_child child3=new Expand_DTO.Expand_child();
  child3.setChildname("55555");
  child3.setChildcont("66666");
  expand_children_one.add(child3);

  Expand_DTO.Expand_child child4=new Expand_DTO.Expand_child();
  child4.setChildname("77777");
  child4.setChildcont("88888");
  expand_children_one.add(child4);
  map.put(1,expand_children_one);

  List<Expand_DTO.Expand_child> children_data_two=new ArrayList<Expand_DTO.Expand_child>();
  Expand_DTO.Expand_child child5=new Expand_DTO.Expand_child();
  child5.setChildname("99999");
  child5.setChildcont("00000");
  children_data_two.add(child5);

  Expand_DTO.Expand_child child6=new Expand_DTO.Expand_child();
  child6.setChildname("12345");
  child6.setChildcont("54321");
  children_data_two.add(child6);
  map.put(2,children_data_two);

 }
}

完活布局很簡單就不貼出來了,加上圖片有圖有真相:

完美實現ExpandableListView二級分欄效果

源碼下載:ExpandableListView二級分欄效果

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

慈利县| 南漳县| 邵阳县| 新巴尔虎右旗| 吕梁市| 昌乐县| 团风县| 无棣县| 含山县| 澜沧| 利津县| 长海县| 伊通| 蕲春县| 额敏县| 炎陵县| 区。| 青田县| 平乐县| 康乐县| 玉屏| 句容市| 大悟县| 临高县| 长阳| 凯里市| 福泉市| 邯郸市| 邮箱| 措美县| 宜宾市| 巧家县| 海口市| 青川县| 桦川县| 大城县| 黔西县| 泸溪县| 涿州市| 永兴县| 镇赉县|