怎么在Android中實(shí)現(xiàn)一個(gè)ListView點(diǎn)擊展開收起效果

怎么在Android中實(shí)現(xiàn)一個(gè)ListView點(diǎn)擊展開收起效果?相信很多沒有經(jīng)驗(yàn)的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。

創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),五大連池企業(yè)網(wǎng)站建設(shè),五大連池品牌網(wǎng)站建設(shè),網(wǎng)站定制,五大連池網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,五大連池網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  tools:context=".MainActivity" >
  <!--提示框-->
  <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="請選擇您的類型:"
    android:textSize="30sp"
    android:textColor="#ffffffff"
    android:paddingLeft="5dp"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:background="#ff000000"/>
  <!--定義一個(gè)ExpandableListView組件-->
  <ExpandableListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  </ExpandableListView>
</LinearLayout>

然后就是具體實(shí)現(xiàn):

這里主要是添加幾個(gè)必須的屬性 大多數(shù)方法不用重寫

參考我代碼中的位置稍加改動(dòng)就行

public class MainActivity extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //創(chuàng)建一個(gè)ExpandableListAdapter對象
    final ExpandableListAdapter adapter = new ExpandableListAdapter() {
      int[] logos = new int[]{
          R.drawable.human1st,
          R.drawable.human1st,
          R.drawable.human2nd,
          R.drawable.human3rd
      };
      private String[] humanTypes = new String[]{
          "不是人","聰明人","普通人","我這樣的人"
      };
      private String[][] humans = new String[][]{
          {"上仙","大神","荷蘭豬"},
          {"超人","一般聰明人","假的聰明人"},
          {"努力的人","快樂的普通人","苦逼的普通人"},
          {"天才","傻逼","蠢萌"}
      };
      //獲得制定組的位置、指定子列表項(xiàng)處的字列表項(xiàng)數(shù)據(jù)
      private TextView getTextView(){
        AbsListView.LayoutParams layoutParams =
            new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,164);
        TextView textView = new TextView(MainActivity.this);
        textView.setLayoutParams(layoutParams);
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        textView.setPadding(36,0,0,0);
        textView.setTextSize(30);
        return textView;
      }
      @Override
      public void registerDataSetObserver(DataSetObserver observer) {
      }
      @Override
      public void unregisterDataSetObserver(DataSetObserver observer) {
      }
      @Override
      public int getGroupCount() {
        return humanTypes.length;
      }
      @Override
      public int getChildrenCount(int groupPosition) {
        return humans[groupPosition].length;
      }
      //獲取制定組位置處的組數(shù)據(jù)
      @Override
      public Object getGroup(int groupPosition) {
        return humanTypes[groupPosition];
      }
      @Override
      public Object getChild(int groupPosition, int childPosition) {
        return humans[groupPosition][childPosition];
      }
      @Override
      public long getGroupId(int groupPosition) {
        return groupPosition;
      }
      @Override
      public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
      }
      @Override
      public boolean hasStableIds() {
        return true;
      }
      //該方法決定每個(gè)組選項(xiàng)的外觀
      @Override
      public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        LinearLayout linearLayout = new LinearLayout(MainActivity.this);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        ImageView logo = new ImageView(MainActivity.this);
        logo.setImageResource(logos[groupPosition]);
        linearLayout.addView(logo);
        TextView textView = getTextView();
        textView.setText(getGroup(groupPosition).toString());
        linearLayout.addView(textView);
//        linearLayout.setMinimumHeight(50);
        return linearLayout;
      }
      @Override
      public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        TextView textView = getTextView();
        textView.setText(getChild(groupPosition,childPosition).toString());
        return textView;
      }
      @Override
      public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
      }
      @Override
      public boolean areAllItemsEnabled() {
        return false;
      }
      @Override
      public boolean isEmpty() {
        return false;
      }
      @Override
      public void onGroupExpanded(int groupPosition) {
      }
      @Override
      public void onGroupCollapsed(int groupPosition) {
      }
      @Override
      public long getCombinedChildId(long groupId, long childId) {
        return 0;
      }
      @Override
      public long getCombinedGroupId(long groupId) {
        return 0;
      }
    };
    ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.list);
    expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
      @Override
      public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
        Toast.makeText(MainActivity.this,"你是一個(gè):" +
            adapter.getChild(groupPosition,childPosition),Toast.LENGTH_SHORT).show();
        return true;
      }
    });
    expandableListView.setAdapter(adapter);
  }
}

看完上述內(nèi)容,你們掌握怎么在Android中實(shí)現(xiàn)一個(gè)ListView點(diǎn)擊展開收起效果的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

網(wǎng)站欄目:怎么在Android中實(shí)現(xiàn)一個(gè)ListView點(diǎn)擊展開收起效果
網(wǎng)頁網(wǎng)址:http://bm7419.com/article12/gocpgc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)軟件開發(fā)、網(wǎng)頁設(shè)計(jì)公司企業(yè)建站、微信小程序、域名注冊

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)