Recently I was working on an Rss reader. I saw that lisView in another reader can be scaled and expanded, and I searched the internet. Let me find it. Here is a small example.
ActivityMain. java
Package com. android;
Import android. app. ExpandableListActivity;
Import android. OS. Bundle;
Import android. view. ContextMenu;
Import android. view. MenuItem;
Import android. view. View;
Import android. view. ContextMenu. ContextMenuInfo;
Import android. widget. ExpandableListAdapter;
Import android. widget. ExpandableListView;
Import android. widget. TextView;
Import android. widget. Toast;
Import android. widget. ExpandableListView. ExpandableListContextMenuInfo;
Public class ActivityMain extends ExpandableListActivity {
Private ExpandableListAdapter mAdapter;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
This. setTitle ("ExpandableList ");
MAdapter = new MyExpandableListAdapter (this );
SetListAdapter (mAdapter );
RegisterForContextMenu (this. getExpandableListView ());
}
// Create a context menu for each item in the list (that is, a long-pressed and outgoing menu)
@ Override
Public void onCreateContextMenu (ContextMenu menu, View v,
ContextMenuInfo menuInfo ){
Menu. setHeaderTitle ("ContexMenu ");
Menu. add (0, 0, 0, "ContextMenu ");
}
// Click the logic behind the context menu
@ Override
Public boolean onContextItemSelected (MenuItem item ){
ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item. getMenuInfo ();
String title = (TextView) info.tar getView). getText (). toString ();
Int type = ExpandableListView. getPackedPositionType (info. packedPosition );
If (type = ExpandableListView. PACKED_POSITION_TYPE_CHILD)
{
Int groupPos = ExpandableListView. getPackedPositionGroup (info. packedPosition );
Int childPos = ExpandableListView. getPackedPositionChild (info. packedPosition );
Toast. makeText (this, title + "-Group Index" + groupPos + "Child Index:" + childPos,
Toast. LENGTH_SHORT). show ();
Return true;
}
Return false;
}
MyExpandableListAdapter. java
Package com. android;
Import android. content. Context;
Import android. view. Gravity;
Import android. view. View;
Import android. view. ViewGroup;
Import android. widget. AbsListView;
Import android. widget. BaseExpandableListAdapter;
Import android. widget. TextView;
Public class MyExpandableListAdapter extends BaseExpandableListAdapter {
Private Context mContext;
// Parent List Data
Private String [] groups = {"group1", "group2", "group3", "group4 ",""};
// Sublist data
Private String [] [] children = {
{"Child1 "},
{"Child1", "child2 "},
{"Child1", "child2", "child3 "},
{"Child1", "child2", "child3", "child4 "},
};
MyExpandableListAdapter (Context context ){
MContext = context;
}
@ Override
Public Object getChild (int groupPosition, int childPosition ){
// TODO Auto-generated method stub
Return children [groupPosition] [childPosition];
}
@ Override
Public long getChildId (int groupPosition, int childPosition ){
// TODO Auto-generated method stub
Return childPosition;
}
// Obtain the view of an item in the sublist
@ Override
Public View getChildView (int groupPosition, int childPosition,
Boolean isLastChild, View convertView, ViewGroup parent ){
TextView textView = getGenericView ();;
TextView. setText (getChild (groupPosition, childPosition). toString ());
Return textView;
}
@ Override
Public int getChildrenCount (int groupPosition ){
// TODO Auto-generated method stub
Return children [groupPosition]. length;
}
@ Override
Public Object getGroup (int groupPosition ){
Return groups [groupPosition];
}
@ Override
Public int getGroupCount (){
// TODO Auto-generated method stub
Return groups. length;
}
@ Override
Public long getGroupId (int groupPosition ){
// TODO Auto-generated method stub
Return groupPosition;
}
@ Override
Public View getGroupView (int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent ){
TextView textView = getGenericView ();
TextView. setText (getGroup (groupPosition). toString ());
Return textView;
}
@ Override
Public boolean hasStableIds (){
// TODO Auto-generated method stub
Return true;
}
@ Override
Public boolean isChildSelectable (int groupPosition, int childPosition ){
// TODO Auto-generated method stub
Return true;
}
// Obtain the view logic of a certain item
Private TextView getGenericView (){
AbsListView. LayoutParams lp = new AbsListView. LayoutParams (ViewGroup. LayoutParams. FILL_PARENT, 48 );
TextView textView = new TextView (mContext );
TextView. setLayoutParams (lp );
TextView. setGravity (Gravity. CENTER_VERTICAL | Gravity. LEFT );
TextView. setPadding (32, 0, 0, 0 );
Return textView;
}
}
The running result is as follows: