点击地区     未点击地区

MainActivity

package com.example.doublelist;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.doublelist.leftAdapter.NotifactionRigthAdapter;

public class MainActivity extends Activity implements OnClickListener {

    List<String> list1;
    List<String> list2;
    List<String> list3;
    leftAdapter leftadapter;
    rightAdapter rightAdapter;
    ListView listview1;
    ListView listview2;
    View view;
    PopupWindow window;
    int width,height;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
            WindowManager wm = this.getWindowManager();
            width = wm.getDefaultDisplay().getWidth();
            height = wm.getDefaultDisplay().getHeight();
            
        init();
    }
    public void init(){
        ((TextView)findViewById(R.id.text1)).setOnClickListener(this);
        ((TextView)findViewById(R.id.text2)).setOnClickListener(this);
        ((TextView)findViewById(R.id.text3)).setOnClickListener(this);
        
        list1=new ArrayList<String>();
        list2=new ArrayList<String>();
        list3=new ArrayList<String>();
        
        for(int i=0;i<50;i++){
            list1.add("A"+i);
            list2.add("B"+i);
            list3.add("C"+i);
        }
        
        view=LayoutInflater.from(this).inflate(R.layout.view, null);
        listview1=(ListView) view.findViewById(R.id.listview1);
        listview2=(ListView) view.findViewById(R.id.listview2);
        
        leftadapter=new leftAdapter( MainActivity.this);
        rightAdapter=new rightAdapter( MainActivity.this);
        
        
        leftadapter.setList(list1);
        rightAdapter.setList(list2);
        
        listview1.setAdapter(leftadapter);
        listview2.setAdapter(rightAdapter);
    //    listview1.setChoiceMode(ListView.CHOICE_MODE_SINGLE);// 一定要设置这个属性,否则ListView不会刷新 
        
        window=ShowPopupwindow(view);
        
        leftadapter.setnotifactionRigthAdapter(new NotifactionRigthAdapter() {
            
            @Override
            public void execute(View v) {
                // TODO Auto-generated method stub
                int postion=(Integer) v.getTag();
                List<String> list=new ArrayList<String>();
                for(int i=0;i<50;i++){
                    list.add(postion+"B"+i);
                }
                rightAdapter.setList(list);
                rightAdapter.notifyDataSetChanged();
                leftadapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        switch (arg0.getId()) {
        case R.id.text1:
            leftadapter.setList(list1);
            rightAdapter.setList(list2);
            window.showAsDropDown(((TextView)findViewById(R.id.text1)),10,0);
            break;
        case R.id.text2:
            
            break;
        case R.id.text3:
            
            break;
        default:
            break;
        }
    }
    
    public PopupWindow ShowPopupwindow(View view){
        PopupWindow window=new PopupWindow(MainActivity.this);
        window.setContentView(view);
        
        window.setWidth(width-20);
        window.setHeight(height-100);
        window.setFocusable(true);
        window.setTouchable(true);
        window.setOutsideTouchable(true);
        //window.setBackgroundDrawable(getResources().getDrawable(R.color.tmgray));
        
        return window;
        
    }
}

leftAdapter

package com.example.doublelist;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class leftAdapter extends BaseAdapter {

    Context context;
    List<String> list;
    NotifactionRigthAdapter notifactionRigthAdapter;
    int currentPostion=0;
    public void setnotifactionRigthAdapter(NotifactionRigthAdapter notifactionRigthAdapter){
        this.notifactionRigthAdapter=notifactionRigthAdapter;
    }
    public leftAdapter(Context context) {
        // TODO Auto-generated constructor stub
        this.context=context;
    }
    public void setList(List<String> list){
        this.list=list;
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return list.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public View getView(int postion, View view, ViewGroup arg2) {
        // TODO Auto-generated method stub
        view=LayoutInflater.from(context).inflate(R.layout.item, null);
        ((TextView)view.findViewById(R.id.text)).setText(list.get(postion));
        view.setOnClickListener(new itemListener());
        view.setTag(postion);
        if(currentPostion==postion){
            view.setBackgroundColor(Color.GRAY);
        }else{
            view.setBackgroundColor(Color.WHITE);
        }
        return view;
    }

    class itemListener implements OnClickListener{

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            notifactionRigthAdapter.execute(v);
            currentPostion=(Integer) v.getTag();
        }
    }
    public interface NotifactionRigthAdapter{
        public void execute(View v);
    }
}

rightAdapter

package com.example.doublelist;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class rightAdapter extends BaseAdapter {

    Context context;
    List<String> list;
    public rightAdapter(Context context) {
        // TODO Auto-generated constructor stub
        this.context=context;
    }
    public void setList(List<String> list){
        this.list=list;
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return list.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        // TODO Auto-generated method stub
        arg1=LayoutInflater.from(context).inflate(R.layout.item, null);
        ((TextView)arg1.findViewById(R.id.text)).setText(list.get(arg0));
        arg1.setBackgroundColor(Color.WHITE);
        return arg1;
    }

}

activity_main

<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:orientation="vertical" >
<LinearLayout 
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:orientation="horizontal"
    >
    <TextView 
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="地区"
        android:gravity="center"
        android:id="@+id/text1"
        />
        <TextView 
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="美食"
        android:gravity="center"
        android:id="@+id/text2"
        />
            <TextView 
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="距离"
        android:gravity="center"
        android:id="@+id/text3"
        />
</LinearLayout>

</LinearLayout>

view.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="horizontal" >
     <!-- 设置点击时背景无颜色 -->
<ListView 
    android:id="@+id/listview1"
    android:layout_width="0dp"
    android:layout_weight="2"
    android:scrollbars="none"
    android:listSelector="#ffffff" 
    android:cacheColorHint="#ffffff"
    android:layout_height="fill_parent">
</ListView>
<TextView 
    android:layout_width="1dp"
    android:layout_height="match_parent"
    android:background="@color/black"/>
<ListView 
    android:id="@+id/listview2"
    android:layout_width="0dp"
    android:layout_weight="3"
    android:scrollbars="none"
    android:listSelector="#ffffff"
    android:cacheColorHint="#ffffff"
    android:layout_height="fill_parent">
</ListView>
</LinearLayout>


item.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" >
    
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:id="@+id/text"/>
</LinearLayout>

 

 

 

posted on 2014-06-17 11:58  青年程序猿  阅读(670)  评论(0)    收藏  举报