1、Android总结 孙沛林 QQ:1067206190 北京唐城
ViewPager+Fragment+xUtils+网页图片抓取
项目: FragmentDemo
1.建项目
2.导入xUtils, ,
3.加权限
2、ermission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
布局
main.xml
3、>
5、 android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#0f0"
>
6、 android:text="网页1"
android:layout_centerInParent="true"
android:textColor="#fff"
android:textSize="24sp"
/>
7、 android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#00f"
>
8、id:layout_centerInParent="true"
android:textColor="#fff"
android:textSize="24sp"
/>
10、 android:textColor="#fff"
android:textSize="24sp"
/>
12、 android:textSize="24sp"
/>
13、ayout_above="@id/id_ly_bottombar"
android:layout_below="@id/id_fragment_title"
android:orientation="horizontal" >
14、ght="match_parent"
android:flipInterval="3000"
android:persistentDrawingCache="animation" />
15、atch_parent"
android:layout_height="45dp"
android:background="#000" >
16、
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold" />
18、oid="
android:layout_width="match_parent"
android:layout_height="match_parent" >
19、ayout_centerHorizontal="true"
/>
20、example.fragmentdemo2.fragment.Fragment1;
import spl.example.fragmentdemo2.fragment.Fragment3;
import spl.example.fragmentdemo2.fragment.Fragment4;
import spl.example.fragmentdemo2.fragment.Fragment2;
import android.os.Bundle;
import android.graphics.Color;
// 同时使用V4包
import android.support 21、v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
public class MainActivity e 22、xtends FragmentActivity
implements OnClickListener, InfoGet{
private RelativeLayout mTabWeiXin;// 切换按钮
private RelativeLayout mTabFriend;
private RelativeLayout mTab3;// 切换按钮
private RelativeLayout mTab4;
private RelativeLayout[] arrBtn = new RelativeLayout[4]; // 按钮数组
private 23、 BaseFragment mWeiXin = null;
private BaseFragment mFriend = null;
private BaseFragment m3 = null;
private BaseFragment m4 = null;
private List 24、id onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 拿到两个底部的相对容器
mTabWeiXin = (RelativeLayout) findViewById(R.id.btn1);
mTabFriend = (RelativeLayout) findViewById(R.id.btn2);
mTab3 = (RelativeLayout) findViewById(R.id.btn3); 25、
mTab4 = (RelativeLayout) findViewById(R.id.btn4);
arrBtn[0] = mTabWeiXin;
arrBtn[1] = mTabFriend;
arrBtn[2] = mTab3;
arrBtn[3] = mTab4;
for (int i = 0; i < arrBtn.length; i++) {
arrBtn[i].setOnClickListener(this);
}
mWeiXin = new Fragment1();
mFriend = 26、 new Fragment2();
m3 = new Fragment3();
m4 = new Fragment4();
mWeiXin.setIndex(1);
mFriend.setIndex(2);
m3.setIndex(3);
m4.setIndex(4);
mWeiXin.setInfoGet(this);
mFriend.setInfoGet(this);
m3.setInfoGet(this);
m4.setInfoGet(this);
// 获取ViewPager对象
viewP 27、ager = (ViewPager) findViewById(R.id.vp);
// 准备数据
fragments = new ArrayList 28、r.setFragments(fragments);// 添加数据
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(0);// 将当前页设定为第1页
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
// 选中当前页
setColor(arg0);
Log.i("spl", "= 29、onPageSelected="+(arg0+1));
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// 在滑动时
//Log.i("spl", "arg0="+arg0);
//Log.i("spl", "arg1="+arg1);
//Log.i("spl", "arg2="+arg2);
}
@Override
public void onPageScrollStat 30、eChanged(int arg0) {
// 当滑动状态改变时
//Log.i("spl", "StateChanged="+arg0);
}
});
}
public void setColor(int index){
initTabColor();
arrBtn[index].setBackgroundColor(Color.GRAY);
}
public void initTabColor(){
mTabWeiXin.setBackgroundColor(Color.BLUE);// 标记 31、背景色
mTabFriend.setBackgroundColor(Color.GREEN);// 标记背景色
mTab3.setBackgroundColor(Color.RED);// 标记背景色
mTab4.setBackgroundColor(Color.MAGENTA);// 标记背景色
}
@Override
public void onClick(View v) {
// 点击事件
// 恢复默认颜色
//initTabColor();
// 根据点击底部的相对容器,来判断该切换的碎片
switch (v. 32、getId()) {
case R.id.btn1:
viewPager.setCurrentItem(0);// 第一页
//mTabWeiXin.setBackgroundColor(Color.RED);// 标记背景色
break;
case R.id.btn2:
viewPager.setCurrentItem(1);// 第二页
break;
case R.id.btn3:
viewPager.setCurrentItem(2);// 第二页
break;
case R.id.btn4:
vie 33、wPager.setCurrentItem(3);// 第二页
break;
default:
break;
}
}
@Override
public String getDataString(int id) {
String res = "";
switch (id) {
case 1:
res = "孙老师说";
break;
case 2:
res = "霍老师说";
break;
default:
break;
}
return res;
34、 }
@Override
public List 35、w HashMap 36、android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class MyFragmentAdapater extends FragmentPagerAdapter {
private List 37、
public MyFragmentAdapater(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// TODO Auto-generated method stub
return fragments.get(position);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return fragments 38、size();
}
// @Override
// public void setPrimaryItem(ViewGroup container, int position, Object object) {
// // 适配器决定当前的碎片对象为主碎片
// super.setPrimaryItem(container, position, object);
// // 自定义的方法
// fragments.get(position).show();
// }
//
// @Override
// public int getItemPositio 39、n(Object object) {
// // 当前碎片的下标, 强制刷新当前的碎片
// return PagerAdapter.POSITION_NONE;
// }
//
// @Override
// public void destroyItem(ViewGroup container, int position, Object object) {
// // 注释下面的语句,不再销毁碎片
// //super.destroyItem(container, position, object);
// }
}
TitleFragment.java
40、
package spl.example.fragmentdemo2;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class TitleFragment extends Fragment{
//private ImageButton mLeftMenu; // 图片按钮
@Override
p 41、ublic View onCreateView(
LayoutInflater inflater, // 反射器
ViewGroup container, // 容器
Bundle savedInstanceState//保存状态
) {
View view = inflater.inflate(R.layout.fragment_title, container, false);
return view;
}
}
碎片
BaseFragment.java
package spl.example.fragment 42、demo2.fragment;
import spl.example.fragmentdemo2.InfoGet;
import android.support.v4.app.Fragment;
public abstract class BaseFragment extends Fragment{
protected InfoGet infoGet;// 声明一个接口成员
public void setInfoGet(InfoGet infoGet){
this.infoGet = infoGet;
}
/**
* 加载数据
*/ 43、
public abstract void show();
protected int index = 0;
public void setIndex(int index){
this.index = index;
}
}
Fragment1.java
package spl.example.fragmentdemo2.fragment;
import java.util.ArrayList;
import java.util.List;
import spl.example.fragmentdemo2.R;
import spl 44、example.fragmentdemo2.xutils.BitmapHelp;
import spl.example.fragmentdemo2.xutils.ImageAdapter;
import spl.example.fragmentdemo2.xutils.xUtilsImageLoader;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
impo 45、rt com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
public c 46、lass Fragment1 extends BaseFragment{
ImageAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.i("spl", "onCreateView "+index);//+infoGet.getDataList(1).toString());
View view = inflater.in 47、flate(R.layout.main2, container, false);
//BitmapUtils bitmapUtils = BitmapHelp.getBitmapUtils(getActivity());
xUtilsImageLoader loader = new xUtilsImageLoader(getActivity());
ListView imageList = (ListView) view.findViewById(R.id.listview);
String url = BitmapHelp.imgSites[i 48、ndex];
List 49、"+url);
new HttpUtils().send(HttpRequest.HttpMethod.GET, url,
new RequestCallBack






