The second lesson of the complete APP project (including server source code) - imitation Jingdong product classification
First of all, the structure of Jingdong's product classification is divided into: the first-level classification on the left, the second-level classification on the right, and the third-level classification under the second-level classification, as shown in the following figure:
The analysis structure is as follows:
Yesterday was a listview and on the right was a GridView.
The list implementation is very common, but the GridView grouping currently does not provide this function in the Android SDK. Here we use a third-party library: StickyGridHeadersGridView You can go to github to download.
Part of the code snippet, main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@color/bg_activity"> <include layout="@layout/titlebar"/> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <ListView android:id="@+id/category_left_lstv" android:layout_width="match_parent" android:layout_height="fill_parent" android:layout_weight="3" android:choiceMode="singleChoice" android:divider="@color/separator_line" android:dividerHeight="1px" android:listSelector="#00000000" android:scrollbars="none" > </ListView> <com.tonicartos.widget.stickygridheaders.StickyGridHeadersGridView android:id="@+id/category_right_gdvv" android:layout_width="match_parent" android:layout_height="fill_parent" android:layout_marginLeft="@dimen/margin_space" android:layout_marginRight="@dimen/margin_space" android:layout_weight="1" android:numColumns="3" android:listSelector="@android:color/transparent" android:background="@color/bg_activity" android:scrollbars="none"/> </LinearLayout> </LinearLayout>
Use StickyGridHeadersGridView in code
mLeftLstv = (ListView)view.findViewById(R.id.category_left_lstv); mRightGdv = (StickyGridHeadersGridView)view.findViewById(R.id.category_right_gdvv); mRightGdv.setAreHeadersSticky(false); //Set the title bar to not slide as the list slides mLeftAdapter = new SPCategoryLeftAdapter(getActivity()); mRightAdapter = new SPCategoryRightAdapter(getActivity()); mLeftLstv.setAdapter(mLeftAdapter); mRightGdv.setAdapter(mRightAdapter);
The key is the implementation of Adapter:
Adapter needs to implement the implements StickyGridHeadersSimpleAdapter interface and implement the following two methods:
public long getHeaderId(int position)
The header ID, if the ID is different from the previous ID, getHeaderView will be called to create a HeadView
public View getHeaderView(int position, View convertView, ViewGroup parent)
This function returns a HeadView layout file.
@Override public long getHeaderId(int position) { SPCategory parentCategory = mCategoryLst.get(position).getParentCategory(); return parentCategory.getId() ; } @Override public View getHeaderView(int position, View convertView, ViewGroup parent) { HeaderViewHolder headerHolder; if (convertView == null) { headerHolder = new HeaderViewHolder(); convertView = LayoutInflater.from(mContext).inflate(R.layout.category_grid_title_item, parent, false); headerHolder.titleTxtv = (TextView) convertView.findViewById(R.id.catelogy_right_title_txtv); convertView.setTag (headerHolder); } else { headerHolder = (HeaderViewHolder) convertView.getTag(); } SPCategory parentCategory = mCategoryLst.get(position).getParentCategory(); headerHolder.titleTxtv.setText(parentCategory.getName()); return convertView; }
Complete source code download address: demo tpshop Android source code download