Android Custom Component Listpopwindow_android

Source: Internet
Author: User

First look at the effect:

The effect is this, look at the implementation, in fact, it is not difficult, is to open up for small partners to use, if there is unreasonable place, I hope everyone will correct me.
1. Custom Popwindow
First of all, we analyze, this effect is definitely a popwindow nested listview, and the above title, and the following cancel is two text boxes, to achieve relatively simple.
We then declare two interfaces in the Popwindow to recall the click events of Cancel and item

public interface onpopitemclicklistener{
  void Onpopitemclick (View view,int position);

 public interface onbottomtextviewclicklistener{
  void Onbottomclick ();
 }

And then set some of the Popwindow properties

Parentview = Layoutinflater.from (context). Inflate (r.layout.list_popwindow,null);
  Setcontentview (Parentview);
  LV = (ListView) Parentview.findviewbyid (R.id.lv_popwindow);
  Sets the high
  this.setwidth (ViewGroup.LayoutParams.MATCH_PARENT) of the pop-up form;
  This.setheight (ViewGroup.LayoutParams.MATCH_PARENT);
  Set pop-up form to click
  this.setfocusable (TRUE);
  Instantiate a colordrawable color for semitransparent
  colordrawable DW = new colordrawable (0xb0000000);
  Sets the background
  this.setbackgrounddrawable (DW) of the Selectpicpopupwindow pop-up form;

Take a look at the whole code:

public class Listpopwindow extends popupwindow{the private context;  Context private View Parentview; Parent view private list<popbean> dataList; Item data source private Onpopitemclicklistener listener; Item click on the interface private ListView LV; Item List view private view viewtop; Title View private String Toptext,bottomtext; Title text, bottom text private TextView tvtop,tvbottom; Title text, bottom text private popwindowadapter adapter; Adapter Private Onbottomtextviewclicklistener bottomlistener;//Bottom Click interface public interface onpopitemclicklistener{void on
 Popitemclick (View view,int position);
 public interface onbottomtextviewclicklistener{void Onbottomclick (); } public Listpopwindow (context Context,onpopitemclicklistener Listener,onbottomtextviewclicklistener BottomListener
  , View parentview,list<popbean> datalist,string bottomtext,string toptext) {this.context = context;
  This.listener = listener;
  This.parentview = Parentview;
  This.datalist = dataList; This.bottomlIstener = Bottomlistener;
  This.toptext = TopText;

  This.bottomtext = Bottomtext;
 Initviews ();
  private void Initviews () {Parentview = Layoutinflater.from (context). Inflate (R.layout.list_popwindow,null);
  Setcontentview (Parentview);
  LV = (ListView) Parentview.findviewbyid (R.id.lv_popwindow);
  Sets the high this.setwidth (ViewGroup.LayoutParams.MATCH_PARENT) of the pop-up form;
  This.setheight (ViewGroup.LayoutParams.MATCH_PARENT);
  Set pop-up form to click This.setfocusable (TRUE);
  Instantiate a colordrawable color for semitransparent colordrawable DW = new colordrawable (0xb0000000);

  Sets the background this.setbackgrounddrawable (DW) of the Selectpicpopupwindow pop-up form; View add Ontouchlistener listening judgment get touch position if outside the layout, destroy the pop-up Parentview.setontouchlistener (new View.ontouchlistener () {public
    Boolean Ontouch (View V, motionevent event) {int height = Parentview.findviewbyid (r.id.ll_bottom). GetTop ();
    int y = (int) event.gety ();
     if (event.getaction () = = motionevent.action_up) {if (Y > height) {dismiss (); } RETurn true;

  }
  });
  Update ();
  Viewtop = Parentview.findviewbyid (r.id.view_line1);
  Tvbottom = (TextView) Parentview.findviewbyid (R.id.tv_popwindow_bottom);
  Tvtop = (TextView) Parentview.findviewbyid (R.id.tv_popwindow_first);
  adapter = new Popwindowadapter (context,datalist,false);

  Lv.setadapter (adapter); if (!
   Textutils.isempty (TopText)) {tvtop.setvisibility (view.visible);
   Tvtop.settext (TopText);
  Viewtop.setvisibility (view.visible);
   else {tvtop.setvisibility (view.gone);
  Viewtop.setvisibility (View.gone); } if (!
   Textutils.isempty (Bottomtext)) {tvbottom.setvisibility (view.visible);
  Tvbottom.settext (Bottomtext);
  else {tvbottom.setvisibility (view.gone); Lv.setonitemclicklistener (New Adapterview.onitemclicklistener () {@Override public void Onitemclick (Adapterview
   <?> Adapterview, view view, int I, long l) {Listener.onpopitemclick (view, i);

  }
  });
 Tvbottom.setonclicklistener (New View.onclicklistener () {  @Override public void OnClick (view view) {Bottomlistener.onbottomclick ();

 }
  });
 }

}

2. Look at some item of beans

Here I declare the title and the ID of the picture

Package com.hankkin.library;


public class Popbean {
 private String title;
 private int icon_res;

 Public String GetTitle () {return
  title;
 }

 public void Settitle (String title) {
  this.title = title;
 }

 public int geticon_res () {return
  icon_res;
 }

 public void seticon_res (int icon_res) {
  this.icon_res = icon_res;
 }

 Public Popbean (String title, int icon_res) {
  this.title = title;
  This.icon_res = icon_res;
 }


3. Custom Adapter Adapters

Here may want to note is the item background settings, some of the upper part of the rounded corners, some of the lower half of the rounded corners, special treatment

@Override public
 View getview (int i, view view, ViewGroup viewgroup) {
  Viewholder holder;
  if (view = = null) {
   view = inflater.inflate (R.layout.listview_popwindow_item, null);
   Holder = new Viewholder ();
   Holder.tv_name = (TextView) View.findviewbyid (r.id.tv_title);
   Holder.v_line = (View) View.findviewbyid (r.id.v_line);
   View.settag (holder);
  } else {
   holder = (viewholder) view.gettag ();
  }

  Holder.tv_name.setText (Datalist.get (i). GetTitle ());


  if (Datalist.size ()-1 = i) {
   holder.v_line.setVisibility (view.invisible);
   Holder.tv_name.setBackground (Context.getresources (). getdrawable (R.drawable.selector_bottom_half));
  else {
   holder.v_line.setVisibility (view.visible);
   Holder.tv_name.setBackground (Context.getresources (). getdrawable (R.drawable.list_gray_item));
  return view;
 }

Finally look at the call

The activity needs to implement the item interface (Onpopitemclicklistener) and the bottom button interface (Onbottomtextviewclicklistener)

public void Show (view view) {
  list<popbean> pops = new arraylist<> ();
  for (int i=0;i<5;i++) {
   Popbean pop = new Popbean ("item" +i,0);
   Pops.add (POP);
  }
  Popwindow = new Listpopwindow (Mainactivity.this,this,this,rl,pops, "Cancel", "title");
  Popwindow.showatlocation (RL, gravity.center| gravity.bottom,0,0);
 }

The above is the whole content of this article, hope to be able to help everybody's study.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.