二级菜单关联

部分关键代码: 
listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView arg0, View arg1, int position,
					long arg3) {
				// TODO Auto-generated method stub
				final int location=position;
				myAdapter.setSelectedPosition(position);
				myAdapter.notifyDataSetInvalidated();
				subAdapter=new SubAdapter(getApplicationContext(), cities, position);
				subListView.setAdapter(subAdapter);
				subListView.setOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView arg0, View arg1,
							int position, long arg3) {
						// TODO Auto-generated method stub
						Toast.makeText(getApplicationContext(), cities[location][position], Toast.LENGTH_SHORT).show();
					}
				});
			}
		});
        
    }

 private void selectDefult(){
	   final int location=0;
	 		myAdapter.setSelectedPosition(0);
	 		myAdapter.notifyDataSetInvalidated();
	 		subAdapter=new SubAdapter(getApplicationContext(), cities, 0);
	 		subListView.setAdapter(subAdapter);
	 		subListView.setOnItemClickListener(new OnItemClickListener() {

	 			@Override
	 			public void onItemClick(AdapterView arg0, View arg1,
	 					int position, long arg3) {
	 				// TODO Auto-generated method stub
	 				Toast.makeText(getApplicationContext(), cities[location][position], Toast.LENGTH_SHORT).show();
	 			}
	 		});
   }
  ListView关键类:
package com.example.multilistview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ListView;

public class MyListView extends ListView implements Runnable {
	private float mLastDownY = 0f; 
    private int mDistance = 0; 
    private int mStep = 10; 
    private boolean mPositive = false; 
 

    public MyListView (Context context, AttributeSet attrs) { 
          super(context, attrs); 
    } 
 
    public MyListView (Context context, AttributeSet attrs, int defStyle) { 
          super(context, attrs, defStyle); 
    } 
 
    public MyListView (Context context) { 
          super(context); 
    } 
 
    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
         switch (event.getAction()) { 
              case MotionEvent.ACTION_DOWN: 
                   if (mLastDownY == 0f && mDistance == 0) { 
                         mLastDownY = event.getY(); 
                   return true; 
              } 
              break; 
 
    case MotionEvent.ACTION_CANCEL: 
           break;

    case MotionEvent.ACTION_UP: 
          if (mDistance != 0) { 
           mStep = 1; 
           mPositive = (mDistance >= 0); 
           this.post(this); 
           return true; 
        } 
        mLastDownY = 0f; 
        mDistance = 0; 
        break; 
 
    case MotionEvent.ACTION_MOVE: 
        if (mLastDownY != 0f) { 
              mDistance = (int) (mLastDownY - event.getY()); 
              if ((mDistance < 0 && getFirstVisiblePosition() == 0 && getChildAt(0).getTop() == 0) || (mDistance > 0 && getLastVisiblePosition() == getCount() - 1)) { 
                   mDistance /= 2; 
                   scrollTo(0, mDistance); 
                   return true; 
               } 
        } 
        mDistance = 0; 
        break; 
        } 
        return super.onTouchEvent(event); 
    } 
 
    public void run() { 
          mDistance += mDistance > 0 ? -mStep : mStep; 
          scrollTo(0, mDistance); 
          if ((mPositive && mDistance <= 0) || (!mPositive && mDistance >= 0)) { 
                scrollTo(0, 0); 
                mDistance = 0; 
                mLastDownY = 0f; 
                return; 
           } 
          mStep += 1; 
          this.postDelayed(this, 10); 
     } 
} 




你可能感兴趣的:(二级菜单关联)