标签:listview popupwindow interface 布局
在这篇博客之前,还写了一篇关于PopupWindow,那篇主要是关于PopupWindow弹出位置的设置。以及选择PopupWindow布局后的监听。详情看Android popupwindow 示例程序一。接下来这篇主要是讲自定义PopupWindow以及参数传递与返回,我在里面写了一个listview来示例。接下来看代码,都有所注释。
本文项目资源下载:
一、MainActivity
<span style="background-color: rgb(240, 240, 240);">package com.example.popupwimdow;</span> import java.util.ArrayList; import com.example.popupwimdow.TestPopwindow2.OnGetData; import android.os.Bundle; import android.app.Activity; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.PopupWindow.OnDismissListener; import android.widget.Toast; /** * * @author mmsx * 博主博客网址: http://blog.csdn.net/qq_16064871 */ public class MainActivity extends Activity implements OnClickListener, OnDismissListener { private ArrayList<ItemTest> mArrayList = new ArrayList<ItemTest>(); private TestPopwindow2 mTestPopwindow2 = null; private int mnSeclectItem = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); InitUI(); } private void InitUI() { if (mArrayList != null) { mArrayList.clear(); for (int i = 0; i < 3; i++) { ItemTest itemTest1 = new ItemTest(R.drawable.bmp1, "图片一"); mArrayList.add(itemTest1); ItemTest itemTest2 = new ItemTest(R.drawable.bmp2, "图片二"); mArrayList.add(itemTest2); } } // 实例化TestPopwindow2 mTestPopwindow2 = new TestPopwindow2(this); // 设置点击其他位置mTestPopwindow2消失 mTestPopwindow2.setOnDismissListener(this); Button buttonTest2 = (Button) findViewById(R.id.buttonTest2); buttonTest2.setOnClickListener(this); } private void OnPopwindowTest2() { if (mTestPopwindow2 == null) return; //回到接受 mTestPopwindow2.setOnData(new OnGetData() { //记录上一次选中的item @Override public int onSeclectItem() { return mnSeclectItem; } //回调接受函数 @Override public void onDataCallBack(int nSectlect,ArrayList<ItemTest> mArrayList) { Toast.makeText(getApplicationContext(), "listview 的点击" + String.valueOf(nSectlect), Toast.LENGTH_SHORT).show(); mnSeclectItem = nSectlect; } //传递数据源过去 @Override public ArrayList<ItemTest> onArrayList() { return mArrayList; } }); // location获得控件的位置 int[] location = new int[2]; View v = findViewById(R.id.buttonTest2); if (v != null) v.getLocationOnScreen(location); // 控件在屏幕的位置 mTestPopwindow2.setAnimationStyle(R.style.AppBaseTheme); // mTestPopwindow2弹出在某控件(button)的下面 mTestPopwindow2.showAtLocation(v, Gravity.TOP | Gravity.LEFT, location[0] - v.getWidth(), location[1] + v.getHeight()); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.buttonTest2: OnPopwindowTest2(); break; default: break; } } // 点击其他地方消失 @Override public void onDismiss() { } }二、TestPopwindow2
package com.example.popupwimdow; import java.util.ArrayList; import android.app.ActionBar.LayoutParams; import android.app.Activity; import android.content.Context; import android.graphics.drawable.BitmapDrawable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.PopupWindow; import android.widget.TextView; /** * 自定义TestPopwindow2 * @author mmsx * 博主博客网址: http://blog.csdn.net/qq_16064871 */ public class TestPopwindow2 extends PopupWindow implements OnItemClickListener{ // 根视图 private View mRootView; private LayoutInflater mInflater; // ArrayList数组,listview原始数据 private ArrayList<ItemTest> mArrayList; // 数据接口 OnGetData mOnGetData; // listview适配器 SelfTypeAdapt mSelfTypeAdapt; private int mnSeclectItem = 0; public TestPopwindow2(Activity context) { super(context); InitData(context); InitUI(); } // 数据接口设置,数据源接口传入 public void setOnData(OnGetData sd) { mOnGetData = sd; mArrayList = new ArrayList<ItemTest>(); if (mOnGetData != null) { mArrayList = mOnGetData.onArrayList(); mnSeclectItem = mOnGetData.onSeclectItem(); } } // 数据接口抽象方法 public interface OnGetData { abstract ArrayList<ItemTest> onArrayList(); abstract int onSeclectItem(); abstract void onDataCallBack(int nSectlect, ArrayList<ItemTest> mArrayList); } private void InitData(Context context) { mInflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); mRootView = mInflater.inflate(R.layout.test_popwindow_2, null); setContentView(mRootView); this.setWidth(LayoutParams.FILL_PARENT); this.setHeight(LayoutParams.WRAP_CONTENT); // 设置PopUpWindow弹出的相关属性 setTouchable(true); setOutsideTouchable(true); setFocusable(true); setBackgroundDrawable(new BitmapDrawable(context.getResources())); update(); getContentView().setFocusableInTouchMode(true); getContentView().setFocusable(true); setAnimationStyle(R.style.AppBaseTheme); } private void InitUI() { ListView listView = (ListView) mRootView .findViewById(R.id.listView1); mSelfTypeAdapt = new SelfTypeAdapt(mRootView.getContext()); listView.setAdapter(mSelfTypeAdapt); listView.setOnItemClickListener(this); } public class SelfTypeAdapt extends BaseAdapter { private Context mContext; private LayoutInflater mLayoutInflater; public SelfTypeAdapt(Context context) { mContext = context; mLayoutInflater = (LayoutInflater) mContext .getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { return mArrayList == null ? 0 : mArrayList.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder = null; if (convertView == null) { viewHolder = new ViewHolder(); convertView = (LinearLayout) mLayoutInflater.inflate( R.layout.listview_item, null); viewHolder.textView = (TextView) convertView .findViewById(R.id.textViewTest); viewHolder.imageView = (ImageView) convertView .findViewById(R.id.imageView1); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } ItemTest itemTest = new ItemTest(); itemTest = mArrayList.get(position); viewHolder.textView.setText(itemTest.getImageViewID()); viewHolder.imageView.setImageResource(itemTest.getImageViewID()); return convertView; } } // 内部类实现,提升listview效率 class ViewHolder { public ImageView imageView; public TextView textView; } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { mOnGetData.onDataCallBack(position, mArrayList); dismiss(); } }三、listview里面的实体类ItemTest
package com.example.popupwimdow; /** * 实体类 ItemTest * @author mmsx * 博主博客网址: http://blog.csdn.net/qq_16064871 */ public class ItemTest { private int mImageViewID; private String mstrName; public ItemTest(){ } public ItemTest(int ImageViewID, String strName) { this.mImageViewID = ImageViewID; this.mstrName = strName; } public int getImageViewID() { return mImageViewID; } public String getstrName() { return mstrName; } }四、布局文件
1、activity_main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayoutMain" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="right"> <Button android:id="@+id/buttonTest2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="测试2" /> </LinearLayout> </LinearLayout>2、test_popwindow_2
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#00ff" > <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" > </ListView> </LinearLayout>3、listview_item
<?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="50dp" android:descendantFocusability="blocksDescendants" android:orientation="horizontal" > <ImageView android:id="@+id/imageView1" android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/bmp1" /> <TextView android:id="@+id/textViewTest" android:layout_width="0dp" android:layout_height="50dp" android:layout_weight="1" android:gravity="center" android:text="TextView" android:textColor="#FF236A9C" /> </LinearLayout>在这上面主要展示的是点击button后,在这个button下方弹出PopupWindow。PopupWindow的内容是一个listview布局。那listview的数据源使用抽象接口传进去以及返回选择后的listview的item信息。这是一个灵活使用PopupWindow之一,大可以根据实际需要,做出不同的效果。
五、实现效果
项目资源下载:
转载请注明出处的博客网址: http://blog.csdn.net/qq_16064871
如有转载未注明出处博客网址,或者没有得到作者的同意。作者持有版权所有权。
Android 自定义PopupWindow以及参数传递与返回
标签:listview popupwindow interface 布局
原文地址:http://blog.csdn.net/qq_16064871/article/details/46489359