大家晚上好,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
效果图如下,好像就一点东西。。。呵呵。。。建议还是认真看源码吧。。。。。。不敢说你一定有收获。。。。。。只是不会让你有损失。。。。。。。
主要源码有:
- package com.cjj.shopapp.fragment;
- import java.util.ArrayList;
- import java.util.List;
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.support.v4.view.ViewPager;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- import com.cjj.shopapp.activity.R;
- import com.cjj.shopapp.adapter.ViewPagerAdvAdapter;
- public class HomeFragment extends Fragment {
- private ViewPager vp_ad;
- private ImageView[] mImageViews;
- private int currentPosition = 0;
- private TextView tv_title;
- @Override
- public void onActivityCreated(Bundle savedInstanceState) {
- // 获得服务端广告图片,这里我们就简单的直接取本地数据
- getAdData();
- super.onActivityCreated(savedInstanceState);
- }
- private void getAdData() {
- List<Integer> list = new ArrayList<Integer>();
- list.add(R.drawable.huoying);
- list.add(R.drawable.caomao);
- list.add(R.drawable.yinhun);
- list.add(R.drawable.diguang);
- list.add(R.drawable.jianxin);
- vp_ad.setAdapter(new ViewPagerAdvAdapter(getActivity(), list));
- vp_ad.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
- @Override
- public void onPageSelected(int position) {
- super.onPageSelected(position);
- setCurPoint(position);
- }
- });
- }
- private void setCurPoint(int index){
- if(index<0||index>mImageViews.length||index==currentPosition){
- return;
- }
- mImageViews[currentPosition].setEnabled(true);
- mImageViews[index].setEnabled(false);
- //set tv title
- switch(index){
- case 0:
- tv_title.setText("火影忍者");
- break;
- case 1:
- tv_title.setText("海贼王");
- break;
- case 2:
- tv_title.setText("银魂");
- break;
- case 3:
- tv_title.setText("黑子的篮球");
- break;
- case 4:
- tv_title.setText("浪客剑心");
- break;
- }
- currentPosition = index;
- }
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- return inflater.inflate(R.layout.fragment_home, null);
- }
- @Override
- public void onDestroyView() {
- super.onDestroyView();
- }
- @Override
- public void onViewCreated(View view, Bundle savedInstanceState) {
- super.onViewCreated(view, savedInstanceState);
- vp_ad = (ViewPager) view.findViewById(R.id.vp_ad);
- tv_title = (TextView) view.findViewById(R.id.tv_title);
- createPoint(view);
- }
- private void createPoint(View view) {
- // six index round point
- LinearLayout ll = (LinearLayout) view.findViewById(R.id.llayout);
- LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
- LinearLayout.LayoutParams.WRAP_CONTENT,
- LinearLayout.LayoutParams.WRAP_CONTENT);
- lp.setMargins(0, 0, 12, 0);
- mImageViews = new ImageView[5];
- for (int i = 0; i < mImageViews.length; i++) {
- mImageViews[i] = new ImageView(getActivity());
- mImageViews[i].setImageResource(R.drawable.guide_round);
- mImageViews[i].setEnabled(true);
- mImageViews[i].setLayoutParams(lp);
- ll.addView(mImageViews[i]);
- }
- mImageViews[currentPosition].setEnabled(false);
- }
- }
复制代码
所对应的
fragment_hom.xml
- <?xml version="1.0" encoding="utf-8"?>
- <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" >
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@color/white"
- android:orientation="vertical" >
- <FrameLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="10dp"
- android:layout_marginRight="10dp"
- android:layout_marginTop="4dp" >
- <android.support.v4.view.ViewPager
- android:id="@+id/vp_ad"
- android:layout_width="match_parent"
- android:layout_height="180dp" />
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="30dp"
- android:layout_gravity="bottom"
- android:background="#6000"
- android:gravity="center_vertical" >
- <TextView
- android:id="@+id/tv_title"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:layout_marginLeft="2dp"
- android:text="火影忍者" />
- <LinearLayout
- android:id="@+id/llayout"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="right|bottom"
- android:layout_marginBottom="2dp"
- android:orientation="horizontal" >
- </LinearLayout>
- </LinearLayout>
- </FrameLayout>
- <GridView
- android:id="@+id/gv_category"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="10dp"
- android:layout_marginRight="10dp"
- android:background="@color/white"
- android:cacheColorHint="@android:color/transparent"
- android:fadingEdge="none"
- android:gravity="center"
- android:horizontalSpacing="7dp"
- android:numColumns="4"
- android:paddingBottom="10dip"
- android:paddingLeft="15dp"
- android:paddingTop="20dip"
- android:scrollbarStyle="outsideInset"
- android:verticalSpacing="20dip" />
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="10dp"
- android:layout_marginRight="10dp"
- android:background="@color/home_group_item_e4"
- android:gravity="center_vertical"
- android:paddingBottom="5dp"
- android:paddingLeft="10dp"
- android:paddingTop="5dp"
- android:text="漫画推荐"
- android:textColor="@color/home_group_item_txt_67"
- android:textSize="14sp" />
- <GridView
- android:id="@+id/gv_category"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="10dp"
- android:layout_marginRight="10dp"
- android:background="@color/white"
- android:cacheColorHint="@android:color/transparent"
- android:fadingEdge="none"
- android:gravity="center"
- android:horizontalSpacing="7dp"
- android:numColumns="4"
- android:paddingBottom="10dip"
- android:paddingLeft="15dp"
- android:paddingTop="20dip"
- android:scrollbarStyle="outsideInset"
- android:verticalSpacing="20dip" />
- </LinearLayout>
- </ScrollView>
复制代码
适配器:
- package com.cjj.shopapp.adapter;
- import java.util.ArrayList;
- import java.util.List;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Bundle;
- import android.support.v4.view.PagerAdapter;
- import android.support.v4.view.ViewPager;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.ImageView;
- /**
- * 首页商家广告适配器
- *
- */
- public class ViewPagerAdvAdapter extends PagerAdapter implements OnClickListener{
- private List<Integer> mDatas;
- private List<ImageView> mViews;
- private ImageView mImageView;
- private Context mContext;
- private Bundle mBundle;
- private Intent mIntent;
- public ViewPagerAdvAdapter(Context mContext,List<Integer> mDatas)
- {
- mIntent = new Intent();
- mBundle = new Bundle();
- this.mContext = mContext;
- mViews = new ArrayList<ImageView>();
- this.mDatas = mDatas;
- int length = mDatas == null ? 0 : mDatas.size();
- for(int i=0;i<length;i++)
- {
- ImageView mImageView = new ImageView(mContext);
- mViews.add(mImageView);
- }
- length = 0;
- }
- @Override
- public int getCount() {
- return mDatas == null ? 0 : mDatas.size();
- }
- @Override
- public boolean isViewFromObject(View arg0, Object arg1) {
- return arg0==(arg1);
- }
- @Override
- public Object instantiateItem(View container, int position) {
- int pos = mDatas.get(position);
- mImageView = mViews.get(position);
- mImageView.setAdjustViewBounds(true);
- mImageView.setScaleType(ImageView.ScaleType.FIT_XY);
- mImageView.setTag(pos);
- mImageView.setOnClickListener(this);
- mImageView.setImageResource(pos);
- ((ViewPager)container).addView(mImageView,0);
- return mImageView;
- }
- @Override
- public void destroyItem(View container, int position, Object object) {
- mImageView = mViews.get(position);
- ((ViewPager)container).removeView(mImageView);
- }
- @Override
- public void onClick(View v) {
- }
- }
复制代码
就写了这么多,手好了我继续写。。。呵呵。。。。吐槽下自己:别再荒芜时间了,别把自己玩坏了,自己让自己少操心吧。。。。好吧,,,我承认自己是个白痴啊。。。。。