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

《自定义组件》下拉快速选择组件

时间:2015-04-03 01:38:17      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:风飞雪未扬   自定义控件   下拉快速选择组件   

下拉快速选择组件

相信大家都有使用QQ的最近的登录的下拉框,点击可以显示最近登录的效果,点击选项可以将内容提交到输入框,实现快速输入数据的效果。

今天我们就来实现这个效果。

技术分享

首先使用先对布局实现界面

<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="match_parent"
    android:gravity="center_horizontal"
    tools:context=".MainActivity" >

    <EditText
       
        android:gravity="center_horizontal"
        android:id="@+id/tv_msg"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:hint="点击右侧快速选择" />

    <ImageView
        android:clickable="true"
       android:layout_marginTop="10dp"
        android:layout_alignRight="@id/tv_msg"
        android:id="@+id/iv_down_arrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/down_arrow" />

</RelativeLayout>


使用到listview,listview的布局

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

    <LinearLayout
        android:layout_marginBottom="5dp"
        android:gravity="center_vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/iv_user"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/user" />

        <TextView
            android:id="@+id/tv_msg"
            android:gravity="center_horizontal"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="000000000" />

        <ImageView
            android:id="@+id/iv_delete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/delete" />
    </LinearLayout>

</LinearLayout>


<span style="font-size:18px;">package comflyou.down.select;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.renderscript.Type.CubemapFace;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

public class MainActivity extends Activity {
  private EditText msg;
  private ImageView image;
  private PopupWindow popupWindow;
  private ArrayList<String> dataList;
  private ListView myListview;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    msg = (EditText) findViewById(R.id.tv_msg);
    image = (ImageView) findViewById(R.id.iv_down_arrow);
    myListview = new ListView(MainActivity.this);
    popupWindow = new PopupWindow(getApplicationContext());
    // 填充数据
    dataList = new ArrayList<String>();
    for (int i = 0; i < 20; i++) {
      dataList.add("flyou" + i);
    }
    // 初始化数据
    init();
    // 初始化lictview
    initListView();
  }

  private void init() {

    image.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        // 设置PopupWindow的宽度
        popupWindow.setWidth(msg.getWidth());
        // 设置PopupWindow的高度
        popupWindow.setHeight(300);
        popupWindow.setContentView(myListview);

        popupWindow.setOutsideTouchable(true);
        popupWindow.showAsDropDown(msg, 0, 0);
      }
    });

  }

  private void initListView() {

    myListview.setBackgroundResource(R.drawable.listview_background); // 设置listView
                                                                      // 背景
    myListview.setDivider(null); // 设置条目之间的分隔线为null
    myListview.setVerticalScrollBarEnabled(false); // 关闭
    myListview.setAdapter(new MyAdapter());

  }

  public class MyAdapter extends BaseAdapter {

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

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

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
      ViewHolder holder;
      if (convertView == null) {
        convertView = View.inflate(getApplicationContext(), R.layout.list_item, null);
        holder = new ViewHolder();
        holder.iv_delete = (ImageView) convertView.findViewById(R.id.iv_delete);
        holder.iv_user = (ImageView) convertView.findViewById(R.id.iv_user);
      holder.tv_msg=(TextView)convertView.findViewById(R.id.tv_msg);
        convertView.setTag(holder);
      } else {

        holder = (ViewHolder) convertView.getTag();

      }
      
      holder.tv_msg.setText(dataList.get(position));
      holder.iv_delete.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
          // 移除数据
          dataList.remove(position);
          //更新listview
          MyAdapter.this.notifyDataSetChanged();
        }
      });
      convertView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
          msg.setText(dataList.get(position));
          popupWindow.dismiss();
        }

      });
      return convertView;
    }

  
  }
 //数据缓存
  private class ViewHolder {
    ImageView iv_delete;
    TextView tv_msg;
    ImageView iv_user;

  }

}
</span>


《自定义组件》下拉快速选择组件

标签:风飞雪未扬   自定义控件   下拉快速选择组件   

原文地址:http://blog.csdn.net/u013616976/article/details/44841497

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