android 支援展開/收縮功能的清單控制項

來源:互聯網
上載者:User

    最近在做一個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;
 }
 
}

 

啟動並執行結果如下:

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.