您好,登錄后才能下訂單哦!
本篇文章為大家展示了怎么在android中實現列表的展開和收縮功能,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
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()); } //為列表的每一項創建上下文菜單(即長按后 呼出的菜單) @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { menu.setHeaderTitle("ContexMenu"); menu.add(0,0,0,"ContextMenu"); } //單擊上下文菜單后的邏輯 @Override public boolean onContextItemSelected(MenuItem item) { ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo)item.getMenuInfo(); String title = ((TextView) info.targetView).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; //父列表數據 private String[] groups ={"group1","group2","group3","group4",""}; //子列表數據 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; } //取子列表中的某一項的view @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; } //獲取某一項的view的邏輯 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; } }
Android是一種基于Linux內核的自由及開放源代碼的操作系統,主要使用于移動設備,如智能手機和平板電腦,由美國Google公司和開放手機聯盟領導及開發。
上述內容就是怎么在android中實現列表的展開和收縮功能,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。