模仿三星通讯录 ListView 左右滑动拨号或发短信
ListView 里面item 为ViewPaper
直接上代码:
View Code
1 package com.pzh; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import com.jon.R; 7 8 import android.R.integer; 9 import android.app.Activity; 10 import android.content.Context; 11 import android.os.Bundle; 12 import android.os.Parcelable; 13 import android.support.v4.view.PagerAdapter; 14 import android.support.v4.view.ViewPager; 15 import android.util.Log; 16 import android.view.LayoutInflater; 17 import android.view.View; 18 import android.view.ViewGroup; 19 import android.widget.ArrayAdapter; 20 import android.widget.ListView; 21 22 public class ViewPagerListView extends Activity 23 { 24 private ContactListView listView; 25 26 @Override 27 public void onCreate(Bundle savedInstanceState) 28 { 29 super.onCreate(savedInstanceState); 30 setContentView(R.layout.main); 31 listView = (ContactListView) this.findViewById(R.id.listview); 32 String str = "a"; 33 List<String> strings = new ArrayList<String>(); 34 strings.add(str); 35 strings.add(str); 36 strings.add(str); 37 strings.add(str); 38 strings.add(str); 39 listView.setAdapter(new ListViewAdapter(this, 0, strings)); 40 } 41 42 class ListViewAdapter extends ArrayAdapter<String> 43 { 44 private Context mContext; 45 private View view1; 46 private View view2; 47 private View view3; 48 private List<View> views; 49 50 public ListViewAdapter(Context context, int textViewResourceId, 51 List<String> objects) 52 { 53 super(context, textViewResourceId, objects); 54 views = new ArrayList<View>(); 55 mContext = context; 56 view1 = LayoutInflater.from(mContext).inflate(R.layout.view1, null); 57 view2 = LayoutInflater.from(mContext).inflate(R.layout.view2, null); 58 view3 = LayoutInflater.from(mContext).inflate(R.layout.view3, null); 59 views.add(view1); 60 views.add(view2); 61 views.add(view3); 62 } 63 64 @Override 65 public View getView(int position, View convertView, ViewGroup parent) 66 { 67 if (convertView == null) 68 { 69 LayoutInflater inflater = LayoutInflater.from(mContext); 70 convertView = inflater.inflate(R.layout.vp_item, null); 71 } 72 ViewPager vp = (ViewPager) convertView 73 .findViewById(R.id.tabcontent_vp); 74 vp.setAdapter(new TabAdapter()); 75 vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() 76 { 77 78 @Override 79 public void onPageSelected(int position) 80 { 81 // // TODO Auto-generated method stub 82 // int count = 0; 83 // if (position == 1) 84 // { 85 // listView.setCanMoving(false); 86 // count++; 87 // } 88 // if (position == 0) 89 // { 90 // count--; 91 // if (count == 0) 92 // { 93 // listView.setCanMoving(true); 94 // } 95 // } 96 } 97 98 @Override 99 public void onPageScrolled(int arg0, float arg1, int arg2) 100 { 101 // TODO Auto-generated method stub 102 // 拦截父控件的onTounch事件 103 if (arg0 > 0.1 && arg1 < 0.9) 104 { 105 listView.requestDisallowInterceptTouchEvent(true); 106 } 107 } 108 109 @Override 110 public void onPageScrollStateChanged(int arg0) 111 { 112 // TODO Auto-generated method stub 113 // 解除父控件的onTounch事件 114 if (arg0 == ViewPager.SCROLL_STATE_DRAGGING) 115 { 116 listView.requestDisallowInterceptTouchEvent(false); 117 } 118 119 } 120 }); 121 vp.setCurrentItem(1); 122 return convertView; 123 } 124 125 private class TabAdapter extends PagerAdapter 126 { 127 128 @Override 129 public int getCount() 130 { 131 return views.size(); 132 } 133 134 @Override 135 public Object instantiateItem(View arg0, int arg1) 136 { 137 138 ((ViewPager) arg0).addView(views.get(arg1)); 139 return views.get(arg1); 140 } 141 142 @Override 143 public void destroyItem(View arg0, int arg1, Object arg2) 144 { 145 ((ViewPager) arg0).removeView(views.get(arg1)); 146 } 147 148 @Override 149 public boolean isViewFromObject(View arg0, Object arg1) 150 { 151 return arg0 == arg1; 152 } 153 154 @Override 155 public void restoreState(Parcelable arg0, ClassLoader arg1) 156 { 157 158 } 159 160 @Override 161 public Parcelable saveState() 162 { 163 return null; 164 } 165 166 @Override 167 public void startUpdate(View arg0) 168 { 169 170 } 171 172 @Override 173 public void finishUpdate(View arg0) 174 { 175 176 } 177 } 178 179 } 180 181 }
代码地址:https://files.cnblogs.com/echo6106/ViewPagerListViewItem.zip