Android中的仿微信界面(二)

昨天進行的一個仿微信界面的項目最後留下了一個問題,本來想通過另外一種方法來實現,但是今天嘗試了之後,卻發現了更多的問題。。今天就把它記下來

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();
				}
			}
		});
    }
}


activity_main.xml

<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,即一個圖片加文字,但我百度了很久還是沒有找到方法,希望之後的日子裏能解決這個問題


效果圖:

    

發佈了48 篇原創文章 · 獲贊 1 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章