昨天进行的一个仿微信界面的项目最后留下了一个问题,本来想通过另外一种方法来实现,但是今天尝试了之后,却发现了更多的问题。。今天就把它记下来
MainActivity.java
package com.whisker.custompopupwindow2;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Toast;
public class MainActivity extends Activity {
private PopupWindow popupWindow;
private Button addImageView;
private Button moreImageView;
private View view;
private ListView listView;
private List<String> groups;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addImageView = (Button) findViewById(R.id.btnAdd);
moreImageView = (Button) findViewById(R.id.btnSet);
addImageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
showWindow(view);
Toast.makeText(getApplicationContext(), "Click Add", Toast.LENGTH_SHORT).show();
}
});
moreImageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
showWindow2(view);
Toast.makeText(getApplicationContext(), "Click More", Toast.LENGTH_SHORT).show();
}
});
}
private void showWindow(View parent){
if(popupWindow == null){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.group_list, null);
listView = (ListView) view.findViewById(R.id.lvGroup);
groups = new ArrayList<String>();
groups.add("添加任务");
groups.add("团队成员");
GroupAdapter groupAdapter = new GroupAdapter(this, groups);
listView.setAdapter(groupAdapter);
popupWindow = new PopupWindow(view,50,250);
}
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.setBackgroundDrawable(new ColorDrawable(0000000000));
popupWindow.update();
popupWindow.setWidth(MainActivity.this.getWindowManager().getDefaultDisplay().getWidth()/2-100);
popupWindow.setAnimationStyle(R.style.AnimationPreview);
if(!popupWindow.isShowing()){
popupWindow.showAsDropDown(parent, -40, 0);
}
else{
popupWindow.dismiss();
}
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
Toast.makeText(MainActivity.this, "你点的是" + groups.get(position), Toast.LENGTH_LONG).show();
if(popupWindow != null){
popupWindow.dismiss();
}
}
});
}
private void showWindow2(View parent){
if(popupWindow == null){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.group_list, null);
listView = (ListView) view.findViewById(R.id.lvGroup);
groups = new ArrayList<String>();
groups.add("我的相册");
groups.add("我的收藏");
groups.add("我的银行卡");
groups.add("设置");
groups.add("退出登录");
GroupAdapter groupAdapter = new GroupAdapter(this, groups);
listView.setAdapter(groupAdapter);
popupWindow = new PopupWindow(view,50,250);
}
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.setBackgroundDrawable(new ColorDrawable(0000000000));
popupWindow.update();
popupWindow.setWidth(MainActivity.this.getWindowManager().getDefaultDisplay().getWidth()/2 -100);
popupWindow.setAnimationStyle(R.style.AnimationPreview);
if(!popupWindow.isShowing()){
Log.i("123", parent.getLayoutParams().width/2 + "");
popupWindow.showAsDropDown(parent, 40, 0);
}
else{
popupWindow.dismiss();
}
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
Toast.makeText(MainActivity.this, "你点的是" + groups.get(position), Toast.LENGTH_LONG).show();
if(popupWindow != null){
popupWindow.dismiss();
}
}
});
}
}
<LinearLayout 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"
android:clickable="true"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dip"
android:background="@drawable/abc_ab_bottom_solid_dark_holo">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/ic_launcher"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:text="微信"
android:textColor="@color/lightgray"
android:textSize="18sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="@+id/btnSearch"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginRight="20dip"
android:visibility="gone"
android:background="@drawable/actionbar_search_icon"/>
<Button
android:id="@+id/btnAdd"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginRight="20dip"
android:background="@drawable/actionbar_add_icon"/>
<Button
android:id="@+id/btnSet"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/actionbar_more_icon"/>
</LinearLayout>
</RelativeLayout>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
</LinearLayout>
group_item_list.xml
<?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="40dip"
android:orientation="vertical" >
<TextView
android:id="@+id/groupItem"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
style="@style/grouplist_item_textview"/>
</LinearLayout>
group_list.xml
<?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/lvGroup"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="true"
android:divider="@drawable/group_divider"
android:cacheColorHint="#000000"
android:dividerHeight="2.0px"/>
</LinearLayout>
接下来就是问题了,问题主要有两个,首先界面上的两个按钮只能按一次,第二次按的时候就会出错,退出。其次,由于接触Android时间不长,我希望的是在List中能添加的是一个LinearLayout,即一个图片加文字,但我百度了很久还是没有找到方法,希望之后的日子里能解决这个问题
效果图: