android的ListView点击item使item展开的做法
Posted cxchanpin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android的ListView点击item使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>
效果图:
以上是关于android的ListView点击item使item展开的做法的主要内容,如果未能解决你的问题,请参考以下文章
Android中Listview点击item不变颜色以及设置listselector 无效
Android 中listview嵌套listview,外面listview的item点击事件失效
Android 中listview嵌套listview,外面listview的item点击事件失效