android 목록 컨트롤 전개,수축 기능 구현

최근 에 Rss 리 더 를 만 들 고 있 는데 다른 사람 이 만 든 리더 중의 lisView 가 신축 되 고 펼 쳐 지 는 것 을 보고 인터넷 에서 검색 해 봤 습 니 다.과연 찾 게 해 주 셨 군요.다음은 제 가 찾 은 작은 예 입 니 다.여러분 께 공유 해 드 리 겠 습 니 다. 
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;
 }
 
}
 실행 결 과 는 다음 과 같 습 니 다.

이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기