实现了 搜索及快速索引的 联系人列表页 效果图如下:
⊙ ------- 复制以下代码需要自己手动稍做修改
需要用到 pinyin4j 的jar包 里面的 PinYinUtils 类
以下代码稍做修改可实现的效果:
布局如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第三个fragment"/> <!--上面搜索框--> <LinearLayout android:layout_width="match_parent" android:layout_height="33dp" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" android:focusableInTouchMode="true" android:layout_marginTop="11dp" android:layout_marginBottom="5dp" android:background="@drawable/shape_indexbar_search_corners" android:focusable="true" android:gravity="center_vertical"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="18dp" android:layout_marginRight="8dp" android:src="@mipmap/search"/> <EditText android:id="@+id/et_search" android:singleLine="true" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="16dp" android:textColor="#999999" android:hint="搜索养老院、拼音首字母..." android:background="@null" /> </LinearLayout> <!--搜索结果的展示--> <include layout="@layout/search_result"/> <!--快速索引--> <RelativeLayout android:id="@+id/rl_all" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="match_parent" android:dividerHeight="1px" android:divider="@color/line_divider"/> <myapp.first.myapplication.view.QuickIndexBar android:id="@+id/quickindexbar" android:layout_width="30dp" android:layout_marginTop="18dp" android:layout_marginBottom="30dp" android:layout_height="match_parent" android:layout_alignParentRight="true" android:background="#00000000" /> <TextView android:id="@+id/tv_prompt" android:layout_width="80dp" android:layout_height="80dp" android:layout_centerInParent="true" android:background="@drawable/shape_indexbar_corners" android:gravity="center" android:text="A" android:textSize="24sp" android:visibility="gone" /> </RelativeLayout> </LinearLayout>
代码的实现:
对象----
package myapp.first.myapplication.bean; import myapp.first.myapplication.tools.PinYinUtils; /** * @Author Xi * Created by zz.wbkj.xi on 2016/11/7. */ public class DataBean implements Comparable<DataBean>{ private String beadhouse_id; private String beadhouse_name; public String pinyin; public DataBean(){ } public DataBean(String beadhouse_name){ this.beadhouse_name = beadhouse_name; this.pinyin = PinYinUtils.getPinYin(beadhouse_name); } public String getBeadhouse_id() { return beadhouse_id; } public void setBeadhouse_id(String beadhouse_id) { this.beadhouse_id = beadhouse_id; } public String getBeadhouse_name() { return beadhouse_name; } public void setBeadhouse_name(String beadhouse_name) { this.beadhouse_name = beadhouse_name; this.pinyin = PinYinUtils.getPinYin(beadhouse_name); } @Override public int compareTo(DataBean another) { return this.pinyin.compareTo(another.pinyin); } }
具体的实现代码:
package myapp.first.myapplication.ui; import android.text.Editable; import android.text.TextUtils; import android.text.TextWatcher; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.EditText; import android.widget.FrameLayout; import android.widget.ListView; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.Collections; import java.util.List; import myapp.first.myapplication.R; import myapp.first.myapplication.adapter.DefaultBaseAdapter; import myapp.first.myapplication.adapter.QuickIndexBarAdapter; import myapp.first.myapplication.base.MyBaseFragment; import myapp.first.myapplication.bean.DataBean; import myapp.first.myapplication.view.QuickIndexBar; /** * Created by Administrator on 2016/10/7 0007. */ public class FragmentRbtn3 extends MyBaseFragment { // 搜索框 private EditText et_search; // 搜索框下面的 相对布局 private RelativeLayout rl_all; // 快速索引 搜ABCDEFGH.... private QuickIndexBar quickIndexBar; // 展示所有内容的 listview private ListView listView; // 搜索过滤的 listview private ListView filter_listview; // 快速索引的提示块 屏幕中间显示的 A 等 private TextView tvPrompt; // 搜索无结果的提示 private TextView tv_search_no_result; // 搜索无结果与搜索后过滤的listview 的 根部局 private FrameLayout fl_result; // 数据集合 List<DataBean> dataBeanList = new ArrayList<>(); // 排序后的集合 List<DataBean> sortDataBeanList = new ArrayList<>(); // 搜索过滤后的数据集合 List<DataBean> filterDataList = new ArrayList<>(); @Override public void init() { } @Override public int getLayoutResID() { return R.layout.fragment_rbtn3; } @Override public void initView(View view) { et_search = (EditText)view.findViewById(R.id.et_search); rl_all = (RelativeLayout)view.findViewById(R.id.rl_all); quickIndexBar = (QuickIndexBar) view.findViewById(R.id.quickindexbar); listView = (ListView) view.findViewById(R.id.listview); filter_listview = (ListView) view.findViewById(R.id.filter_listview); tvPrompt = (TextView) view.findViewById(R.id.tv_prompt); tv_search_no_result = (TextView) view.findViewById(R.id.tv_search_no_result); fl_result = (FrameLayout) view.findViewById(R.id.fl_result); } @Override public void initData() { dataBeanList.clear(); dataBeanList.add(new DataBean("张三")); dataBeanList.add(new DataBean("李四")); dataBeanList.add(new DataBean("王五")); fillDataAndSort(dataBeanList); listView.setAdapter(new QuickIndexBarAdapter(mContext, sortDataBeanList)); } @Override public void initListener() { quickIndexBar.setOnLetterChangeListener(new QuickIndexBar.OnLetterChangeListener() { @Override public void onLetterChange(String letter) { tvPrompt.setVisibility(View.VISIBLE); tvPrompt.setText(letter); // 当点击最顶部的文字时 让Listview到最顶部 if (letter.equals("热")) { listView.setSelection(0); } // ToastUtils.showToast(MainActivity.this, letter); for (int i = 0; i < dataBeanList.size(); i++) { DataBean haoHan = dataBeanList.get(i); if (TextUtils.equals(haoHan.pinyin.charAt(0) + "", letter)) { // listView.setSelection(i); //控制 点击后字母上面显示多出来的元素 listView.setSelection(i + 1); // break; } } } @Override public void onReset() { // ToastUtils.showToast(MainActivity.this, "手指抬起"); tvPrompt.setVisibility(View.GONE); } }); // 搜索框的输入改变监听 et_search.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // 每次改变都需要根据改变去筛选 // 如果输入框中的 字符不为空 让filterlistview显示 filterDataList.clear(); // 每次都要清空之前过滤后存有过滤数据的集合 String search_str = et_search.getText().toString().trim(); if (!TextUtils.isEmpty(search_str)){ // 搜索结果及无结果的提示 fl_result.setVisibility(View.VISIBLE); filter_listview.setVisibility(View.VISIBLE); rl_all.setVisibility(View.GONE); // 根据输入框的内容进行动态过滤 filterDataList = getFilterDataList(search_str); if (filterDataList.size()==0){ tv_search_no_result.setVisibility(View.VISIBLE); }else { tv_search_no_result.setVisibility(View.GONE); } filter_listview.setAdapter(new DefaultBaseAdapter<DataBean>(mContext, filterDataList) { @Override public View getView(final int position, View convertView, ViewGroup parent) { ResultViewHolder holder = null; if (convertView == null){ convertView = LayoutInflater.from(mContext).inflate(R.layout.item_indexbar_search_result,null); holder = new ResultViewHolder(); holder.tv = (TextView)convertView.findViewById(R.id.text1); convertView.setTag(holder); }else { holder = (ResultViewHolder) convertView.getTag(); } final int fposition = position; holder.tv.setText(filterDataList.get(position).getBeadhouse_name()); holder.tv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 点击 Toast.makeText(mContext,"点击:"+filterDataList.get(fposition).getBeadhouse_name(),Toast.LENGTH_SHORT).show(); // 必须放在后面 et_search.setText(""); } }); return convertView; } class ResultViewHolder{ public TextView tv; } }); }else { //为空时gone掉 filter_listview.setVisibility(View.GONE); rl_all.setVisibility(View.VISIBLE); tv_search_no_result.setVisibility(View.GONE); fl_result.setVisibility(View.GONE); } } @Override public void afterTextChanged(Editable s) { } }); } /*** * 获得过滤后的数据集合 * @param search_str * @return */ private List<DataBean> getFilterDataList(String search_str) { for (int i = 0; i < sortDataBeanList.size(); i++) { DataBean haohan = sortDataBeanList.get(i); //这里需要变成大写 因为在对象内部变拼音的时候 转换的是大写 if (haohan.getBeadhouse_name().contains(search_str)||haohan.pinyin.contains(search_str.toUpperCase())){ DataBean newHaohan = new DataBean(haohan.getBeadhouse_name()); newHaohan.setBeadhouse_id(sortDataBeanList.get(i).getBeadhouse_id()); filterDataList.add(newHaohan); // filterDataList.add(newHaohan.getBeadhouse_name()); } } return filterDataList; } /*** * 填充对数据集合进行排序 * @param bean */ private void fillDataAndSort(List<DataBean> bean) { sortDataBeanList.clear(); String[] names = new String[bean.size()]; String[] ids = new String[bean.size()]; for (int i = 0; i < bean.size() ; i++) { names[i] = bean.get(i).getBeadhouse_name(); ids[i] = bean.get(i).getBeadhouse_id(); } // 1. 将数组 转换为 javaben DataBean dataBean; for (int i = 0; i < names.length; i++) { String name = names[i]; dataBean = new DataBean(name); String id = ids[i]; dataBean.setBeadhouse_id(id); sortDataBeanList.add(dataBean); } // 2排序 Collections.sort(sortDataBeanList); } /*** * 返回键的处理 我这里是在fragment 所以这里会报错 如果在 Activity中可以这样处理 */ // @Override // public void onBackPressed() { super.onBackPressed(); // if (!TextUtils.isEmpty(et_search.getText().toString().trim())){ // et_search.setText(""); // }else { // super.onBackPressed(); // } // } }
快速索引------
package myapp.first.myapplication.view; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import myapp.first.myapplication.R; import myapp.first.myapplication.tools.DensityUtil; /** * Created by xi on 2016/7/3. * 快速索引 */ public class QuickIndexBar extends View { private static final String[] LETTERS = new String[]{"热","A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}; private final int mTextHeight; private Paint paint; private int mCellWidth; private int mCellHeight; // 层高 private int currentIndex = - 1; public QuickIndexBar(Context context) { this(context, null); } public QuickIndexBar(Context context, AttributeSet attrs) { this(context, attrs, 0); } public QuickIndexBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); // 初始化 paint = new Paint();// 默认是黑色 paint.setColor(Color.WHITE); //抗锯齿 paint.setAntiAlias(true); // paint.setTypeface(Typeface.DEFAULT_BOLD);//加粗 paint.setTextSize(18); // 获取字体高度 Paint.FontMetrics fontMetrics = paint.getFontMetrics(); mTextHeight = (int) (fontMetrics.descent - fontMetrics.ascent); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); //会调用多次 } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); // 控件的宽高 mCellWidth = getMeasuredWidth(); mCellHeight = getMeasuredHeight() / LETTERS.length; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); for (int i = 0; i < LETTERS.length; i++) { String letter = LETTERS[i]; // 通过画笔获取字体的宽度 float mTextWidth = paint.measureText(letter); float x = (mCellWidth - mTextWidth) / 2; // paint.getTextBounds(); float y = (mCellHeight + mTextHeight) / 2 + mCellHeight * i; if (currentIndex == i){ paint.setColor(Color.BLACK); // if (currentIndex == 0){ // paint.setTextSize(DensityUtil.dip2px(getContext(),15)); // }else { // paint.setTextSize(DensityUtil.dip2px(getContext(),11)); // } }else{ // paint.setColor(Color.WHITE); if (i == 0){ paint.setTextSize(DensityUtil.dip2px(getContext(),15)); paint.setColor(getResources().getColor(R.color.quickIndexBar_text_color)); }else { paint.setTextSize(DensityUtil.dip2px(getContext(),15)); paint.setColor(getResources().getColor(R.color.quickIndexBar_zimu_color)); } } // 画字 canvas.drawText(letter, x, y, paint); //重新绘制 invalidate(); } } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: //手指按下 int downY = (int) event.getY(); currentIndex = downY / mCellHeight; // Toast.makeText(getContext(), LETTERS[currentIndex], Toast.LENGTH_SHORT).show(); if (currentIndex < 0 || currentIndex > LETTERS.length) { } else { // ToastUtils.showToast(getContext(), LETTERS[currentIndex]); if (onLetterChangeListener != null){ onLetterChangeListener.onLetterChange( LETTERS[currentIndex]); } } //重新绘制 invalidate(); break; case MotionEvent.ACTION_MOVE: int moveY = (int) event.getY(); currentIndex = moveY / mCellHeight; // Toast.makeText(getContext(), LETTERS[currentIndex], Toast.LENGTH_SHORT).show(); if (currentIndex < 0 || currentIndex >= LETTERS.length) { } else { // ToastUtils.showToast(getContext(), LETTERS[currentIndex]); if (onLetterChangeListener != null){ onLetterChangeListener.onLetterChange( LETTERS[currentIndex]); } } //重新绘制 invalidate(); break; case MotionEvent.ACTION_UP: if (onLetterChangeListener != null){ onLetterChangeListener.onReset(); } currentIndex = -1; invalidate(); break; } // move + up return true; } private OnLetterChangeListener onLetterChangeListener; public OnLetterChangeListener getOnLetterChangeListener() { return onLetterChangeListener; } public void setOnLetterChangeListener(OnLetterChangeListener onLetterChangeListener) { this.onLetterChangeListener = onLetterChangeListener; } public interface OnLetterChangeListener { void onLetterChange(String letter); void onReset();//手指抬起 } }
DefaultAdapter:
package myapp.first.myapplication.adapter; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import java.util.List; /** * @Author Xi * Created by zz.wbkj.xi on 2016/8/10. */ public abstract class DefaultBaseAdapter<T> extends BaseAdapter { public int res; public List<T> dataList; public Context mContext; public DefaultBaseAdapter(Context context, List<T> dataList) { this.dataList = dataList; this.mContext = context; } @Override public int getCount() { return dataList.size(); } @Override public Object getItem(int position) { return dataList.get(position); } @Override public long getItemId(int position) { return position; } @Override public abstract View getView(int position, View convertView, ViewGroup parent) ; }
QuickIndexBarAdapter:
package myapp.first.myapplication.adapter; import android.content.Context; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import android.widget.Toast; import java.util.List; import myapp.first.myapplication.R; import myapp.first.myapplication.bean.DataBean; import myapp.first.myapplication.tools.ToastUtils; /** * Created by wangx on 2016/7/3. */ public class QuickIndexBarAdapter extends BaseAdapter { // private HistoryYanglaoyuanDao historyDao; private Context context; private List<DataBean> dataBeanList; public QuickIndexBarAdapter(Context context, List<DataBean> dataBeanList) { this.context = context; this.dataBeanList = dataBeanList; // historyDao = HistoryYanglaoyuanDao.getInstance(context); } @Override public int getCount() { return dataBeanList.size(); } @Override public Object getItem(int i) { return null; } @Override public long getItemId(int i) { return 0; } @Override public View getView(final int position, View convertView, ViewGroup viewGroup) { ViewHolder holder = null; if (convertView == null) { convertView = View.inflate(context, R.layout.item_indexbar, null); holder = new ViewHolder(convertView); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } DataBean dataBean = dataBeanList.get(position); DataBean preHaohan = null; if (position == 0) { preHaohan = null; } else { preHaohan = dataBeanList.get(position - 1); } boolean isShowPinyin = true;// 是否显示拼音 if (preHaohan== null){ isShowPinyin = true; }else{ if (dataBean.pinyin.charAt(0) == preHaohan.pinyin.charAt(0)){ // 当前好汉 的首字母 和上一个好汉的首字母 一样 isShowPinyin = false; }else{ isShowPinyin = true; } } final DataBean finalDatabean = dataBean; holder.tv_pinyin.setVisibility(isShowPinyin? View.VISIBLE: View.GONE); holder.line.setVisibility(isShowPinyin? View.VISIBLE: View.GONE); holder.tv_name.setText(dataBean.getBeadhouse_name()); holder.tv_name.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(context,"点击:"+finalDatabean.getBeadhouse_name(),Toast.LENGTH_SHORT).show(); } }); holder.tv_pinyin.setText(dataBean.pinyin.charAt(0) + ""); return convertView; } public static class ViewHolder { public View rootView; public TextView tv_pinyin; public TextView tv_name; public View line; public ViewHolder(View rootView) { this.rootView = rootView; this.tv_pinyin = (TextView) rootView.findViewById(R.id.tv_pinyin); this.tv_name = (TextView) rootView.findViewById(R.id.tv_name); this.line = (View) rootView.findViewById(R.id.line); } } }
PinYinUtils:
package myapp.first.myapplication.tools; import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType; import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; import net.sourceforge.pinyin4j.format.HanyuPinyinToneType; import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination; /** * Created by xi on 2016/7/3. */ public class PinYinUtils { /** * 汉语转换为拼音 * @param text * @return */ public static String getPinYin(String text){ char[] chars = text.toCharArray(); StringBuilder builder = new StringBuilder(); HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat(); format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 取消声调 format.setCaseType(HanyuPinyinCaseType.UPPERCASE);//大写拼音 for (char ch : chars) { //过滤空格 if (Character.isWhitespace(ch)) { continue; //跳出当前循环 } // 是中文字符 转拼音 ascII > 128 < -127 if (ch > 128 || ch < -127) { try { String[] str = PinyinHelper.toHanyuPinyinStringArray(ch, format); // 多音字 贾平凹 builder.append(str[0]); } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) { badHanyuPinyinOutputFormatCombination.printStackTrace(); } }else{ // 是英文时 builder.append(String.valueOf(ch).toUpperCase()); } } return builder.toString(); } }
shape_indexbar_corners.xml:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <!--填充颜色--> <solid android:color="#88bdbdbd" /> <!--弧度--> <corners android:radius="10dp" /> </shape>shape_indexbar_search_corners.xml:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <!--填充颜色--> <solid android:color="@color/white" /> <!--弧度--> <corners android:radius="20dp" /> </shape>
item_indexbar.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="vertical"> <TextView android:id="@+id/tv_pinyin" android:layout_width="match_parent" android:layout_height="33dp" android:background="#f6f6f6" android:gravity="center_vertical" android:minHeight="24dp" android:paddingLeft="12dp" android:text="A" android:textColor="#343434" android:textSize="18sp" /> <View android:id="@+id/line" android:layout_width="match_parent" android:layout_height="1dp" android:background="#d6d5d6"/> <TextView android:id="@+id/tv_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:background="@color/white" android:minHeight="48dp" android:paddingLeft="13dp" android:textColor="#343434" android:text="姓名" android:textSize="20sp" /> </LinearLayout>
item_indexbar_search_result.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:id="@+id/item" android:layout_height="40dp"> <TextView android:id="@+id/text1" android:layout_width="match_parent" android:layout_height="40dp" android:gravity="center_vertical" android:paddingLeft="15dp" android:background="@color/white" android:textSize="14dp" android:textColor="#666666" android:text="结果"> </TextView> </LinearLayout>
search_result.xml:
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:id="@+id/fl_result" android:visibility="gone" android:layout_height="match_parent"> <ListView android:id="@+id/filter_listview" android:visibility="gone" android:layout_width="match_parent" android:layout_height="match_parent"> </ListView> <TextView android:id="@+id/tv_search_no_result" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:textSize="15dp" android:paddingBottom="90dp" android:text="搜索无结果"/> </FrameLayout>
<!--快速索引--> <color name="quickIndexBar_zimu_color">#548adf</color> <color name="quickIndexBar_text_color">#777777</color> <color name="line_divider">#d4d4d4</color> <color name="white">#ffffff</color>
最下面的这个是 子条目gridview形式的 adapter: 其它的部分内容请参考者自己补充
package myapp.first.myapplication.adapter; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import myapp.first.myapplication.R; import myapp.first.myapplication.bean.DataBean; import myapp.first.myapplication.view.MyGridView; /** * @Author Xi * Created by zz.wbkj.xi on 2016/8/31. */ public class QuickIndexBarGridAdapter extends DefaultBaseAdapter<DataBean> { public List<String> list = new ArrayList<>(); public HashMap<String,List<DataBean>> gridViewList = new HashMap<>(); public QuickIndexBarGridAdapter(Context context, List<DataBean> dataList) { super(context, dataList); for (int i = 0; i < dataList.size(); i++) { String zimu = dataList.get(i).pinyin.substring(0,1); if (!list.contains(zimu)){ list.add(zimu); List<DataBean> item_gridview_list = new ArrayList<>(); item_gridview_list.add(dataList.get(i)); gridViewList.put(zimu,item_gridview_list); }else { gridViewList.get(zimu).add(dataList.get(i)); } } } @Override public int getCount() { return list.size(); } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null){ convertView = LayoutInflater.from(mContext).inflate(R.layout.item_indexbar_grid,null); holder = new ViewHolder(); holder.tv_zimu = (TextView)convertView.findViewById(R.id.tv_zimu); holder.gridview = (MyGridView)convertView.findViewById(R.id.gridview); convertView.setTag(holder); }else{ holder = (ViewHolder) convertView.getTag(); } for (int i = 0; i < list.size(); i++) { if (position == i){ holder.tv_zimu.setText(list.get(position)); } } // R.layout.item_addkinsfolk_gridview holder.gridview.setAdapter(new DefaultBaseAdapter<DataBean>(mContext,gridViewList.get(list.get(position))) { @Override public View getView(final int position, View convertView, ViewGroup parent) { GridViewHolder gridviewholder = null; if (convertView == null){ convertView = LayoutInflater.from(mContext).inflate(R.layout.item_indexbar_gridview_item,null); gridviewholder = new GridViewHolder(); gridviewholder.item = (LinearLayout)convertView.findViewById(R.id.item); gridviewholder.tv = (TextView)convertView.findViewById(R.id.tv_item_gridview); convertView.setTag(gridviewholder); }else { gridviewholder = (GridViewHolder) convertView.getTag(); } final int finalPosition = position; gridviewholder.tv.setText(dataList.get(position).getBeadhouse_name()); gridviewholder.item.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 点击 Toast.makeText(mContext,"点击:"+dataList.get(finalPosition).getBeadhouse_name(),Toast.LENGTH_SHORT).show(); } }); return convertView; } }); return convertView; } class ViewHolder { public TextView tv_zimu; public MyGridView gridview; } class GridViewHolder{ public TextView tv; public LinearLayout item; } }