In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how to use ExpandableListView which supports multiple choices. It is very detailed and has a certain reference value. Friends who are interested must finish it!
Goals (requirements):
1. Create an expandable and retractable list
two。 The list item contains multiple checkable parts, and when you select a row, the checkable parts that the row contains need to be changed accordingly
3. Multiple list items can be selected and can be read out
Result picture:
Achieve:
1. Create a master layout for planning list display. For specific list items, we also create a layout file for implementation convenience.
two。 Similarly, ListView,ExpandableListView manages the various elements and operations it contains through Adapter. Here we create an Adapter that extends from BaseExpandableListAdapter. Unlike ListView, ExpandableListAdapter renders operations that implement two levels of View (Group level and list item level). It renders the Group items through getGroupView () and the list subitems through getChildView ().
@ Override public View getGroupView (int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {View groupView = convertView; if (groupView = = null) {groupView = new TextView (context); ((TextView) groupView) .setGravity (Gravity.LEFT | Gravity.CENTER_VERTICAL); groupView.setPadding ((TextView) groupView) .setText (groupData[ groupPosition]) ((TextView) groupView) .setTextColor (context.getResources (). GetColor (R.color.fgcolor)); return groupView;} @ Override public View getChildView (final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {View itemView = convertView; final ViewHolder vh; if (itemView = = null) {LayoutInflater inflater = (LayoutInflater) context.getSystemService (Context.LAYOUT_INFLATER_SERVICE) ItemView = inflater.inflate (R.layout.item_view, null); vh = new ViewHolder (); vh.layout = (CheckableLinearLayout) itemView.findViewById (R.id.layout); vh.item = (TextView) itemView.findViewById (R.id.item); itemView.setTag (vh);} else {vh = (ViewHolder) itemView.getTag ();} vh.item.setText (itemData[ groupPosition] [childPosition]) Final ExpandableListView listView = (ExpandableListView) ((MainActivity) context) .findViewById (R.id.list); final int position = listView.getFlatListPosition (ExpandableListView.getPackedPositionForChild (groupPosition, childPosition)); listView.setItemChecked (position, checkedState [groupPosition] [childPosition]); vh.layout.setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {(CheckableLinearLayout) v) .toggle () CheckedState [groupPosition] [childPosition] =! checkedState [groupPosition] [childPosition]; listView.setItemChecked (position, ((CheckableLinearLayout) v). IsChecked ();}}); return itemView;}
3. Create OnClickListener listening mouse click events for each list subitem container. Note here that because the list subitems contain CheckBox, in order to keep click events from being captured by CheckBox, we need to create a class that extends from CheckBox to shield mouse and keyboard events. At the same time, you need to search this container for the checkable parts it contains and pass the check operation to those parts.
The method getChildView () in Adapter needs to implement a mouse click listener:
Public View getChildView (final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {View itemView = convertView; final ViewHolder vh;... Final int position = listView.getFlatListPosition (ExpandableListView.getPackedPositionForChild (groupPosition, childPosition)); listView.setItemChecked (position, checkedState [groupPosition] [childPosition]); vh.layout.setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {((CheckableLinearLayout) v). Toggle (); checkedState [groupPosition] [childPosition] =! checkedState [groupPosition] [childPosition]; listView.setItemChecked (position, (CheckableLinearLayout) v). IsChecked ();}}); return itemView;}
InertCheckBox extended from CheckBox requires masking keyboard and mouse events
Public class InertCheckBox extends CheckBox {@ Override public boolean onKeyDown (int keyCode, KeyEvent event) {/ / return false return false;} @ Override public boolean onKeyUp (int keyCode, KeyEvent event) {/ / return false return false;} @ Override public boolean onTouchEvent (MotionEvent event) {/ / return false return false directly } @ Override public boolean onKeyMultiple (int keyCode, int repeatCount, KeyEvent event) {/ / return false return false; directly.}
The list item container needs to implement the Checkable interface and pass the check operation to its checkable child
Public class CheckableLinearLayout extends LinearLayout implements Checkable {... @ Override public void setChecked (boolean checked) {this.isChecked = checked; for (Checkable view: checkableViews) {view.setChecked (checked);}} @ Override public boolean isChecked () {return this.isChecked;} @ Override public void toggle () {isChecked =! isChecked; for (Checkable view: checkableViews) {view.toggle () } @ Override protected void onFinishInflate () {super.onFinishInflate (); for (int iTuno; I)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.