码迷,mamicode.com
首页 > 其他好文 > 详细

ListView只允许展开其中一个item的方法

时间:2016-11-14 14:51:34      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:click   add   pre   android   launch   state   getc   dap   color   

 

xml文件代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_below="@id/image"
        android:id="@+id/relativelayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:id="@+id/line1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:visibility="gone">

            <TextView
                android:id="@+id/text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="5dp"
                android:text="我是文字 要被隐藏" />
        </LinearLayout>
    </RelativeLayout>

    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@mipmap/ic_launcher" />
</RelativeLayout>

 

java代码:

public class MainActivity extends AppCompatActivity {
    ListView listview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listview = (ListView) findViewById(R.id.listview);
    }


    @Override
    protected void onStart() {
        super.onStart();
        myAdapter = new MyAdapter();
        listview.setAdapter(myAdapter);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int childCount = ((RelativeLayout) view).getChildCount();
                if(myAdapter.getView() != null){
                    myAdapter.getView().setVisibility(View.GONE);
                }
                for (int i = 0; i < childCount; i++) {
                    int relativelayout = R.id.relativelayout;        //要隐藏布局的外层一个容器ID
                    int id1 = ((RelativeLayout) view).getChildAt(i).getId();
                    if(relativelayout == id1){//如果是要隐藏布局的外层一个容器 就循环寻找要隐藏的容器
                        RelativeLayout childAt = (RelativeLayout) ((RelativeLayout) view).getChildAt(i);
                        int childCount1 = childAt.getChildCount();
                        for (int j = 0; j < childCount1; j++) {
                            int line1 = R.id.line1;
                            int id2 = childAt.getChildAt(j).getId();
                            if(line1 == id2){
                                childAt.getChildAt(j).setVisibility(View.VISIBLE);
                                myAdapter.setView((LinearLayout) childAt.getChildAt(j));
                            }
                        }
                    }
                }

            }
        });
    }


    MyAdapter myAdapter;

    class MyAdapter extends BaseAdapter {
        LinearLayout view;  //要隐藏或者展示的view   ——如果是多个,可以修改为一个Entity
        public LinearLayout getView(){
            return view;
        }
        public void setView(LinearLayout view){
            this.view = null;
            this.view = view;
        }
        @Override
        public int getCount() {
            return 7;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final ViewHolder vh;
            if (convertView == null) {
                vh = new ViewHolder();
                convertView = View.inflate(MainActivity.this, R.layout.item, null);
                vh.image = (ImageView) convertView.findViewById(R.id.image);
                vh.text = (TextView) convertView.findViewById(R.id.text);
                convertView.setTag(vh);
            } else {
                vh = (ViewHolder) convertView.getTag();
            }
            return convertView;
        }

        class ViewHolder {
            ImageView image;
            TextView text;
        }
    }
}

耦合度很高,只要布局文件里面的结构改变,onItemClick的代码就要改。。。。

ListView只允许展开其中一个item的方法

标签:click   add   pre   android   launch   state   getc   dap   color   

原文地址:http://www.cnblogs.com/yaxiaoke/p/6061494.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!