Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android的ListView點擊item使item展開的做法

android的ListView點擊item使item展開的做法

編輯:關於Android編程

直接上代碼把,主要是重新給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 myList = new ArrayList();
	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
Layout:



    
    

    

        
        
    


效果圖:


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

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved