PopupWindow扩展
来源:互联网 发布:房价m2知乎 编辑:程序博客网 时间:2024/06/05 18:19
1.MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private LinearLayout mLinearLeft; private LinearLayout mLinearRight; private ListView mListBottom; private LinearLayout mActivityMain; private PopupWindow popupWindow; /** * 艾欧尼亚 */ private TextView mTvLeft; /** * 诺克萨斯 */ private TextView mTvRight; private List<String> list1; private List<String> list2; private MyAdapter myAdapter1; private MyAdapter myAdapter2; private View popView; private ListView pop_lv; private LinearLayout mLineTitle; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); list1 = new ArrayList<>(); list2 = new ArrayList<>(); list1.add("伊泽瑞尔"); list1.add("德邦总管"); list1.add("无极剑圣"); list1.add("战争之王"); list1.add("深海泰坦"); list1.add("蛮族之王"); list1.add("盲僧^^瞎"); list2.add("机械公敌"); list2.add("暗夜猎手"); list2.add("虚空恐惧"); list2.add("扭曲树精"); list2.add("永恒梦魇"); list2.add("祖安狂人"); list2.add("熔岩巨兽"); initPop(); } private void initPop() { //把包裹成ListView的XML转换成View popView = LayoutInflater.from(this).inflate(R.layout.poplist, null); LinearLayout viewById = popView.findViewById(R.id.pop_line_bottom); pop_lv = popView.findViewById(R.id.pop_lv); popupWindow = new PopupWindow(popView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); //设置可触摸 设置焦点 popupWindow.setOutsideTouchable(true); popupWindow.setFocusable(true); //设置背景 popupWindow.setBackgroundDrawable(new ColorDrawable()); //设置结束时的监听 popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { //设置TextView的颜色,把所有LinearLayout的文本颜色该为灰色 mTvLeft.setTextColor(Color.GREEN); mTvRight.setTextColor(Color.GREEN); } }); //点其他关闭 viewById.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { popupWindow.dismiss(); } }); //做俩个适配器 myAdapter1 = new MyAdapter(list1, MainActivity.this); myAdapter2 = new MyAdapter(list2, MainActivity.this); pop_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { popupWindow.dismiss(); switch (flag) { case 0: String s = list1.get(i); mTvLeft.setText(s); break; case 1: String s1 = list2.get(i); mTvRight.setText(s1); break; default: break; } } }); } private int flag = 0; private void initView() { mLinearLeft = (LinearLayout) findViewById(R.id.linear_left); mLinearLeft.setOnClickListener(this); mLinearRight = (LinearLayout) findViewById(R.id.linear_right); mLinearRight.setOnClickListener(this); mListBottom = (ListView) findViewById(R.id.list_bottom); mActivityMain = (LinearLayout) findViewById(R.id.activity_main); mTvLeft = (TextView) findViewById(R.id.tvLeft); mTvRight = (TextView) findViewById(R.id.tvRight); mLineTitle = (LinearLayout) findViewById(R.id.line_title); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.linear_left: pop_lv.setAdapter(myAdapter1); popupWindow.showAsDropDown(mLineTitle,0,2); flag = 0; break; case R.id.linear_right: pop_lv.setAdapter(myAdapter2); popupWindow.showAsDropDown(mLineTitle,0,2); flag = 1; break; } }}2.MyAdapter
public class MyAdapter extends BaseAdapter { private List<String> list; private Context context; public MyAdapter(List<String> list, Context context) { this.list = list; this.context = context; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int i) { return null; } @Override public long getItemId(int i) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if(convertView==null){ holder=new ViewHolder(); convertView=convertView.inflate(context,R.layout.item,null); holder.textView= (TextView) convertView.findViewById(R.id.tv); convertView.setTag(holder); }else{ holder= (ViewHolder) convertView.getTag(); } holder.textView.setText(list.get(position)); return convertView; } static class ViewHolder{ TextView textView; }}3.MainXML
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v7.widget.Toolbar android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/holo_purple"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="英雄联盟" android:textSize="25sp" /> </android.support.v7.widget.Toolbar> <View android:layout_width="match_parent" android:layout_height="2dp" android:background="#666" /> <LinearLayout android:id="@+id/line_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <LinearLayout android:id="@+id/linear_left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center"> <TextView android:id="@+id/tvLeft" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/line" android:text="艾欧尼亚" android:textSize="30sp" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> </LinearLayout> <TextView android:text="召唤师峡谷" android:layout_width="15dp" android:layout_height="wrap_content"/> <LinearLayout android:id="@+id/linear_right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center"> <TextView android:id="@+id/tvRight" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/line" android:text="诺克萨斯" android:textSize="30sp" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> </LinearLayout> </LinearLayout> <ListView android:divider="#f0f0f0" android:dividerHeight="10dp" android:id="@+id/list_bottom" android:layout_width="match_parent" android:scrollbarStyle="outsideOverlay" android:layout_height="wrap_content"> </ListView></LinearLayout>4.item
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:textSize="25sp" android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout>5.poplist
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:background="#5000" android:layout_height="match_parent"> <ListView android:id="@+id/pop_lv" android:scrollbarStyle="outsideOverlay" android:layout_width="match_parent" android:layout_height="wrap_content"></ListView> <LinearLayout android:orientation="vertical" android:id="@+id/pop_line_bottom" android:layout_width="match_parent" android:layout_height="0dp"> </LinearLayout></LinearLayout>
阅读全文
0 0
- PopupWindow扩展
- 可扩展的popUpwindow
- 可扩展的popUpwindow
- 自定义PopupWindow--可扩展操作
- popupwindow
- popupWindow
- PopupWindow
- PopupWindow
- PopupWindow
- PopupWindow
- PopupWindow
- popupwindow
- PopupWindow
- PopupWindow
- PopupWindow
- PopupWindow
- PopupWindow
- PopupWindow
- MYSQL-explain
- linux下mongo启动关闭重启方法
- 宗教信仰
- Land of Lisp 笔记
- 搜狗笔试整理
- PopupWindow扩展
- Java 修饰符
- UVA116
- 基于Apache服务器的文件列表,即文件的http下载模式
- CMD下操作VPN
- js正则表达式
- C++杂忆集(六)
- HDU 6162 Ch’s gift(LCA 暴力 17多校第九场)
- 出现 libcaffe.so.1.0.0-rc5: cannot open shared object file