标签:
省去前因后果,咱直奔主题,我们首先来看一下美团最新版本的关于全部分类的布局。
进入全部分类
点击小分类中的下拉TextView弹出完整小分类
从下图可以看出明显的生成LinearLayout再生成TextView痕迹
ListView +自动生成TextView、
0.布局里写个listview,初始化并设置自定义适配器
1.从服务器哥们手里拿数据
HttpRequestDAO.getInstance().request(WebConstrant.getMoreCategory, null, context, new IStringRequestCallback() {
@Override
public void onFailure(int code, String error) {
netError();
}
private void netError() {
Toast.makeText(EWifi.getApp().getMainActivity(), "网络错误", Toast.LENGTH_SHORT).show();
}
@Override
public void onSuccess(int code, String data) {
}
});
格式化
生成JavaBean
创建JavaBean生成必要getter/setter方法
继续数据转换和调试
HttpRequestDAO.getInstance().request(WebConstrant.getMoreCategory, null, context, new IStringRequestCallback() {
@Override
public void onFailure(int code, String error) {
netError();
}
private void netError() {
Toast.makeText(EWifi.getApp().getMainActivity(), "网络错误", Toast.LENGTH_SHORT).show();
}
@Override
public void onSuccess(int code, String data) {
Gson gson = new GsonBuilder().create();
MoreCategory moreCategory = gson.fromJson(data, MoreCategory.class);// json转bean
ArrayList<String> hotRowNames = new ArrayList<String>();// 热门大类分类名称arraylist
ArrayList<String> categoryNames = new ArrayList<>();// 普通分类大类的名称
categorys = new LinkedHashMap<String, ArrayList<String>>();// 全部分类
for (int i = 0; i < moreCategory.responseData.total; i++) {// 大类
categoryNames.add(moreCategory.responseData.rows.get(i).getName());
Log.e("MoreFragment", "普通大类:" + moreCategory.responseData.rows.get(i).getName());
}
for (int j = 0; j < categoryNames.size(); j++) {// 往普通大类里添加小分类
ArrayList<String> childrenNames = new ArrayList<>();// 小分类名称集合
for (int k = 0; k < moreCategory.responseData.rows.get(j).children.size(); k++) {
childrenNames.add(moreCategory.responseData.rows.get(j).children.get(k).name);
}
categorys.put(categoryNames.get(j), childrenNames);
}
// 热门分类填充数据
for (int i = 0; i < moreCategory.getResponseData().getHottotal(); i++) {
hotRowNames.add(moreCategory.getResponseData().getHotrows().get(i).getName());
}
// 往全部分类最后添加热门分类
categorys.put("热门", hotRowNames);
printList(categorys);//数据调试
EWifi.getApp().getMainActivity().runOnUiThread(new Runnable() {
public void run() {
categoryAdapter.notifyDataSetChanged();
}
});
}
private void printList(LinkedHashMap<String, ArrayList<String>> categorys) {
Iterator iter = categorys.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
System.out.println("大分类: "+entry.getKey()+" 小分类 "+entry.getValue());
}
}
});
输出如下
编写adapter
package com.xxx.app.adapter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import com.xxx.app.xxx;
import com.xxx.app.R;
import com.xxx.app.adapter.CategoryAdapter.CategoryTitleHolder;
import com.nostra13.universalimageloader.core.ImageLoader;
import android.content.Context;
import android.graphics.Color;
import android.media.Image;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;
public class CategoryAdapterNew extends BasicAdapter<Object> {
private final int ITEM_TITLE = 0;// title类型的item
private final int ITEM_INFO = 1;// info类型的item
private final ArrayList<TextView> disabledTextView;
public CategoryAdapterNew(Context context, ArrayList<Object> list) {
super(context, list);
disabledTextView = new ArrayList<TextView>();
}
@Override
public boolean areAllItemsEnabled() {
return false;
}
@Override
public boolean isEnabled(int position) {
return false;
}
/**
* 共2中item类型:title和info
*/
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TitleHolder titleHolder;
InfoHolder infoHolder;
int itemViewType = getItemViewType(position);
switch (itemViewType) {
case ITEM_TITLE:// 当前是title
if (null == convertView) {
convertView = View.inflate(context, R.layout.adapter_category_title_new, null);
titleHolder = new TitleHolder();
titleHolder.icon = (ImageView) convertView.findViewById(R.id.icon);
titleHolder.category_title = (TextView) convertView.findViewById(R.id.category_title);
convertView.setTag(titleHolder);
} else {
titleHolder = (TitleHolder) convertView.getTag();
}
setTitleImage(list.get(position).toString(), titleHolder);
titleHolder.category_title.setText(list.get(position).toString());
titleHolder.category_title.setTextColor(Color.parseColor("#666666"));
break;
case ITEM_INFO:// 当前是info
if (null == convertView) {
convertView = View.inflate(context, R.layout.adapter_category_info_new, null);
infoHolder = new InfoHolder();
infoHolder.infoLayout = (LinearLayout) convertView.findViewById(R.id.adapter_category_info);
convertView.setTag(infoHolder);
} else {
infoHolder = (InfoHolder) convertView.getTag();
infoHolder.infoLayout.removeAllViews();
}
// 获取分类数据
ArrayList<String> smallCategoryList = new ArrayList<String>();
smallCategoryList = (ArrayList<String>) list.get(position);
buildLayout(smallCategoryList, infoHolder.infoLayout);// 创建布局
break;
}
return convertView;
}
private void buildLayout(final ArrayList<String> smallCategoryList, LinearLayout infoLayout) {
// 根据小分类的长度进行创建布局
// 与4求模
int count = 0;// 求模次数
// 数据调试
// smallCategoryList.clear();
// smallCategoryList.add("测试1");
// smallCategoryList.add("测试2");
// smallCategoryList.add("测试3");
// smallCategoryList.add("测试4");
// smallCategoryList.add("测试5");
// smallCategoryList.add("测试6");
// smallCategoryList.add("测试7");
// smallCategoryList.add("测试8");
// smallCategoryList.add("测试9");
// smallCategoryList.add("测试10");
// smallCategoryList.add("测试11");
// smallCategoryList.add("测试12");
// smallCategoryList.add("测试13");
// smallCategoryList.add("测试14");
System.out.println("求模前尺寸:" + smallCategoryList.size());
for (int i = 0; i < smallCategoryList.size(); i++) {
if (i != 0) {
if (i % 4 == 0) {
count = count + 1;
}
}
}
if (4 * count < smallCategoryList.size()) {
System.out.println("剩余模独数:" + (smallCategoryList.size() - 4 * count) + "已得生成数:" + count);
count = count + 1;// 不管怎样要多一行
if ((smallCategoryList.size() - 4 * count) == 4) {
}
}
// 判断count数量是否大于3 是的话3行4列显示下拉 否则直接全部显示
if (count > 3) {
for (int i = 0; i < count; i++) {
// 生成两行行小分类LinearLayout并且生成小分类列表对应尺寸的textview
// 生成4格小格小LinearLayout
LinearLayout llayout = new LinearLayout(context);
llayout.setOrientation(LinearLayout.HORIZONTAL);
llayout.setBackground(context.getResources().getDrawable(R.drawable.setbar_bg));
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
android.view.ViewGroup.LayoutParams.MATCH_PARENT);
params.leftMargin = xxx.getApp().getResources().getDimensionPixelOffset(R.dimen.x50);
params.rightMargin = xxx.getApp().getResources().getDimensionPixelOffset(R.dimen.x50);
llayout.setGravity(Gravity.CENTER_VERTICAL);
llayout.setLayoutParams(params);
// TextView部分
for (int j = i * 4; j < smallCategoryList.size(); j++) {
final int m = j;
// 当大于第12项 将3行4列变为下拉的符号
if (j == 11) {
/*
* <LinearLayout android:id="@+id/line3_tv5"
* android:layout_width="0dp"
* android:layout_height="@dimen/y150"
* android:layout_weight="3"
* android:background="@drawable/setbar_bg"
* android:gravity="center" android:visibility="gone" >
*
*
* <ImageView android:layout_width="wrap_content"
* android:layout_height="wrap_content"
* android:background=
* "@drawable/ic_global_more_category_arrow_down" />
*/
final ImageView imageView = new ImageView(context);
imageView.setBackground(
context.getResources().getDrawable(R.drawable.ic_global_more_category_arrow_down));
LinearLayout.LayoutParams mParams = new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
mParams.setMargins(xxx.getApp().getResources().getDimensionPixelOffset(R.dimen.x100), 0,
xxx.getApp().getResources().getDimensionPixelOffset(R.dimen.x50), 0);
mParams.gravity = Gravity.CENTER_VERTICAL;
imageView.setLayoutParams(mParams);
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
imageView.setVisibility(View.GONE);
System.out.println("disabledTextView"+disabledTextView.size()+"smallCategoryList"+smallCategoryList.size());
for (int k = 0; k < disabledTextView.size(); k++) {
disabledTextView.get(k).setVisibility(View.VISIBLE);
}
}
});
// 当时的那个下拉对应的textview
// 添加4格TextView
TextView textView = new TextView(context);
// 获得屏幕宽
WindowManager wm = xxx.getApp().getMainActivity().getWindowManager();
int width = wm.getDefaultDisplay().getWidth();
int height = wm.getDefaultDisplay().getHeight();
width = width / 4 - (xxx.getApp().getResources().getDimensionPixelOffset(R.dimen.x26));
System.out.println("mWidth4:" + width);
android.view.ViewGroup.LayoutParams tvparams = new LayoutParams(width,
xxx.getApp().getResources().getDimensionPixelOffset(R.dimen.y150));
textView.setLayoutParams(tvparams);
textView.setTextColor(Color.parseColor("#535553"));
textView.setGravity(Gravity.CENTER);
textView.setSingleLine();
textView.setEllipsize(TextUtils.TruncateAt.END);
textView.setBackground(xxx.getApp().getResources().getDrawable(R.drawable.setbar_bg));
textView.setText(smallCategoryList.get(j));
textView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, smallCategoryList.get(m), Toast.LENGTH_SHORT).show();
}
});
textView.setVisibility(View.GONE);
disabledTextView.add(textView);
llayout.addView(textView);
llayout.addView(imageView);
break;
}
// 添加4格TextView
TextView textView = new TextView(context);
// 获得屏幕宽
WindowManager wm = xxx.getApp().getMainActivity().getWindowManager();
int width = wm.getDefaultDisplay().getWidth();
int height = wm.getDefaultDisplay().getHeight();
width = width / 4 - (xxx.getApp().getResources().getDimensionPixelOffset(R.dimen.x26));
System.out.println("mWidth4:" + width);
android.view.ViewGroup.LayoutParams tvparams = new LayoutParams(width,
xxx.getApp().getResources().getDimensionPixelOffset(R.dimen.y150));
textView.setLayoutParams(tvparams);
textView.setTextColor(Color.parseColor("#535553"));
textView.setGravity(Gravity.CENTER);
textView.setSingleLine();
textView.setEllipsize(TextUtils.TruncateAt.END);
textView.setBackground(xxx.getApp().getResources().getDrawable(R.drawable.setbar_bg));
textView.setText(smallCategoryList.get(j));
textView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, smallCategoryList.get(m), Toast.LENGTH_SHORT).show();
}
});
if (j > 11) {
textView.setVisibility(View.GONE);
disabledTextView.add(textView);
}
llayout.addView(textView);
}
infoLayout.addView(llayout);
}
} else
{
// 如果一行
if (count == 1) {
// 生成一行小分类LinearLayout并且生成小分类列表对应尺寸的textview
// 生成4格小格小LinearLayout
LinearLayout llayout = new LinearLayout(context);
llayout.setOrientation(LinearLayout.HORIZONTAL);
llayout.setBackground(context.getResources().getDrawable(R.drawable.setbar_bg));
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
android.view.ViewGroup.LayoutParams.MATCH_PARENT);
params.leftMargin = xxx.getApp().getResources().getDimensionPixelOffset(R.dimen.x50);
params.rightMargin = xxx.getApp().getResources().getDimensionPixelOffset(R.dimen.x50);
llayout.setGravity(Gravity.CENTER_VERTICAL);
llayout.setLayoutParams(params);
// TextView部分
for (int i = 0; i < smallCategoryList.size(); i++) {
final int m = i;
// 添加4格TextView
TextView textView = new TextView(context);
// 获得屏幕宽
WindowManager wm = xxx.getApp().getMainActivity().getWindowManager();
int width = wm.getDefaultDisplay().getWidth();
int height = wm.getDefaultDisplay().getHeight();
width = width / 4 - (xxx.getApp().getResources().getDimensionPixelOffset(R.dimen.x26));
System.out.println("mWidth4:" + width);
android.view.ViewGroup.LayoutParams tvparams = new LayoutParams(width,
xxx.getApp().getResources().getDimensionPixelOffset(R.dimen.y150));
textView.setLayoutParams(tvparams);
textView.setTextColor(Color.parseColor("#535553"));
textView.setGravity(Gravity.CENTER);
textView.setSingleLine();
textView.setEllipsize(TextUtils.TruncateAt.END);
textView.setBackground(xxx.getApp().getResources().getDrawable(R.drawable.setbar_bg));
textView.setText(smallCategoryList.get(i));
textView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, smallCategoryList.get(m), Toast.LENGTH_SHORT).show();
}
});
llayout.addView(textView);
}
infoLayout.addView(llayout);
} else if (count == 2) {
for (int i = 0; i < count; i++) {
// 生成两行行小分类LinearLayout并且生成小分类列表对应尺寸的textview
// 生成4格小格小LinearLayout
LinearLayout llayout = new LinearLayout(context);
llayout.setOrientation(LinearLayout.HORIZONTAL);
llayout.setBackground(context.getResources().getDrawable(R.drawable.setbar_bg));
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
android.view.ViewGroup.LayoutParams.MATCH_PARENT);
params.leftMargin = xxx.getApp().getResources().getDimensionPixelOffset(R.dimen.x50);
params.rightMargin = xxx.getApp().getResources().getDimensionPixelOffset(R.dimen.x50);
llayout.setGravity(Gravity.CENTER_VERTICAL);
llayout.setLayoutParams(params);
// TextView部分
for (int j = i * 4; j < smallCategoryList.size(); j++) {
final int m = j;
System.out.println("初始j:" + j);
System.out.println("初始i:" + i);
// 添加4格TextView
TextView textView = new TextView(context);
// 获得屏幕宽
WindowManager wm = xxx.getApp().getMainActivity().getWindowManager();
int width = wm.getDefaultDisplay().getWidth();
int height = wm.getDefaultDisplay().getHeight();
width = width / 4 - (xxx.getApp().getResources().getDimensionPixelOffset(R.dimen.x26));
System.out.println("mWidth4:" + width);
android.view.ViewGroup.LayoutParams tvparams = new LayoutParams(width,
xxx.getApp().getResources().getDimensionPixelOffset(R.dimen.y150));
textView.setLayoutParams(tvparams);
textView.setTextColor(Color.parseColor("#535553"));
textView.setGravity(Gravity.CENTER);
textView.setSingleLine();
textView.setEllipsize(TextUtils.TruncateAt.END);
textView.setBackground(xxx.getApp().getResources().getDrawable(R.drawable.setbar_bg));
textView.setText(smallCategoryList.get(j));
textView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, smallCategoryList.get(m), Toast.LENGTH_SHORT).show();
}
});
llayout.addView(textView);
}
infoLayout.addView(llayout);
}
} else if (count == 3) {
for (int i = 0; i < count; i++) {
// 生成两行行小分类LinearLayout并且生成小分类列表对应尺寸的textview
// 生成4格小格小LinearLayout
LinearLayout llayout = new LinearLayout(context);
llayout.setOrientation(LinearLayout.HORIZONTAL);
llayout.setBackground(context.getResources().getDrawable(R.drawable.setbar_bg));
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
android.view.ViewGroup.LayoutParams.MATCH_PARENT);
params.leftMargin = xxx.getApp().getResources().getDimensionPixelOffset(R.dimen.x50);
params.rightMargin = xxx.getApp().getResources().getDimensionPixelOffset(R.dimen.x50);
llayout.setGravity(Gravity.CENTER_VERTICAL);
llayout.setLayoutParams(params);
// TextView部分
for (int j = i * 4; j < smallCategoryList.size(); j++) {
final int m = j;
// 添加4格TextView
TextView textView = new TextView(context);
// 获得屏幕宽
WindowManager wm = xxx.getApp().getMainActivity().getWindowManager();
int width = wm.getDefaultDisplay().getWidth();
int height = wm.getDefaultDisplay().getHeight();
width = width / 4 - (xxx.getApp().getResources().getDimensionPixelOffset(R.dimen.x26));
System.out.println("mWidth4:" + width);
android.view.ViewGroup.LayoutParams tvparams = new LayoutParams(width,
xxx.getApp().getResources().getDimensionPixelOffset(R.dimen.y150));
textView.setLayoutParams(tvparams);
textView.setTextColor(Color.parseColor("#535553"));
textView.setGravity(Gravity.CENTER);
textView.setSingleLine();
textView.setEllipsize(TextUtils.TruncateAt.END);
textView.setBackground(xxx.getApp().getResources().getDrawable(R.drawable.setbar_bg));
textView.setText(smallCategoryList.get(j));
textView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, smallCategoryList.get(m), Toast.LENGTH_SHORT).show();
}
});
llayout.addView(textView);
}
infoLayout.addView(llayout);
}
}
}
}
/**
* 根据position判断当前bean应该使用哪种类型的item
*/
@Override
public int getItemViewType(int position) {
Object object = list.get(position);
if (object instanceof String) {
// 当前是title类型的item
System.out.println("object instanceof String");
return ITEM_TITLE;
} else {
// 当前是info类型的item
System.out.println("object not instanceof String");
return ITEM_INFO;
}
}
private void setTitleImage(String title, TitleHolder titleHolder) {
if (title.equals("热门")) {
ImageLoader.getInstance().displayImage("drawable://" + R.drawable.renmen, titleHolder.icon);
} else if (title.equals("美食")) {
ImageLoader.getInstance().displayImage("drawable://" + R.drawable.meishi, titleHolder.icon);
} else if (title.equals("电影")) {
ImageLoader.getInstance().displayImage("drawable://" + R.drawable.dianying, titleHolder.icon);
} else if (title.equals("房产")) {
ImageLoader.getInstance().displayImage("drawable://" + R.drawable.fangchan, titleHolder.icon);
} else if (title.equals("购物")) {
ImageLoader.getInstance().displayImage("drawable://" + R.drawable.gouwu, titleHolder.icon);
} else if (title.equals("酒店")) {
ImageLoader.getInstance().displayImage("drawable://" + R.drawable.jiudian, titleHolder.icon);
} else if (title.equals("丽人")) {
ImageLoader.getInstance().displayImage("drawable://" + R.drawable.liren, titleHolder.icon);
} else if (title.equals("旅游")) {
ImageLoader.getInstance().displayImage("drawable://" + R.drawable.lvyou, titleHolder.icon);
} else if (title.equals("汽车")) {
ImageLoader.getInstance().displayImage("drawable://" + R.drawable.qiche, titleHolder.icon);
} else if (title.equals("其他")) {
ImageLoader.getInstance().displayImage("drawable://" + R.drawable.qita, titleHolder.icon);
} else if (title.equals("热门")) {
ImageLoader.getInstance().displayImage("drawable://" + R.drawable.renmen, titleHolder.icon);
} else if (title.equals("音乐")) {
ImageLoader.getInstance().displayImage("drawable://" + R.drawable.yinyue, titleHolder.icon);
} else if (title.equals("娱乐")) {
ImageLoader.getInstance().displayImage("drawable://" + R.drawable.yule, titleHolder.icon);
} else if (title.equals("生活")) {
ImageLoader.getInstance().displayImage("drawable://" + R.drawable.qita, titleHolder.icon);
}
}
private static class TitleHolder {
ImageView icon;
TextView category_title;
}
private static class InfoHolder {
LinearLayout infoLayout;
}
}
最终效果 左边美团 右边我们的
仿美团全部分类页面(网络数据加载+Listview+Textview自动生成)
标签:
原文地址:http://www.cnblogs.com/code4m/p/5143940.html