收藏 分销(赏)

Android滑动页碎片_网图抓取器开发步骤.doc

上传人:pc****0 文档编号:7545230 上传时间:2025-01-08 格式:DOC 页数:19 大小:1.32MB 下载积分:10 金币
下载 相关 举报
Android滑动页碎片_网图抓取器开发步骤.doc_第1页
第1页 / 共19页
Android滑动页碎片_网图抓取器开发步骤.doc_第2页
第2页 / 共19页


点击查看更多>>
资源描述
Android总结 孙沛林 QQ:1067206190 北京唐城 ViewPager+Fragment+xUtils+网页图片抓取 项目: FragmentDemo 1.建项目 2.导入xUtils, , 3.加权限 <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> 布局 main.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=" xmlns:tools=" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fff" > <!-- 头部标题 --> <fragment android:id="@+id/id_fragment_title" android:name="spl.example.fragmentdemo2.TitleFragment" android:layout_width="fill_parent" android:layout_height="45dp" /> <!-- 底部按钮 --> <LinearLayout android:id="@+id/id_ly_bottombar" android:layout_width="fill_parent" android:layout_height="55dp" android:layout_alignParentBottom="true" android:orientation="horizontal" android:background="#111"> <RelativeLayout android:id="@+id/btn1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:background="#0f0" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="网页1" android:layout_centerInParent="true" android:textColor="#fff" android:textSize="24sp" /> </RelativeLayout> <RelativeLayout android:id="@+id/btn2" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:background="#00f" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="网页2" android:layout_centerInParent="true" android:textColor="#fff" android:textSize="24sp" /> </RelativeLayout> <RelativeLayout android:id="@+id/btn3" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:background="#00f" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="网页3" android:layout_centerInParent="true" android:textColor="#fff" android:textSize="24sp" /> </RelativeLayout> <RelativeLayout android:id="@+id/btn4" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:background="#00f" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="网页4" android:layout_centerInParent="true" android:textColor="#fff" android:textSize="24sp" /> </RelativeLayout> </LinearLayout> <!-- 切换Fragment的地方 --> <LinearLayout android:id="@+id/id_content" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_above="@id/id_ly_bottombar" android:layout_below="@id/id_fragment_title" android:orientation="horizontal" > <android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="match_parent" android:flipInterval="3000" android:persistentDrawingCache="animation" /> </LinearLayout> </RelativeLayout> fragment_title.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=" android:layout_width="match_parent" android:layout_height="45dp" android:background="#000" > <!-- 标题 --> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:text="@string/app_name" android:textColor="#fff" android:textSize="20sp" android:textStyle="bold" /> </RelativeLayout> main2.xml 主碎片布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/listview" android:layout_width="fill_parent" android:layout_height="wrap_content" ></ListView> </LinearLayout> item.xml 行布局 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/pic" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" android:layout_centerHorizontal="true" /> </RelativeLayout> 主程 MainActivity.java package spl.example.fragmentdemo2; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import spl.example.fragmentdemo2.fragment.BaseFragment; import spl.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.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 extends FragmentActivity implements OnClickListener, InfoGet{ private RelativeLayout mTabWeiXin;// 切换按钮 private RelativeLayout mTabFriend; private RelativeLayout mTab3;// 切换按钮 private RelativeLayout mTab4; private RelativeLayout[] arrBtn = new RelativeLayout[4]; // 按钮数组 private BaseFragment mWeiXin = null; private BaseFragment mFriend = null; private BaseFragment m3 = null; private BaseFragment m4 = null; private List<BaseFragment> fragments;// 碎片集合 private ViewPager viewPager;// 滑动页 private MyFragmentAdapater adapter;//碎片适配器 @Override protected void 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); 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 = 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对象 viewPager = (ViewPager) findViewById(R.id.vp); // 准备数据 fragments = new ArrayList<BaseFragment>(); fragments.add(mWeiXin); fragments.add(mFriend); fragments.add(m3); fragments.add(m4); setColor(0); adapter = new MyFragmentAdapater(getSupportFragmentManager()); adapter.setFragments(fragments);// 添加数据 viewPager.setAdapter(adapter); viewPager.setCurrentItem(0);// 将当前页设定为第1页 viewPager.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int arg0) { // 选中当前页 setColor(arg0); Log.i("spl", "========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 onPageScrollStateChanged(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);// 标记背景色 mTabFriend.setBackgroundColor(Color.GREEN);// 标记背景色 mTab3.setBackgroundColor(Color.RED);// 标记背景色 mTab4.setBackgroundColor(Color.MAGENTA);// 标记背景色 } @Override public void onClick(View v) { // 点击事件 // 恢复默认颜色 //initTabColor(); // 根据点击底部的相对容器,来判断该切换的碎片 switch (v.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: viewPager.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; } @Override public List<Map<String, Object>> getDataList(int id) { // TODO Auto-generated method stub List<Map<String, Object>> list = new ArrayList<Map<String,Object>>(); Map<String,Object> map = new HashMap<String, Object>(); map.put("name", "孙老师"); list.add(map); map = new HashMap<String, Object>(); map.put("name", "霍老师"); list.add(map); return list; } } MyFragmentAdapater.java package spl.example.fragmentdemo2; import java.util.List; import spl.example.fragmentdemo2.fragment.BaseFragment; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; public class MyFragmentAdapater extends FragmentPagerAdapter { private List<BaseFragment> fragments; public void setFragments(List<BaseFragment> fragments) { this.fragments = fragments; } 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.size(); } // @Override // public void setPrimaryItem(ViewGroup container, int position, Object object) { // // 适配器决定当前的碎片对象为主碎片 // super.setPrimaryItem(container, position, object); // // 自定义的方法 // fragments.get(position).show(); // } // // @Override // public int getItemPosition(Object object) { // // 当前碎片的下标, 强制刷新当前的碎片 // return PagerAdapter.POSITION_NONE; // } // // @Override // public void destroyItem(ViewGroup container, int position, Object object) { // // 注释下面的语句,不再销毁碎片 // //super.destroyItem(container, position, object); // } } TitleFragment.java 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 public 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.fragmentdemo2.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; } /** * 加载数据 */ 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.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; import 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 class 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.inflate(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[index]; List<String> urlList = new ArrayList<String>(); adapter = new ImageAdapter(getActivity(), loader); adapter.setList(urlList); imageList.setAdapter(adapter); loadImgList(url); return view; } private void loadImgList(String url) { Log.i("spl", "loadImgList url="+url); new HttpUtils().send(HttpRequest.HttpMethod.GET, url, new RequestCallBack<String>() { @Override public void onSuccess(ResponseInfo<String> responseInfo) { //Log.i("spl", "result="+responseInfo.result); adapter.addSrc(BitmapHelp.getImgSrcList(responseInfo.result));
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 百科休闲 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服