通过Viewpager 来实现微信界面左右滑动。

 1 package com.lixu.huadong;
 2 
 3 import java.util.ArrayList;
 4 
 5 import android.os.Bundle;
 6 import android.support.v4.app.Fragment;
 7 import android.support.v4.app.FragmentActivity;
 8 import android.support.v4.app.FragmentManager;
 9 import android.support.v4.app.FragmentPagerAdapter;
10 import android.support.v4.view.ViewPager;
11 import android.view.Window;
12 
13 public class MainActivity extends FragmentActivity {
14     private MyAdapter mMyAdapter = null;
15     private ArrayList<Fragment> list = null;
16 
17     @Override
18     protected void onCreate(Bundle savedInstanceState) {
19         super.onCreate(savedInstanceState);
20         // 去掉程序标题头
21         requestWindowFeature(Window.FEATURE_NO_TITLE);
22 
23         setContentView(R.layout.activity_main);
24 
25         list = new ArrayList<Fragment>();
26         list.add(new MyFragment1());
27         list.add(new MyFragment2());
28         list.add(new MyFragment3());
29         list.add(new MyFragment4());
30 
31         ViewPager vp = (ViewPager) findViewById(R.id.vp);
32         mMyAdapter = new MyAdapter(this.getSupportFragmentManager());
33         vp.setAdapter(mMyAdapter);
34 
35     }
36 
37     private class MyAdapter extends FragmentPagerAdapter {
38 
39         public MyAdapter(FragmentManager fm) {
40             super(fm);
41         }
42 
43         @Override
44         public Fragment getItem(int arg0) {
45             return list.get(arg0);
46         }
47 
48         @Override
49         public int getCount() {
50             return list.size();
51         }
52 
53     }
54 
55 }
 1 package com.lixu.huadong;
 2 
 3 import android.os.Bundle;
 4 import android.support.v4.app.Fragment;
 5 import android.view.LayoutInflater;
 6 import android.view.View;
 7 import android.view.ViewGroup;
 8 import android.widget.ImageView;
 9 
10 class MyFragment1 extends Fragment {
11 
12     @Override
13     public void onCreate(Bundle savedInstanceState) {
14         super.onCreate(savedInstanceState);
15     }
16 
17     // 创建view 也可以直接在这里面获取view中的控件。
18     @Override
19     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
20 
21         View view = inflater.inflate(R.layout.list, null);
22 
23         return view;
24     }
25 
26     // 创建view完毕 可以直接在onCreateView中获取view中控件。
27     @Override
28     public void onViewCreated(View view, Bundle savedInstanceState) {
29         super.onViewCreated(view, savedInstanceState);
30 
31         ImageView iv = (ImageView) view.findViewById(R.id.iv);
32         iv.setImageResource(R.drawable.weixinjiemian);
33     }
34 }
  1 package com.lixu.huadong;
  2 
  3 import java.util.ArrayList;
  4 import java.util.HashMap;
  5 
  6 import android.content.Context;
  7 import android.graphics.Color;
  8 import android.os.Bundle;
  9 import android.support.v4.app.Fragment;
 10 import android.view.LayoutInflater;
 11 import android.view.View;
 12 import android.view.ViewGroup;
 13 import android.widget.ArrayAdapter;
 14 import android.widget.ListView;
 15 import android.widget.TextView;
 16 
 17 class MyFragment2 extends Fragment {
 18     private static final String TYPE = "type";
 19     private static final String DATE = "date";
 20     private static final int GROUP = -1;
 21     private static final int ITEM = -2;
 22 
 23     private ArrayList<HashMap<String, Object>> item = null;
 24 
 25     @Override
 26     public void onCreate(Bundle savedInstanceState) {
 27         super.onCreate(savedInstanceState);
 28 
 29     }
 30 
 31     // 创建view
 32     @Override
 33     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
 34 
 35         View view = inflater.inflate(R.layout.list1, null);
 36 
 37         return view;
 38     }
 39 
 40     // 创建view完毕
 41     @Override
 42     public void onViewCreated(View view, Bundle savedInstanceState) {
 43         super.onViewCreated(view, savedInstanceState);
 44 
 45         ListView lv = (ListView) view.findViewById(R.id.lv);
 46         MyAdaper adapter = new MyAdaper(getActivity(), -1);
 47 
 48         item = new ArrayList<HashMap<String, Object>>();
 49 
 50         String[] groups = { "家人", "朋友", "同学", "同事" };
 51         String[] date = { "张三", "李四", "王五", "狗儿" };
 52         for (String str : groups) {
 53             HashMap<String, Object> group_map = new HashMap<String, Object>();
 54 
 55             group_map.put(TYPE, GROUP);
 56             group_map.put(DATE, str);
 57             item.add(group_map);
 58             for (String n : date) {
 59 
 60                 HashMap<String, Object> date_map = new HashMap<String, Object>();
 61 
 62                 date_map.put(TYPE, ITEM);
 63                 date_map.put(DATE, n);
 64                 item.add(date_map);
 65 
 66             }
 67 
 68         }
 69         lv.setAdapter(adapter);
 70     }
 71 
 72     private class MyAdaper extends ArrayAdapter {
 73         private LayoutInflater flater = null;
 74         private Context context;
 75 
 76         public MyAdaper(Context context, int resource) {
 77             super(context, resource);
 78             this.context = context;
 79             flater = LayoutInflater.from(context);
 80         }
 81 
 82         @Override
 83         public int getCount() {
 84             return item.size();
 85         }
 86 
 87         @Override
 88         public int getItemViewType(int position) {
 89             HashMap<String, Object> map = item.get(position);
 90             return (Integer) map.get(TYPE);
 91         }
 92 
 93         @Override
 94         public int getViewTypeCount() {
 95 
 96             return 2;
 97         }
 98 
 99         @Override
100         public View getView(int position, View convertView, ViewGroup parent) {
101             int type = getItemViewType(position);
102             // 根据不同的view type加载不同的布局文件。
103             switch (type) {
104             case GROUP:
105 
106                 convertView = flater.inflate(android.R.layout.simple_list_item_1, null);
107                 TextView tv1 = (TextView) convertView.findViewById(android.R.id.text1);
108                 tv1.setText(item.get(position).get(DATE) + "");
109                 tv1.setBackgroundColor(Color.RED);
110                 tv1.setTextSize(20);
111 
112                 break;
113             case ITEM:
114                 convertView = flater.inflate(android.R.layout.simple_list_item_1, null);
115                 TextView tv2 = (TextView) convertView.findViewById(android.R.id.text1);
116                 tv2.setText(item.get(position).get(DATE) + "");
117 
118                 break;
119 
120             default:
121                 break;
122             }
123 
124             return convertView;
125         }
126 
127     }
128 
129 }
 1 package com.lixu.huadong;
 2 
 3 import android.os.Bundle;
 4 import android.support.v4.app.Fragment;
 5 import android.view.LayoutInflater;
 6 import android.view.View;
 7 import android.view.ViewGroup;
 8 import android.widget.ImageView;
 9 
10 class MyFragment3 extends Fragment {
11 
12     @Override
13     public void onCreate(Bundle savedInstanceState) {
14         super.onCreate(savedInstanceState);
15     }
16 
17     // 创建view
18     @Override
19     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
20 
21         View view = inflater.inflate(R.layout.list, null);
22 
23         return view;
24     }
25 
26     // 创建view完毕
27     @Override
28     public void onViewCreated(View view, Bundle savedInstanceState) {
29         super.onViewCreated(view, savedInstanceState);
30 
31         ImageView iv = (ImageView) view.findViewById(R.id.iv);
32         iv.setImageResource(R.drawable.faxianjiemian);
33     }
34 }
 1 package com.lixu.huadong;
 2 
 3 import android.os.Bundle;
 4 import android.support.v4.app.Fragment;
 5 import android.view.LayoutInflater;
 6 import android.view.View;
 7 import android.view.ViewGroup;
 8 import android.widget.ImageView;
 9 
10 class MyFragment4 extends Fragment {
11 
12     @Override
13     public void onCreate(Bundle savedInstanceState) {
14         super.onCreate(savedInstanceState);
15     }
16 
17     // 创建view
18     @Override
19     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
20 
21         View view = inflater.inflate(R.layout.list, null);
22 
23         return view;
24     }
25 
26     // 创建view完毕
27     @Override
28     public void onViewCreated(View view, Bundle savedInstanceState) {
29         super.onViewCreated(view, savedInstanceState);
30 
31         ImageView iv = (ImageView) view.findViewById(R.id.iv);
32         iv.setImageResource(R.drawable.wojiemian);
33     }
34 }

xml文件:

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     tools:context="com.lixu.huadong.MainActivity" >
 6 
 7     <android.support.v4.view.ViewPager
 8         android:id="@+id/vp"
 9         android:layout_width="match_parent"
10         android:layout_height="match_parent"
11         />
12 
13 </RelativeLayout>
1 <?xml version="1.0" encoding="utf-8"?>
2 <ImageView xmlns:android="http://schemas.android.com/apk/res/android"
3     android:id="@+id/iv"
4     android:layout_width="match_parent"
5     android:layout_height="match_parent" >
6 
7 </ImageView>
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6 
 7     <ListView
 8         android:id="@+id/lv"
 9         android:layout_width="match_parent"
10         android:layout_height="match_parent" >
11     </ListView>
12 
13 </LinearLayout>

运行效果图:

通过Viewpager 来实现微信界面左右滑动。

通过Viewpager 来实现微信界面左右滑动。

上一篇:当angularjs遇到微信jsdk


下一篇:在主Android Activity中加载Fragment的一般简易方法 ,来模拟一个微信界面。