Android——仿京东淘宝分类页面

效果图:

json在assets下

添加依赖:

 

compile 'com.squareup.okhttp3:okhttp:3.8.1'
compile 'com.facebook.fresco:fresco:0.11.0'
compile 'com.alibaba:fastjson:1.1.46.android'

MainActivitypublic class MainActivity extends AppCompatActivity {

private List<String> menuList = new ArrayList<>();
private List<CategoryBean.DataBean> homeList = new ArrayList<>();
private List<Integer> showTitle;

private ListView lv_menu;
private ListView lv_home;

private MenuAdapter menuAdapter;
private HomeAdapter homeAdapter;
private int currentItem;

private TextView tv_title;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fresco.initialize(this);
initView();
loadData();
}


private void loadData() {

String json = getJson(this, "category.json");
CategoryBean categoryBean = JSONObject.parseObject(json, CategoryBean.class);
showTitle = new ArrayList<>();
for (int i = 0; i < categoryBean.getData().size(); i++) {
CategoryBean.DataBean dataBean = categoryBean.getData().get(i);
menuList.add(dataBean.getModuleTitle());
showTitle.add(i);
homeList.add(dataBean);
}
tv_title.setText(categoryBean.getData().get(0).getModuleTitle());

menuAdapter.notifyDataSetChanged();
homeAdapter.notifyDataSetChanged();
}

private void initView() {
lv_menu = (ListView) findViewById(R.id.lv_menu);
tv_title = (TextView) findViewById(R.id.tv_titile);
lv_home = (ListView) findViewById(R.id.lv_home);
menuAdapter = new MenuAdapter(this, menuList);
lv_menu.setAdapter(menuAdapter);

homeAdapter = new HomeAdapter(this, homeList);
lv_home.setAdapter(homeAdapter);

lv_menu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
menuAdapter.setSelectItem(position);
menuAdapter.notifyDataSetInvalidated();
tv_title.setText(menuList.get(position));
lv_home.setSelection(showTitle.get(position));
}
});


lv_home.setOnScrollListener(new AbsListView.OnScrollListener() {
private int scrollState;

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
this.scrollState = scrollState;
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
return;
}
int current = showTitle.indexOf(firstVisibleItem);
// lv_home.setSelection(current);
if (currentItem != current && current >= 0) {
currentItem = current;
tv_title.setText(menuList.get(currentItem));
menuAdapter.setSelectItem(currentItem);
menuAdapter.notifyDataSetInvalidated();
}
}
});
}

/**
* 得到json文件中的内容
*
* @param context
* @param fileName
* @return
*/
public static String getJson(MainActivity context, String fileName) {
StringBuilder stringBuilder = new StringBuilder();
//获得assets资源管理器
AssetManager assetManager = context.getAssets();
//使用IO流读取json文件内容
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
assetManager.open(fileName), "utf-8"));
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
return stringBuilder.toString();
}


}

GridViewForScrollViewpublic class GridViewForScrollView extends GridView {

public GridViewForScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public GridViewForScrollView(Context context) {
super(context);
}

public GridViewForScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec+30);

}


}

HomeAdapter/**
* 右侧主界面ListView的适配器
*
* @author Administrator
*/
public class HomeAdapter extends BaseAdapter {

private Context context;
private List<CategoryBean.DataBean> foodDatas;

public HomeAdapter(Context context, List<CategoryBean.DataBean> foodDatas) {
this.context = context;
this.foodDatas = foodDatas;
}

@Override
public int getCount() {
if (foodDatas != null) {
return foodDatas.size();
} else {
return 10;
}
}

@Override
public Object getItem(int position) {
return foodDatas.size();
}

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


@Override
public View getView(int position, View convertView, ViewGroup parent) {
CategoryBean.DataBean dataBean = foodDatas.get(position);
List<CategoryBean.DataBean.DataListBean> dataList = dataBean.getDataList();
ViewHold viewHold = null;
if (convertView == null) {
convertView = View.inflate(context, R.layout.item_home, null);
viewHold = new ViewHold();
viewHold.gridView = (GridViewForScrollView) convertView.findViewById(R.id.gridView);
viewHold.blank = (TextView) convertView.findViewById(R.id.blank);
convertView.setTag(viewHold);
} else {
viewHold = (ViewHold) convertView.getTag();
}
HomeItemAdapter adapter = new HomeItemAdapter(context, dataList);
viewHold.blank.setText(dataBean.getModuleTitle());
viewHold.gridView.setAdapter(adapter);
return convertView;
}

private static class ViewHold {
private GridViewForScrollView gridView;
private TextView blank;
}

}

HomeItemAdapter/**
* author:wangzihang
* date: 2017/8/8 19:15
* desctiption:
* e-mail:wangzihang@xiaohongchun.com
*/

public class HomeItemAdapter extends BaseAdapter {

private Context context;
private List<CategoryBean.DataBean.DataListBean> foodDatas;

public HomeItemAdapter(Context context, List<CategoryBean.DataBean.DataListBean> foodDatas) {
this.context = context;
this.foodDatas = foodDatas;
}


@Override
public int getCount() {
if (foodDatas != null) {
return foodDatas.size();
} else {
return 10;
}
}

@Override
public Object getItem(int position) {
return foodDatas.size();
}

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


@Override
public View getView(int position, View convertView, ViewGroup parent) {
CategoryBean.DataBean.DataListBean subcategory = foodDatas.get(position);
ViewHold viewHold = null;
if (convertView == null) {
convertView = View.inflate(context, R.layout.item_home_category, null);
viewHold = new ViewHold();
viewHold.tv_name = (TextView) convertView.findViewById(R.id.item_home_name);
viewHold.iv_icon = (SimpleDraweeView) convertView.findViewById(R.id.item_album);
convertView.setTag(viewHold);
} else {
viewHold = (ViewHold) convertView.getTag();
}
viewHold.tv_name.setText(subcategory.getTitle());
Uri uri = Uri.parse(subcategory.getImgURL());
viewHold.iv_icon.setImageURI(uri);
return convertView;


}

private static class ViewHold {
private TextView tv_name;
private SimpleDraweeView iv_icon;
}

}

MenuAdapter/**
* 左侧菜单ListView的适配器
*
* @author Administrator
*/
public class MenuAdapter extends BaseAdapter {

private Context context;
private int selectItem = 0;
private List<String> list;

public MenuAdapter(Context context, List<String> list) {
this.list = list;
this.context = context;
}

public int getSelectItem() {
return selectItem;
}

public void setSelectItem(int selectItem) {
this.selectItem = selectItem;
}

@Override
public int getCount() {
return list.size();
}

@Override
public Object getItem(int arg0) {
return list.get(arg0);
}

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

@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
ViewHolder holder = null;
if (arg1 == null) {
holder = new ViewHolder();
arg1 = View.inflate(context, R.layout.item_menu, null);
holder.tv_name = (TextView) arg1.findViewById(R.id.item_name);
arg1.setTag(holder);
} else {
holder = (ViewHolder) arg1.getTag();
}
if (arg0 == selectItem) {
holder.tv_name.setBackgroundColor(Color.WHITE);
holder.tv_name.setTextColor(context.getResources().getColor(R.color.green));
} else {
holder.tv_name.setBackgroundColor(context.getResources().getColor(R.color.background));
holder.tv_name.setTextColor(context.getResources().getColor(R.color.black));
}
holder.tv_name.setText(list.get(arg0));
return arg1;
}

static class ViewHolder {
private TextView tv_name;
}
}

activity_main.xml<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.bawei.com.jpushdemo.MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="horizontal">

<ListView
android:id="@+id/lv_menu"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#EDECEC" />

<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:background="@color/white">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
android:id="@+id/lv_home"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

<TextView
android:id="@+id/tv_titile"
android:layout_width="match_parent"
android:layout_height="35dp"
android:background="#DCDBDB"
android:gravity="center_vertical"
android:paddingLeft="20dp"
android:text="@string/app_name" />

</RelativeLayout>
</LinearLayout>


</RelativeLayout>

item_home.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/blank"
android:layout_width="match_parent"
android:layout_height="35dp"
android:background="#DCDBDB"
android:gravity="center_vertical"
android:paddingLeft="20dp"
android:text="@string/app_name" />

<com.bawei.com.jpushdemo.GridViewForScrollView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="3"></com.bawei.com.jpushdemo.GridViewForScrollView>


</LinearLayout>

item_home_category.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">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="120dp"
android:orientation="vertical">

<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/item_album"
android:layout_width="85dp"
android:layout_height="85dp"
android:layout_gravity="center_horizontal"
android:scaleType="centerCrop" />

<TextView
android:id="@+id/item_home_name"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:singleLine="true"
android:text="标题"
android:textSize="12dp" />

</LinearLayout>


</LinearLayout>


 

item_menu.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/item_name"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text=""
android:gravity="center"
android:textSize="12sp"
android:textColor="@color/green"/>

</LinearLayout>

请前往http://download.csdn.net/download/xy8199/10128753下载demo

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值