今天项目又来 让什么仿大众的布局 就写了这个例子 给大家参考参考 下载地址http://download.csdn.net/detail/u012303938/8423185
直接上代码
package com.example.popdemo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private PopupWindow mWindow;
private View view1,view2;
private LinearLayout ll;
private Button btn_1,btn_2,btn_3;
private Context context;
private ListView mListView,listView2,lv,mlv;
private ArrayList<Map<String, String>> list;
private Map<String, String> map;
private ArrayList<Map<String, String>> mlist;
private ArrayList<ArrayList<Map<String, String>>> lists;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
findView();
addView();
addListioner();
}
private void addListioner() {
// TODO Auto-generated method stub
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
ArrayList<Map<String, String>> list=lists.get(arg2);
MychAdapter adapter=new MychAdapter(context, list);
listView2.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
});
}
private void loadData(ListView lvs) {
// TODO Auto-generated method stub
list=new ArrayList<Map<String,String>>();
lists=new ArrayList<ArrayList<Map<String,String>>>();
for(int i=0;i<5;i++){
map=new HashMap<String, String>();
map.put("name", "张三"+i);
map.put("no", "csnd");
list.add(map);
mlist=new ArrayList<Map<String,String>>();
for(int j=0;j<4;j++){
map=new HashMap<String, String>();
map.put("cname", "李四"+(10*i+j));
mlist.add(map);
}
lists.add(mlist);
}
MyAdapter adapter=new MyAdapter(context, list);
lvs.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
private void addView() {
// TODO Auto-generated method stub
btn_1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
addPopView(view1);
loadData(mListView);
}
});
btn_3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
addPopView(view2);
loadData(mlv);
}
});
}
private void findView() {
// TODO Auto-generated method stub
context=this;
btn_3=(Button) findViewById(R.id.btn_3);
lv=(ListView) findViewById(R.id.lv);
btn_2=(Button) findViewById(R.id.btn_2);
ll=(LinearLayout) findViewById(R.id.ll);
btn_1=(Button)findViewById(R.id.btn_1);
view1=View.inflate(MainActivity.this, R.layout.pop, null);
mListView=(ListView) view1.findViewById(R.id.listView1);
listView2=(ListView)view1.findViewById(R.id.listView2);
view2=View.inflate(context, R.layout.pop1, null);
mlv=(ListView) view2.findViewById(R.id.mlv);
// loadData(lv);
loadData();
}
private void loadData() {
// TODO Auto-generated method stub
list=new ArrayList<Map<String,String>>();
for(int i=0;i<15;i++){
map=new HashMap<String, String>();
map.put("name", "王五"+i);
map.put("no", "csnd");
list.add(map);
}
MainMyAdapter adapter=new MainMyAdapter(context, list);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
private void addPopView(View view){
mWindow=new PopupWindow(view, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
//设置pop可触摸
mWindow.setOutsideTouchable(true);
//设置焦点
mWindow.setFocusable(true);
//设置背景,点击空白处自动消失
mWindow.setBackgroundDrawable(new BitmapDrawable());
// mWindow.setAnimationStyle(R.)
mWindow.setAnimationStyle(R.anim.popanim);
mWindow.showAsDropDown(btn_1);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private class MyAdapter extends BaseAdapter{
private Context context;
private ArrayList<Map<String, String>> mlist;
public MyAdapter(Context context,ArrayList<Map<String, String>> mlist) {
this.context=context;
// TODO Auto-generated constructor stub
if(list==null){
mlist=new ArrayList<Map<String,String>>();
}else{
this.mlist=mlist;
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mlist.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return mlist.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int position, View converview, ViewGroup arg2) {
// TODO Auto-generated method stub
HoderView mHoderView=new HoderView();
if(converview==null){
View view=View.inflate(context, R.layout.pop_item, null);
converview=view;
mHoderView.tv_name=(TextView) converview.findViewById(R.id.name);
mHoderView.tv_no=(TextView) converview.findViewById(R.id.no);
converview.setTag(mHoderView);
}else{
mHoderView=(HoderView) converview.getTag();
}
Log.i("values", "mlist"+mlist);
mHoderView.tv_name.setText(mlist.get(position).get("name"));
mHoderView.tv_no.setText(mlist.get(position).get("no"));
return converview;
}
private class HoderView{
TextView tv_name,tv_no;
}
}
private class MychAdapter extends BaseAdapter{
private Context context;
private ArrayList<Map<String, String>> mlist;
public MychAdapter(Context context,ArrayList<Map<String, String>> mlist) {
this.context=context;
// TODO Auto-generated constructor stub
if(list==null){
mlist=new ArrayList<Map<String,String>>();
}else{
this.mlist=mlist;
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mlist.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return mlist.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(final int position, View converview, ViewGroup arg2) {
// TODO Auto-generated method stub
HoderView mHoderView=new HoderView();
if(converview==null){
View view=View.inflate(context, R.layout.pop_item, null);
converview=view;
mHoderView.tv_name=(TextView) converview.findViewById(R.id.name);
mHoderView.tv_no=(TextView) converview.findViewById(R.id.no);
mHoderView.ll=(LinearLayout) converview.findViewById(R.id.ll);
converview.setTag(mHoderView);
}else{
mHoderView=(HoderView) converview.getTag();
}
Log.i("values", "mlist"+mlist);
mHoderView.tv_name.setText(mlist.get(position).get("cname"));
// mHoderView.tv_no.setText(mlist.get(position).get("no"));
mHoderView.ll.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(context, mlist.get(position).get("cname"), Toast.LENGTH_SHORT).show();
}
});
return converview;
}
private class HoderView{
TextView tv_name,tv_no;
LinearLayout ll;
}
}
private class MainMyAdapter extends BaseAdapter{
private Context context;
private ArrayList<Map<String, String>> mlist;
public MainMyAdapter(Context context,ArrayList<Map<String, String>> mlist) {
this.context=context;
// TODO Auto-generated constructor stub
if(list==null){
mlist=new ArrayList<Map<String,String>>();
}else{
this.mlist=mlist;
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mlist.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return mlist.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int position, View converview, ViewGroup arg2) {
// TODO Auto-generated method stub
HoderView mHoderView=new HoderView();
if(converview==null){
View view=View.inflate(context, R.layout.main_item, null);
converview=view;
mHoderView.tv_name=(TextView) converview.findViewById(R.id.name);
mHoderView.tv_no=(TextView) converview.findViewById(R.id.no);
converview.setTag(mHoderView);
}else{
mHoderView=(HoderView) converview.getTag();
}
Log.i("values", "mlist"+mlist);
mHoderView.tv_name.setText(mlist.get(position).get("name"));
mHoderView.tv_no.setText(mlist.get(position).get("no"));
return converview;
}
private class HoderView{
TextView tv_name,tv_no;
}
}
}
布局activity_main
<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"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/btn_select"
android:text="附近"
android:textSize="16sp" />
<Button
android:id="@+id/btn_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/btn_select"
android:text="美食"
android:textSize="16sp" />
<Button
android:id="@+id/btn_3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/btn_select"
android:text="智能推荐"
android:textSize="16sp" />
</LinearLayout>
<ListView
android:layout_below="@+id/ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lv"></ListView>
</RelativeLayout>
mian_item
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/img"
android:background="@drawable/ll"
android:orientation="vertical" >
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="asd" />
<TextView
android:id="@+id/no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fdsa" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#ee999999"
android:visibility="gone" />
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/ll"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
pop_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="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/ll"
android:background="@drawable/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="15dp"
android:paddingTop="15dp"
android:text="asd" />
</LinearLayout>
<TextView
android:id="@+id/no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="15dp"
android:paddingTop="15dp"
android:text="fdsa"
android:visibility="gone" />
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#ee999999"
android:visibility="gone" />
</LinearLayout>
pop
<?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:background="#11000000"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="horizontal" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#eeeeeeee" >
</ListView>
<ListView
android:id="@+id/listView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>
</LinearLayout>
<TextView android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#33000000"/>
</LinearLayout>
pop1
<?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" >
<ListView
android:id="@+id/mlv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#eeeeeeee"></ListView>
<TextView android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#33000000"/>
</LinearLayout>
动画anim下的popanim
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale android:fromXScale="0.6"
android:toXScale="1.0"
android:fromYScale="0.6"
android:toYScale="1.0"
android:duration="5000"/>
<alpha android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="5000"/>
</set>
values 下的colors
<resources>
<color name="Red" >#ff0000</color>
<color name="Green">#00ff00</color>
<color name="white">#ffffff</color>
<color name="gray">#999999</color>
</resources>