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

效果图:

Android——仿京东淘宝分类页面_第1张图片Android——仿京东淘宝分类页面_第2张图片

json在assets下

Android——仿京东淘宝分类页面_第3张图片

添加依赖:

 

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 menuList = new ArrayList<>();
private List homeList = new ArrayList<>();
private List 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 foodDatas;

public HomeAdapter(Context context, List 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 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:[email protected]
*/

public class HomeItemAdapter extends BaseAdapter {

private Context context;
private List foodDatas;

public HomeItemAdapter(Context context, List 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 list;

public MenuAdapter(Context context, List 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
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">

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

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

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

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

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


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" />





item_home.xml
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">


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" />

android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="3">


item_home_category.xml
android:layout_width="match_parent"
android:layout_height="match_parent">


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

android:id="@+id/item_album"
android:layout_width="85dp"
android:layout_height="85dp"
android:layout_gravity="center_horizontal"
android:scaleType="centerCrop" />

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" />






 

item_menu.xml
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
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"/>

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

 

你可能感兴趣的:(移动开发)