您的位置:首页 > 移动开发 > Android开发

Android-ExpandableListView 仿京东淘宝购物车

2016-12-14 13:10 597 查看

先上项目效果图



项目分析

1、拿到这个需求的时候以为是用ListView做的,然后发现自己好天真,因为它的店铺个数是不确定的,
然后同一个店铺下面的商品也可能是多个的,so 然后自己百度了下,
发现Android原生有个ExpandableListView列表组件,就可以用来搞这个购物车正好合适。
2、ExpandableListView原生的样式是隐藏列表内容,所以我们只需要用个for循环把每个列表的内容展开就可以了。


代码 MainActivity类

public class MainActivity extends Activity {

private ExpandableListView mEListView;

List<String> shop = null;
Map<String, List<String>> map = null;

private MyAdapter mAdapter;

private int groupCount;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEListView = (ExpandableListView) findViewById(R.id.elv_shoppingcart);

// 搞几个假数据测试
initData();

settingExpandableListView();
}

/** 把内容展开  */
private void settingExpandableListView() {
mEListView.setGroupIndicator(null);
for (int i = 0; i < shop.size(); i++) {
mEListView.expandGroup(i);
}
mEListView.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
return true;
}
});
}

public void addShop(View v) {
groupCount++;

String key = "" + groupCount;

List<String> list1 = new ArrayList<String>();
shop.add(key);
map.put(key, list1);

Log.e("img", "key " + key);
mAdapter.notifyDataSetChanged();
mEListView.expandGroup(groupCount-1);
}

private void initData() {

mAdapter = new MyAdapter();
shop = new ArrayList<String>();
map = new HashMap<String, List<String>>();

groupCount = 0;
String key = "" + groupCount;

List<String> list1 = new ArrayList<String>();
list1.add("");
shop.add(key);
map.put(key, list1);

mEListView.setAdapter(mAdapter);

}

class MyAdapter extends BaseExpandableListAdapter {

// 得到子item需要关联的数据
@Override
public Object getChild(int groupPosition, int childPosition) {
String key = shop.get(groupPosition);
return (map.get(key).get(childPosition));
}

// 得到子item的ID
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}

// 设置子item的组件
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
String key = MainActivity.this.shop.get(groupPosition);
String info = map.get(key).get(childPosition);

if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) MainActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.layout_children, null);
}

return convertView;
}

// 获取当前父item下的子item的个数
@Override
public int getChildrenCount(int groupPosition) {
String key = shop.get(groupPosition);
int size = map.get(key).size();
return size;
}

// 获取当前父item的数据
@Override
public Object getGroup(int groupPosition) {
return shop.get(groupPosition);
}

@Override
public int getGroupCount() {
return shop.size();
}

@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}

// 设置父item组件
@Override
public View getGroupView(final int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) MainActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.layout_parent, null);
holder.img = (ImageView) convertView
.findViewById(R.id.iv_goods_add);

convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

holder.img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String key = shop.get(groupPosition);

Log.e("img", "map "+map.get(key).size());

List<String> list = map.get(key);
list.add("1");

mAdapter.notifyDataSetChanged();
mEListView.expandGroup(groupCount);
}
});

return convertView;
}

@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return false;
}

class ViewHolder {
ImageView img;
}

}


代码 activity_main.xml 文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F3F4F5"
android:orientation="vertical" >

<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="52dp"
android:background="#FFF" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/hello_world" />

<ImageView
android:id="@+id/iv_shop_add"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="20dp"
android:onClick="addShop"
android:src="@drawable/ic_add" />
</RelativeLayout>

<ExpandableListView
android:id="@+id/elv_shoppingcart"
android:layout_width="match_parent"
android:scrollbars="none"
android:layout_height="wrap_content" >
</ExpandableListView>

</LinearLayout>


运行项目

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息