码迷,mamicode.com
首页 > 移动开发 > 详细

android的ListView点击item使item展开的做法

时间:2014-08-16 11:16:00      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:listview   item展开   

直接上代码把,主要是重新给item measure高度,直接上代码把

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

	private ArrayList<MyData> myList = new ArrayList<MyData>();
	private myAdapter myAdapter;
	private LayoutInflater inflater = null;
	private int oldPostion = -1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		final ListView list = (ListView) findViewById(R.id.draggable_list);
		MyData data = new MyData();
		data.name = "11";
		myList.add(data);
		
		data = new MyData();
		data.name = "22";
		myList.add(data);
		
		data = new MyData();
		data.name = "33";
		myList.add(data);
		
		data = new MyData();
		data.name = "44";
		myList.add(data);
		
		data = new MyData();
		data.name = "55";
		myList.add(data);
		
		data = new MyData();
		data.name = "66";
		myList.add(data);
		
		data = new MyData();
		data.name = "77";
		myList.add(data);
		
	
		inflater = LayoutInflater.from(this);
		TextView header = (TextView)inflater.inflate(R.layout.header, null);
		TextView footer = (TextView)inflater.inflate(R.layout.footer, null);

		//list.addFooterView(footer);
		//list.addHeaderView(header);
		myAdapter = new myAdapter();
		list.setAdapter(myAdapter);
		
		list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				MyData data = myList.get(position);
				if (oldPostion == position) {
					if (data.expand)  {
						oldPostion = -1;
					}
					data.expand = !data.expand;
				}else{
					oldPostion = position;
					data.expand = true;
				}
				
				int totalHeight = 0;
				for(int i=0;i<myAdapter.getCount();i++) {
					View viewItem = myAdapter.getView(i, null, list);//这个很重要,那个展开的item的measureHeight比其他的大
					viewItem.measure(0, 0);
					totalHeight += viewItem.getMeasuredHeight();
				}
				
				ViewGroup.LayoutParams params = list.getLayoutParams();
				params.height = totalHeight
						+ (list.getDividerHeight() * (list.getCount() - 1));
				list.setLayoutParams(params);
				myAdapter.notifyDataSetChanged();
			}
		});
		Button btn = (Button)findViewById(R.id.btn);
		btn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
			}
		});
		
		
	}

	
	private class myAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return myList.size();
		}

		@Override
		public Object getItem(int position) {
			return myList.get(position);
		}

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

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			MyTag tag = new MyTag();
			MyData data = myList.get(position);
			if(convertView == null) {
				convertView = inflater.inflate(R.layout.item_layout, null);
				tag.item1 = (TextView)convertView.findViewById(R.id.item1);
				tag.item2 = (RelativeLayout)convertView.findViewById(R.id.item2);
				convertView.setTag(tag);
			}else{
				tag = (MyTag)convertView.getTag();
			}
			if(data.expand) {
				tag.item2.setVisibility(View.VISIBLE);
			}else{
				tag.item2.setVisibility(View.GONE);
			}
			
			tag.item1.setText(data.name);
			return convertView;
		}
		
	}
	
	private class MyTag{
		private TextView item1;
		private RelativeLayout item2;
	}
	
	private class MyData{
		boolean expand;
		String name;
	}
	
}

Layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="50dp" >

    <TextView
        android:id="@+id/item1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:text="Header"
        android:textColor="#000000"
        android:textSize="20sp" >
    </TextView>

    <RelativeLayout
        android:id="@+id/item2"
        android:layout_below="@id/item1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="35dp"
        android:background="#fffeee"
        android:visibility="gone" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:text="Expand"
            android:textColor="#000000"
            android:textSize="16sp" >
        </TextView>
    </RelativeLayout>


</RelativeLayout>

效果图:

bubuko.com,布布扣

代码:http://download.csdn.net/detail/baidu_nod/7765251

android的ListView点击item使item展开的做法,布布扣,bubuko.com

android的ListView点击item使item展开的做法

标签:listview   item展开   

原文地址:http://blog.csdn.net/baidu_nod/article/details/38610749

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