商城之首页商品分类
来源:互联网 发布:java工程发布到tomcat 编辑:程序博客网 时间:2024/04/30 10:42
1.效果图
实现:RecycleView + CardView
2.CardView的使用
CardView: http://blog.csdn.net/u012360634/article/details/53184472
(0)CardView
1.CardView :设置阴影,圆角,等等。。 ,cardview其实继承的是Fraglayout
cardView.setRadius(8);//设置图片圆角的半径大小
cardView.setCardElevation(8);//设置阴影部分大小
cardView.setContentPadding(5,5,5,5);//设置图片距离阴影大小
(1)gradle添加引用
compile 'com.android.support:cardview-v7:23.0.1'
????? app
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" app:cardBackgroundColor="#fff" app:cardCornerRadius="4dp" app:contentPadding="10dp"></android.support.v7.widget.CardView>
(2)template_home_cardview.xml---------第一种item布局
<?xml version="1.0" encoding="utf-8"?><android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_gravity="center" android:gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content" app:cardBackgroundColor="#fff" app:contentPadding="10dp" app:cardCornerRadius="4dp"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/text_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="热门活动" android:textSize="20dp" android:textColor="@color/black" android:paddingTop="10dp" /> <View style="@style/line_vertical" android:layout_marginBottom="10dp" android:layout_marginTop="10dp"></View> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/imgview_big" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <View android:id="@+id/line" style="@style/line_horizontal" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <ImageView android:id="@+id/imgview_small_top" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <View android:id="@+id/line2" style="@style/line_vertical" ></View> <ImageView android:id="@+id/imgview_small_bottom" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout> </LinearLayout></android.support.v7.widget.CardView>
(3)template_home_cardview2.xml ---第二种布局
<?xml version="1.0" encoding="utf-8"?><android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_gravity="center" android:gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content" app:cardBackgroundColor="#fff" app:contentPadding="10dp" app:cardCornerRadius="4dp"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/text_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="热门活动" android:textSize="20dp" android:textColor="@color/black" android:paddingTop="10dp" /> <View style="@style/line_vertical" android:layout_marginBottom="10dp" android:layout_marginTop="10dp"></View> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:id="@+id/layout_left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <ImageView android:id="@+id/imgview_small_top" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/img_0_small1" /> <View android:id="@+id/line2" style="@style/line_vertical" ></View> <ImageView android:id="@+id/imgview_small_bottom" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/img_0_small2" /> </LinearLayout> <View android:id="@+id/line" style="@style/line_horizontal" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" /> <ImageView android:id="@+id/imgview_big" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/img_big_0" /> </LinearLayout> </LinearLayout></android.support.v7.widget.CardView>
(4)fragment_home.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <com.daimajia.slider.library.SliderLayout android:id="@+id/slider" android:layout_width="match_parent" android:layout_height="200dp" /> <!--<com.daimajia.slider.library.Indicators.PagerIndicator--> <!--android:id="@+id/custom_indicator"--> <!--android:layout_width="wrap_content"--> <!--android:layout_height="wrap_content"--> <!--android:gravity="center"--> <!--custom:selected_color="#FF5500"--> <!--custom:unselected_color="#55333333"--> <!--custom:shape="rect"--> <!--custom:selected_padding_left="2dp"--> <!--custom:selected_padding_right="2dp"--> <!--custom:unselected_padding_left="2dp"--> <!--custom:unselected_padding_right="2dp"--> <!--custom:selected_width="16dp"--> <!--custom:selected_height="3dp"--> <!--custom:unselected_width="16dp"--> <!--custom:unselected_height="3dp"--> <!--android:layout_gravity="center"--> <!--/>--> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="wrap_content"> </android.support.v7.widget.RecyclerView></LinearLayout>
(5)HomeCatgoryAdapter
package cniao5.com.cniao5shop.adapter;import android.support.v7.widget.RecyclerView;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ImageView;import android.widget.TextView;import java.util.List;import cniao5.com.cniao5shop.R;import cniao5.com.cniao5shop.bean.HomeCategory;/** * Created by Ivan on 15/9/30. */public class HomeCatgoryAdapter extends RecyclerView.Adapter<HomeCatgoryAdapter.ViewHolder> { private static int VIEW_TYPE_L=0; private static int VIEW_TYPE_R=1; private LayoutInflater mInflater; private List<HomeCategory> mDatas;//传数据 public HomeCatgoryAdapter(List<HomeCategory> datas){ mDatas = datas; } @Override public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int type) { //type由getItemViewType返回 mInflater = LayoutInflater.from(viewGroup.getContext());//根据不同的下标显示不同的布局 if(type == VIEW_TYPE_R){ return new ViewHolder(mInflater.inflate(R.layout.template_home_cardview2,viewGroup,false)); } return new ViewHolder(mInflater.inflate(R.layout.template_home_cardview,viewGroup,false)); } @Override public void onBindViewHolder(ViewHolder viewHolder, int i) { //绑定数据 HomeCategory category = mDatas.get(i); viewHolder.textTitle.setText(category.getName()); viewHolder.imageViewBig.setImageResource(category.getImgBig()); viewHolder.imageViewSmallTop.setImageResource(category.getImgSmallTop()); viewHolder.imageViewSmallBottom.setImageResource(category.getImgSmallBottom()); } @Override public int getItemCount() { return mDatas.size(); }//获取item的下标 @Override public int getItemViewType(int position) { if(position % 2==0){ return VIEW_TYPE_R; } else return VIEW_TYPE_L; } static class ViewHolder extends RecyclerView.ViewHolder{ TextView textTitle; ImageView imageViewBig; ImageView imageViewSmallTop; ImageView imageViewSmallBottom; public ViewHolder(View itemView) { super(itemView); textTitle = (TextView) itemView.findViewById(R.id.text_title); imageViewBig = (ImageView) itemView.findViewById(R.id.imgview_big); imageViewSmallTop = (ImageView) itemView.findViewById(R.id.imgview_small_top); imageViewSmallBottom = (ImageView) itemView.findViewById(R.id.imgview_small_bottom); } }}
(6)DividerItemDecortion 分割线
package cniao5.com.cniao5shop.adapter;import android.graphics.Canvas;import android.graphics.Rect;import android.support.v7.widget.LinearLayoutManager;import android.support.v7.widget.RecyclerView;import android.view.View;public class DividerItemDecortion extends RecyclerView.ItemDecoration { @Override public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { super.onDraw(c, parent, state); } @Override public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) { super.onDrawOver(c, parent, state); } @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { int layoutOrientation = getOrientation(parent); if (layoutOrientation == LinearLayoutManager.VERTICAL) { outRect.top = 10; outRect.left=5; outRect.right=5; } else if(layoutOrientation == LinearLayoutManager.HORIZONTAL) { outRect.left = 5; } } private int getOrientation(RecyclerView parent) { if (parent.getLayoutManager() instanceof LinearLayoutManager) { LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager(); return layoutManager.getOrientation(); } else throw new IllegalStateException("DividerItemDecoration can only be used with a LinearLayoutManager."); }}
7.HomeFragment.java
package cniao5.com.cniao5shop.fragment;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.support.v7.widget.LinearLayoutManager;import android.support.v7.widget.RecyclerView;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.Toast;import com.daimajia.slider.library.Animations.DescriptionAnimation;import com.daimajia.slider.library.Indicators.PagerIndicator;import com.daimajia.slider.library.SliderLayout;import com.daimajia.slider.library.SliderTypes.BaseSliderView;import com.daimajia.slider.library.SliderTypes.DefaultSliderView;import com.daimajia.slider.library.SliderTypes.TextSliderView;import com.daimajia.slider.library.Tricks.ViewPagerEx;import java.util.ArrayList;import java.util.List;import cniao5.com.cniao5shop.R;import cniao5.com.cniao5shop.adapter.DividerItemDecortion;import cniao5.com.cniao5shop.adapter.HomeCatgoryAdapter;import cniao5.com.cniao5shop.bean.HomeCategory;/** * Created by Ivan on 15/9/25. */public class HomeFragment extends Fragment { private SliderLayout mSliderLayout;// private PagerIndicator indicator; private RecyclerView mRecyclerView; private HomeCatgoryAdapter mAdatper; private static final String TAG="HomeFragment"; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_home,container,false); mSliderLayout = (SliderLayout) view.findViewById(R.id.slider);// indicator= (PagerIndicator) view.findViewById(R.id.custom_indicator); initSlider(); initRecyclerView(view); return view; } private void initRecyclerView(View view) { mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerview); List<HomeCategory> datas = new ArrayList<>(15); HomeCategory category = new HomeCategory("热门活动",R.drawable.img_big_1,R.drawable.img_1_small1,R.drawable.img_1_small2); datas.add(category); category = new HomeCategory("有利可图",R.drawable.img_big_4,R.drawable.img_4_small1,R.drawable.img_4_small2); datas.add(category); category = new HomeCategory("品牌街",R.drawable.img_big_2,R.drawable.img_2_small1,R.drawable.img_2_small2); datas.add(category); category = new HomeCategory("金融街 包赚翻",R.drawable.img_big_1,R.drawable.img_3_small1,R.drawable.imag_3_small2); datas.add(category); category = new HomeCategory("超值购",R.drawable.img_big_0,R.drawable.img_0_small1,R.drawable.img_0_small2); datas.add(category); mAdatper = new HomeCatgoryAdapter(datas); mRecyclerView.setAdapter(mAdatper); mRecyclerView.addItemDecoration(new DividerItemDecortion()); mRecyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity())); } private void initSlider(){ DefaultSliderView textSliderView = new DefaultSliderView(this.getActivity()); textSliderView.image("http://m.360buyimg.com/mobilecms/s480x180_jfs/t2278/35/409524152/232719/1d29f7a9/56078dbfNae4f16a3.jpg"); textSliderView.description("aaaa"); textSliderView.setScaleType(BaseSliderView.ScaleType.Fit); DefaultSliderView textSliderView2 = new DefaultSliderView(this.getActivity()); textSliderView2.image("http://m.360buyimg.com/mobilecms/s480x180_jfs/t2071/116/426908452/111248/3e6d388c/5608a437N723ee2ba.jpg"); textSliderView2.description("bbbb"); textSliderView2.setScaleType(BaseSliderView.ScaleType.Fit); DefaultSliderView textSliderView3 = new DefaultSliderView(this.getActivity()); textSliderView3.image("http://m.360buyimg.com/mobilecms/s480x180_jfs/t2113/230/413819408/114393/d8a62616/56078bacN9c9c6dc8.jpg"); textSliderView3.description("秒杀"); textSliderView3.setScaleType(BaseSliderView.ScaleType.Fit); mSliderLayout.addSlider(textSliderView); mSliderLayout.addSlider(textSliderView2); mSliderLayout.addSlider(textSliderView3); mSliderLayout.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);// mSliderLayout.setCustomIndicator(indicator); mSliderLayout.setCustomAnimation(new DescriptionAnimation()); mSliderLayout.setPresetTransformer(SliderLayout.Transformer.RotateUp); mSliderLayout.setDuration(3000); mSliderLayout.addOnPageChangeListener(new ViewPagerEx.OnPageChangeListener() { @Override public void onPageScrolled(int i, float v, int i1) { Log.d(TAG,"onPageScrolled"); } @Override public void onPageSelected(int i) { Log.d(TAG,"onPageSelected"); } @Override public void onPageScrollStateChanged(int i) { Log.d(TAG,"onPageScrollStateChanged"); } }); } @Override public void onDestroy() { super.onDestroy(); mSliderLayout.stopAutoCycle(); }}
0 0
- 商城之首页商品分类
- 商城之商品分类
- 商城之主页商品分类重构
- 商城项目实战 | 5.1 RecyclerView 和 CardView 更配 实现首页商品分类
- 商城开发之商品分类表的设计、使用、管理
- 淘宝商城商品分类特效代码
- ecshop二次开发之首页调用指定分类下的商品
- 商城之商品列表
- 商城之商品分享
- B2Ctt商城05 前台首页商品展示
- 16.首页查询一级分类商品(分类显示商品)
- 仿京东商城商品分类搜索功能
- 京东商城的商品分类代码
- 商城系统03-商品分类总结
- 微信小程序之商城首页
- 微信小程序之商品属性分类 —— 微信小程序实战商城系列(4)
- 仿淘宝首页商品分类列表效果
- ecshop首页分类栏目商品消失
- 2016华为校招上机试题1——交叉排序
- 【SSM 6】Spring+SpringMVC+Mybatis框架搭建步骤
- 判断linux是64位还是32位
- 1115. Counting Nodes in a BST (30) 二叉树
- Java那点事儿总类
- 商城之首页商品分类
- Spring的AOP原理
- 大牛文章:理解矩阵
- Linux配置程序开机自启动
- linux常用命令
- 1116. Come on! Let's C (20) 简单模拟
- Java for Web学习笔记(二九):JSTL(5)FMT Tag(上)
- 【安卓-自定义布局】安卓App开发思路 一步一个脚印(六)实现ViewPager最基本的
- java反射详解