大家晚上好,CJJ不好,前天打球,把右手弄脱臼了。。。搞得我现在只能一只手敲代码。。。那效率,我给自己跪了 。。。写了好久,才写了那么一丁点。。。明明还有好多要说的。。。也只能等手好了再继续吧。。。呵呵,希望有看教程的网友谅解。。。

看过教程
android真实项目教程(二)——漫画App初构_by_CJJ

http://www.apkbus.com/forum.php?mod=viewthread&tid=166262

的朋友一定知道里面有四个子Fragment,今天我们就在其中的HomeFragment(首页)中写写东西。。。我只是依据个人的爱好去设计界面。。。如果您看的很不合理。。。留言和我说说。。。。一定洗耳恭听。。。。。。

android真实项目教程(一)——App应用框架搭建_by_CJJ http://www.apkbus.com/forum.php?mod=viewthread&tid=166151

android真实项目教程(二)——漫画App初构_by_CJJ http://www.apkbus.com/forum.php?mod=viewthread&tid=166262

android真实项目教程(三)——首页初点缀_by_CJJ http://www.apkbus.com/forum.php?mod=viewthread&tid=166630

android真实项目教程(四)——MY APP MY STYLE_by_CJJ
http://www.apkbus.com/forum.php?mod=viewthread&tid=167676

android真实项目教程(五)——有时三点两点雨_by_CJJ

http://www.apkbus.com/forum.php?mod=viewthread&tid=168422


效果图如下,好像就一点东西。。。呵呵。。。建议还是认真看源码吧。。。。。。不敢说你一定有收获。。。。。。只是不会让你有损失。。。。。。。

android真实项目教程(三)——首页初点缀_by_CJJ-编程知识网

android真实项目教程(三)——首页初点缀_by_CJJ-编程知识网

android真实项目教程(三)——首页初点缀_by_CJJ-编程知识网

android真实项目教程(三)——首页初点缀_by_CJJ-编程知识网

android真实项目教程(三)——首页初点缀_by_CJJ-编程知识网

主要源码有:

  1. package com.cjj.shopapp.fragment;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import android.os.Bundle;
  5. import android.support.v4.app.Fragment;
  6. import android.support.v4.view.ViewPager;
  7. import android.util.Log;
  8. import android.view.LayoutInflater;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import android.widget.ImageView;
  12. import android.widget.LinearLayout;
  13. import android.widget.TextView;
  14. import com.cjj.shopapp.activity.R;
  15. import com.cjj.shopapp.adapter.ViewPagerAdvAdapter;
  16. public class HomeFragment extends Fragment {
  17. private ViewPager vp_ad;
  18. private ImageView[] mImageViews;
  19. private int currentPosition = 0;
  20. private TextView tv_title;
  21. @Override
  22. public void onActivityCreated(Bundle savedInstanceState) {
  23. // 获得服务端广告图片,这里我们就简单的直接取本地数据
  24. getAdData();
  25. super.onActivityCreated(savedInstanceState);
  26. }
  27. private void getAdData() {
  28. List<Integer> list = new ArrayList<Integer>();
  29. list.add(R.drawable.huoying);
  30. list.add(R.drawable.caomao);
  31. list.add(R.drawable.yinhun);
  32. list.add(R.drawable.diguang);
  33. list.add(R.drawable.jianxin);
  34. vp_ad.setAdapter(new ViewPagerAdvAdapter(getActivity(), list));
  35. vp_ad.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
  36. @Override
  37. public void onPageSelected(int position) {
  38. super.onPageSelected(position);
  39. setCurPoint(position);
  40. }
  41. });
  42. }
  43. private void setCurPoint(int index){
  44. if(index<0||index>mImageViews.length||index==currentPosition){
  45. return;
  46. }
  47. mImageViews[currentPosition].setEnabled(true);
  48. mImageViews[index].setEnabled(false);
  49. //set tv title
  50. switch(index){
  51. case 0:
  52. tv_title.setText("火影忍者");
  53. break;
  54. case 1:
  55. tv_title.setText("海贼王");
  56. break;
  57. case 2:
  58. tv_title.setText("银魂");
  59. break;
  60. case 3:
  61. tv_title.setText("黑子的篮球");
  62. break;
  63. case 4:
  64. tv_title.setText("浪客剑心");
  65. break;
  66. }
  67. currentPosition = index;
  68. }
  69. @Override
  70. public void onCreate(Bundle savedInstanceState) {
  71. super.onCreate(savedInstanceState);
  72. }
  73. @Override
  74. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  75. Bundle savedInstanceState) {
  76. return inflater.inflate(R.layout.fragment_home, null);
  77. }
  78. @Override
  79. public void onDestroyView() {
  80. super.onDestroyView();
  81. }
  82. @Override
  83. public void onViewCreated(View view, Bundle savedInstanceState) {
  84. super.onViewCreated(view, savedInstanceState);
  85. vp_ad = (ViewPager) view.findViewById(R.id.vp_ad);
  86. tv_title = (TextView) view.findViewById(R.id.tv_title);
  87. createPoint(view);
  88. }
  89. private void createPoint(View view) {
  90. // six index round point
  91. LinearLayout ll = (LinearLayout) view.findViewById(R.id.llayout);
  92. LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
  93. LinearLayout.LayoutParams.WRAP_CONTENT,
  94. LinearLayout.LayoutParams.WRAP_CONTENT);
  95. lp.setMargins(0, 0, 12, 0);
  96. mImageViews = new ImageView[5];
  97. for (int i = 0; i < mImageViews.length; i++) {
  98. mImageViews[i] = new ImageView(getActivity());
  99. mImageViews[i].setImageResource(R.drawable.guide_round);
  100. mImageViews[i].setEnabled(true);
  101. mImageViews[i].setLayoutParams(lp);
  102. ll.addView(mImageViews[i]);
  103. }
  104. mImageViews[currentPosition].setEnabled(false);
  105. }
  106. }

复制代码

所对应的
fragment_hom.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content" >
  5. <LinearLayout
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. android:background="@color/white"
  9. android:orientation="vertical" >
  10. <FrameLayout
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:layout_marginLeft="10dp"
  14. android:layout_marginRight="10dp"
  15. android:layout_marginTop="4dp" >
  16. <android.support.v4.view.ViewPager
  17. android:id="@+id/vp_ad"
  18. android:layout_width="match_parent"
  19. android:layout_height="180dp" />
  20. <LinearLayout
  21. android:layout_width="match_parent"
  22. android:layout_height="30dp"
  23. android:layout_gravity="bottom"
  24. android:background="#6000"
  25. android:gravity="center_vertical" >
  26. <TextView
  27. android:id="@+id/tv_title"
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:layout_weight="1"
  31. android:layout_marginLeft="2dp"
  32. android:text="火影忍者" />
  33. <LinearLayout
  34. android:id="@+id/llayout"
  35. android:layout_width="wrap_content"
  36. android:layout_height="wrap_content"
  37. android:layout_gravity="right|bottom"
  38. android:layout_marginBottom="2dp"
  39. android:orientation="horizontal" >
  40. </LinearLayout>
  41. </LinearLayout>
  42. </FrameLayout>
  43. <GridView
  44. android:id="@+id/gv_category"
  45. android:layout_width="match_parent"
  46. android:layout_height="wrap_content"
  47. android:layout_marginLeft="10dp"
  48. android:layout_marginRight="10dp"
  49. android:background="@color/white"
  50. android:cacheColorHint="@android:color/transparent"
  51. android:fadingEdge="none"
  52. android:gravity="center"
  53. android:horizontalSpacing="7dp"
  54. android:numColumns="4"
  55. android:paddingBottom="10dip"
  56. android:paddingLeft="15dp"
  57. android:paddingTop="20dip"
  58. android:scrollbarStyle="outsideInset"
  59. android:verticalSpacing="20dip" />
  60. <TextView
  61. android:layout_width="match_parent"
  62. android:layout_height="wrap_content"
  63. android:layout_marginLeft="10dp"
  64. android:layout_marginRight="10dp"
  65. android:background="@color/home_group_item_e4"
  66. android:gravity="center_vertical"
  67. android:paddingBottom="5dp"
  68. android:paddingLeft="10dp"
  69. android:paddingTop="5dp"
  70. android:text="漫画推荐"
  71. android:textColor="@color/home_group_item_txt_67"
  72. android:textSize="14sp" />
  73. <GridView
  74. android:id="@+id/gv_category"
  75. android:layout_width="match_parent"
  76. android:layout_height="wrap_content"
  77. android:layout_marginLeft="10dp"
  78. android:layout_marginRight="10dp"
  79. android:background="@color/white"
  80. android:cacheColorHint="@android:color/transparent"
  81. android:fadingEdge="none"
  82. android:gravity="center"
  83. android:horizontalSpacing="7dp"
  84. android:numColumns="4"
  85. android:paddingBottom="10dip"
  86. android:paddingLeft="15dp"
  87. android:paddingTop="20dip"
  88. android:scrollbarStyle="outsideInset"
  89. android:verticalSpacing="20dip" />
  90. </LinearLayout>
  91. </ScrollView>

复制代码

适配器:

  1. package com.cjj.shopapp.adapter;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.os.Bundle;
  7. import android.support.v4.view.PagerAdapter;
  8. import android.support.v4.view.ViewPager;
  9. import android.view.View;
  10. import android.view.View.OnClickListener;
  11. import android.widget.ImageView;
  12. /**
  13. * 首页商家广告适配器
  14. *
  15. */
  16. public class ViewPagerAdvAdapter extends PagerAdapter implements OnClickListener{
  17. private List<Integer> mDatas;
  18. private List<ImageView> mViews;
  19. private ImageView mImageView;
  20. private Context mContext;
  21. private Bundle mBundle;
  22. private Intent mIntent;
  23. public ViewPagerAdvAdapter(Context mContext,List<Integer> mDatas)
  24. {
  25. mIntent = new Intent();
  26. mBundle = new Bundle();
  27. this.mContext = mContext;
  28. mViews = new ArrayList<ImageView>();
  29. this.mDatas = mDatas;
  30. int length = mDatas == null ? 0 : mDatas.size();
  31. for(int i=0;i<length;i++)
  32. {
  33. ImageView mImageView = new ImageView(mContext);
  34. mViews.add(mImageView);
  35. }
  36. length = 0;
  37. }
  38. @Override
  39. public int getCount() {
  40. return mDatas == null ? 0 : mDatas.size();
  41. }
  42. @Override
  43. public boolean isViewFromObject(View arg0, Object arg1) {
  44. return arg0==(arg1);
  45. }
  46. @Override
  47. public Object instantiateItem(View container, int position) {
  48. int pos = mDatas.get(position);
  49. mImageView = mViews.get(position);
  50. mImageView.setAdjustViewBounds(true);
  51. mImageView.setScaleType(ImageView.ScaleType.FIT_XY);
  52. mImageView.setTag(pos);
  53. mImageView.setOnClickListener(this);
  54. mImageView.setImageResource(pos);
  55. ((ViewPager)container).addView(mImageView,0);
  56. return mImageView;
  57. }
  58. @Override
  59. public void destroyItem(View container, int position, Object object) {
  60. mImageView = mViews.get(position);
  61. ((ViewPager)container).removeView(mImageView);
  62. }
  63. @Override
  64. public void onClick(View v) {
  65. }
  66. }

复制代码




就写了这么多,手好了我继续写。。。呵呵。。。。吐槽下自己:别再荒芜时间了,别把自己玩坏了,自己让自己少操心吧。。。。好吧,,,我承认自己是个白痴啊。。。。。