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

下拉快速选择组件

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

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

《自定义组件》下拉快速选择组件_第1张图片

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



    

    



使用到listview,listview的布局




    

        

        

        
    



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 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();
    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;

  }

}


你可能感兴趣的:(自定义控件)