欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android列表控件實現(xiàn)展開、收縮功能

 更新時間:2018年11月28日 16:16:24   作者:ghd2000  
這篇文章主要為大家詳細(xì)介紹了android支持展開/收縮功能的列表控件,具有一定的參考價值,感興趣的小伙伴們可以參考一下

最近在做一個Rss閱讀器,我看了一看別人做的閱讀器中的lisView可以伸縮,展開,我就在網(wǎng)上搜索了一下。果然讓我找到,下面就我找到的一個小例子,給大家分享一下。 

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());
 }

 
 //為列表的每一項創(chuàng)建上下文菜單(即長按后 呼出的菜單)
 @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;
 
 //父列表數(shù)據(jù)
 private String[] groups ={"group1","group2","group3","group4",""};
 
 //子列表數(shù)據(jù)
 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;
 }
 
}

 運行的結(jié)果如下:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Flutter自定義Appbar搜索框效果

    Flutter自定義Appbar搜索框效果

    這篇文章主要為大家詳細(xì)介紹了Flutter自定義Appbar搜索框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • Flutter狀態(tài)管理Provider示例解析

    Flutter狀態(tài)管理Provider示例解析

    這篇文章主要為大家介紹了Flutter狀態(tài)管理Provider示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • Android自定義View彈性滑動Scroller詳解

    Android自定義View彈性滑動Scroller詳解

    這篇文章主要為大家詳細(xì)介紹了Android自定義View彈性滑動Scroller,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Android DialogUtils彈出窗工具類詳解

    Android DialogUtils彈出窗工具類詳解

    這篇文章主要為大家詳細(xì)介紹了Android DialogUtils彈出窗工具類,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • Android onActivityResult和setResult方法詳解及使用

    Android onActivityResult和setResult方法詳解及使用

    這篇文章主要介紹了Android onActivityResult和setResult方法詳解及使用的相關(guān)資料,這里提供實例,幫助大家學(xué)習(xí)理解,需要的朋友可以參考下
    2016-12-12
  • 獲取Android手機中所有短信的實現(xiàn)代碼

    獲取Android手機中所有短信的實現(xiàn)代碼

    這篇文章主要介紹了獲取Android手機中所有短信的實現(xiàn)代碼,需要的朋友可以參考下
    2014-08-08
  • Android滑動事件沖突的解決方法

    Android滑動事件沖突的解決方法

    這篇文章主要為大家詳細(xì)介紹了Android滑動事件沖突的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • Flutter自動路由插件auto_route使用詳解

    Flutter自動路由插件auto_route使用詳解

    這篇文章主要為大家介紹了Flutter自動路由插件auto_route的基本使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • Android GestureDetector用戶手勢檢測實例講解

    Android GestureDetector用戶手勢檢測實例講解

    這篇文章主要為大家詳細(xì)介紹了Android GestureDetector用戶手勢檢測實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • Android實現(xiàn)整理PackageManager獲取所有安裝程序信息

    Android實現(xiàn)整理PackageManager獲取所有安裝程序信息

    這篇文章主要介紹了Android實現(xiàn)整理PackageManager獲取所有安裝程序信息的方法,實例分析了Android使用PackageManager獲取安裝程序信息的具體步驟與相關(guān)技巧,需要的朋友可以參考下
    2016-01-01

最新評論