美文网首页
android - listView点击展开收起实现

android - listView点击展开收起实现

作者: geaosu | 来源:发表于2017-09-20 00:55 被阅读0次

    废话不多说, 上图:

    GIF.gif

    上思路:
    动态改变数据适配器中的getCount()方法的返回值;
    点击刷新适配器中的数据;

    注意:
    1.这是在普通的布局中实现;
    2.如需要在ScrollView中实现该功能, 需要解决嵌套产生的ListView条目显示不全问题;
    3.请看下一篇文章介绍;

    上代码:

    代码01 - listview条目布局的xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:orientation="horizontal">
    
        <ImageView
            android:id="@+id/icon"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginRight="10dp"
            android:src="@drawable/icon2" />
    
        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:textSize="16sp"
            android:text="陈坤" />
    </LinearLayout>
    

    效果图:

    ListView条目布局效果图.png

    代码02 - mainActivity的布局文件xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="com.geaosu.myapplication.MainActivity">
    
        <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
        </ListView>
    
        <TextView
            android:id="@+id/loadMore"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#A4D3FF"
            android:gravity="center_horizontal"
            android:paddingBottom="20dp"
            android:paddingTop="20dp"
            android:textSize="16sp" />
    
    </LinearLayout>
    

    效果图:

    MainActivity布局效果图.png

    代码03 - MainActivity代码

    package com.geaosu.myapplication;
    
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.ImageView;
    import android.widget.ListView;
    import android.widget.TextView;
    
    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    
        private ListView list;
        private TextView loadMore;
    
        private String[] arr1 = {"geaosu01", "geaosu02", "geaosu03", "geaosu04", "geaosu05"};
    
        private boolean isShowMore = false;
        private MoreAdapter moreAdapter;
        private int mCount = 1;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            list = findViewById(R.id.list);
            moreAdapter = new MoreAdapter();
            list.setAdapter(moreAdapter);
    
            loadMore = findViewById(R.id.loadMore);
            loadMore.setOnClickListener(this);
            loadMore.setText("点击展开");
    
        }
    
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.loadMore:
                    if (isShowMore) {
                        loadMore.setText("点击展开");
                    } else {
                        loadMore.setText("点击收起");
                    }
                    isShowMore = !isShowMore;
                    moreAdapter.notifyDataSetChanged();
                    break;
            }
        }
    
        private class MoreAdapter extends BaseAdapter {
    
            @Override
            public int getCount() {
                // 重点区域
                if (isShowMore) {
                    return arr1.length;
                } else {
                    return mCount;
                }
            }
    
            @Override
            public Object getItem(int i) {
                return arr1[i];
            }
    
            @Override
            public long getItemId(int i) {
                return i;
            }
    
            @Override
            public View getView(int i, View view, ViewGroup viewGroup) {
                View itemView = View.inflate(MainActivity.this, R.layout.item_list, null);
    
                TextView name = itemView.findViewById(R.id.name);
                ImageView icon = itemView.findViewById(R.id.icon);
    
                name.setText(arr1[i]);
    
                return itemView;
    
            }
        }
    }
    

    ============== 结束 ==============

    努力让世界变得更好 ------ geaosu

    相关文章

      网友评论

          本文标题:android - listView点击展开收起实现

          本文链接:https://www.haomeiwen.com/subject/chknsxtx.html