1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > ListView动态添加控件

ListView动态添加控件

时间:2021-10-20 17:43:34

相关推荐

ListView动态添加控件

在项目中有时候要用到ListView动态添加一些Item,今天研究了一下,一贯还是使用BaseAdapter

创建一个AddNumberBaseAdpater类用来适配ListView

import java.util.ArrayList;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.EditText;import android.widget.ImageButton;import com.example.mobilephone.R;public class AddNumberBaseAdpater extends BaseAdapter {private LayoutInflater mInflater;private ArrayList<String> text;public AddNumberBaseAdpater(Context context) {text = new ArrayList<String>();text.add("第一项");// 默认只加载一个Itemthis.mInflater = LayoutInflater.from(context);}public int getCount() {// TODO Auto-generated method stubreturn text.size();}public Object getItem(int position) {// TODO Auto-generated method stubreturn text.get(position);}public long getItemId(int position) {// TODO Auto-generated method stubreturn position;}public View getView(final int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubViewHolder holder = new ViewHolder();if (convertView == null) {convertView = mInflater.inflate(R.layout.addnumber, null);holder.btnOpen = (ImageButton) convertView.findViewById(R.id.ibtnAddNumber);holder.editText = (EditText) convertView.findViewById(R.id.editNumber);convertView.setTag(holder);} else {holder = (ViewHolder) convertView.getTag();}switch (position) {// 不要以为XML文件中是2个按钮,其实只有一个case 0:holder.btnOpen.setBackgroundResource(R.drawable.addnumber);// 第一项按钮则显示加号图片break;default:holder.btnOpen.setBackgroundResource(R.drawable.deletenumber);// 超过了一项则显示减号图片,可以删除break;}holder.btnOpen.setOnClickListener(new View.OnClickListener() {// 添加按钮public void onClick(View v) {if (position == 0) {text.add("xxx");// 添加一项控件} else if (position > 0) {// 始终留一项不能删除text.remove(text.size() - 1);// 删除按钮}notifyDataSetChanged();}});return convertView;}public final class ViewHolder {public EditText editText;public ImageButton btnOpen;}}

主MainActivity

import android.app.Activity;import android.os.Bundle;import android.widget.ListView;import com.example.mobilephone.adpater.AddNumberBaseAdpater;public class MainActivity extends Activity {private ListView myList; // ListView控件 private AddNumberBaseAdpater addNumberBaseAdpater;public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.moble_input); addNumberBaseAdpater=new AddNumberBaseAdpater(getApplicationContext());myList = (ListView) findViewById(R.id.listview1);myList.setAdapter(addNumberBaseAdpater);}}

自定义XML文件

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="/apk/res/android"android:id="@+id/relativeNumber"android:layout_width="500px"android:layout_height="100px"android:layout_below="@+id/relativeLayout1"android:layout_centerHorizontal="true"android:layout_marginTop="20dp" ><EditTextandroid:id="@+id/editNumber"android:layout_width="300px"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_alignParentLeft="true"android:gravity="center_vertical"android:layout_centerHorizontal="true"android:layout_marginLeft="16dp"android:background="@drawable/number_input"android:ems="10" /><ImageButtonandroid:id="@+id/ibtnAddNumber"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:focusable="true" android:layout_toRightOf="@+id/editNumber"android:background="@drawable/addnumber" /> </RelativeLayout>

主XML文件

<RelativeLayout xmlns:android="/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#FFFFFF" android:orientation="vertical" ><ListViewandroid:id="@+id/listview1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:cacheColorHint="#00000000"android:layout_centerHorizontal="true"android:divider="#FFFFFF"android:dividerHeight="0px"android:fadingEdge="none"android:listSelector="#00000000" > </ListView><TextViewandroid:id="@+id/textVie"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:layout_below="@+id/listview1"android:text="@string/input"android:textSize="18dp" /></RelativeLayout>

最后运行效果如下,第一次显示

点击加号按钮后运行效果如下:

点击减号删除

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。