分类页面所有view层(activity 跟 布局 跟fragment以及布局)
2018-03-05 13:02
411 查看
MainActivity:
public class MainActivity extends AppCompatActivity implements IFenlei_view { private ListView fen_lei_list_view; private FrameLayout fen_lei_frame; private Fenlei_presenter fenlei_presenter; private Fenlei_adapter fenlei_adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); fen_lei_list_view = findViewById(R.id.fen_lei_list_view); fen_lei_frame = findViewById(R.id.fen_lei_frame); fenlei_presenter = new Fenlei_presenter(this); fenlei_presenter.getFenleiData("https://www.zhaoapi.cn/product/getCatagory"); } @Override public void postFenlei(final GoodsFenleiBean goodsFenleiBean) { runOnUiThread(new Runnable() { @Override public void run() { fenlei_adapter = new Fenlei_adapter(goodsFenleiBean,MainActivity.this); fen_lei_list_view.setAdapter(fenlei_adapter); fen_lei_list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { //设置适配器当前位置的方法 fenlei_adapter.setCurPositon(i); fenlei_adapter.notifyDataSetChanged(); FragmentFenLeiRight fragmentFenLeiRight=FragmentFenLeiRight.getInstance(goodsFenleiBean.getData().get(i).getCid()); getSupportFragmentManager().beginTransaction().replace(R.id.fen_lei_frame,fragmentFenLeiRight).commit(); } }); } }); } }
MainActivity布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.bwie.yuekao.view.MainActivity"> <LinearLayout android:layout_width="match_parent" android:gravity="center" android:layout_height="40dp" android:background="#44ff" android:orientation="horizontal"> <TextView android:layout_width=[b]"wrap_content" android:layout_height="wrap_content" android:text="分类" android:textSize="20dp" android:layout_gravity="center" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:background="#F3F3F6" android:divider="#F3F3F6" android:dividerHeight="0.1dp" android:id="@+id/fen_lei_list_view" android:scrollbars="none" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent"> </ListView> <FrameLayout android:id="@+id/fen_lei_frame" android:layout_width="0dp" android:layout_weight="3" android:layout_height="match_parent"> </FrameLayout> </LinearLayout> </LinearLayout>
DetailActivity:
public class DetailActivity extends AppCompatActivity implements DetailActivityInter,ActivityAddCartInter, View.OnClickListener { private int pid; private DeatailPresenter deatailPresenter; private Banner banner; private TextView detail_title; private TextView detail_bargin_price; private TextView detail_yuan_price; private TextView detai_add_cart; private ImageView detail_image_back; private TextView watch_cart; private AddCartPresenter addCartPresenter; private ImageView detail_share; private DeatilBean deatilBean; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_detail); banner = findViewById(R.id.banner); detail_title = findViewById(R.id.detail_title); detail_bargin_price = findViewById(R.id.detail_bargin_price); detail_yuan_price = findViewById(R.id.detail_yuan_price); detai_add_cart = findViewById(R.id.detai_add_cart); detail_image_back = findViewById(R.id.detail_image_back); watch_cart = findViewById(R.id.watch_cart); detail_share = findViewById(R.id.detail_share); //创建presenter deatailPresenter = new DeatailPresenter(this); addCartPresenter = new AddCartPresenter(this); //接收传递的pid pid = getIntent().getIntExtra("pid", -1); //如果不是默认值代表传递过来数据了 if (pid != -1){ //拿着传递的pid请求商品详情的接口,然后展示数据...MVP deatailPresenter.getDetailData(ApiUtil.DETAIL_URL,pid); } //初始化banner initBanner(); //设置点击事件 detai_add_cart.setOnClickListener(this); detail_image_back.setOnClickListener(this); watch_cart.setOnClickListener(this); detail_share.setOnClickListener(this); } @Override public void onSuccess(final DeatilBean deatilBean) { this.deatilBean = deatilBean; runOnUiThread(new Runnable() { @Override public void run() { //添加删除线 detail_yuan_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //设置数据显示 detail_title.setText(deatilBean.getData().getTitle()); detail_bargin_price.setText("优惠价:"+deatilBean.getData().getBargainPrice()); detail_yuan_price.setText("原价:"+deatilBean.getData().getPrice()); String[] strings = deatilBean.getData().getImages().split("\\|"); final ArrayList<String> imageUrls = new ArrayList<>(); for (int i = 0;i<strings.length;i++){ imageUrls.add(strings[i]); } banner.setImages(imageUrls); //bannner点击事件进行跳转 banner.setOnBannerListener(new OnBannerListener() { @Override public void OnBannerClick(int position) { Intent intent = new Intent(DetailActivity.this,ImageScaleActivity.class); //传递的数据...整个轮播图数据的集合需要传递,,,当前展示的图片的位置需要传递postion //intent传递可以传的数据...基本数据类型...引用数据类型(必须序列化,所有的类,包括内部类实现serilizable接口)...bundle intent.putStringArrayListExtra("imageUrls",imageUrls); intent.putExtra("position",position); startActivity(intent); } }); banner.start(); } }); } private void initBanner() { //设置banner样式...CIRCLE_INDICATOR_TITLE包含标题 banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR); //设置图片加载器 // banner.setImageLoader(new GlideImageLoader()); //设置自动轮播,默认为true banner.isAutoPlay(true); //设置轮播时间 banner.setDelayTime(2500); //设置指示器位置(当banner模式中有指示器时) banner.setIndicatorGravity(BannerConfig.CENTER); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.detail_image_back://返回 //finish() startActivity() setResult()...context.startActiivty() //DetailActivity.this.finish(); finish(); break; } } @Override public void onCartAddSuccess(AddCartBean addCartBean) { Toast.makeText(DetailActivity.this,addCartBean.getMsg(),Toast.LENGTH_SHORT).show(); } }
DetailActivity布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.bwie.yuekao.view.activity.DetailActivity"> <RelativeLayout android:id="@+id/detai_relative" android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/detail_image_back" android:padding="5dp" android:src="@drawable/leftjiantou" android:layout_width="40dp" android:layout_height="40dp" /> <TextView android:layout_centerInParent="true" android:text="商品详情" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/detail_share" android:padding="5dp" android:layout_alignParentRight="true" android:src="@drawable/share" android:layout_width="40dp" android:layout_height="40dp" /> </RelativeLayout> <!--轮播图--> <com.youth.banner.Banner android:layout_below="@+id/detai_relative" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/banner" android:layout_width="match_parent" android:layout_height="300dp" /> <TextView android:layout_below="@+id/banner" android:layout_margin="10dp" android:id="@+id/detail_title" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:text="aaaa" android:layout_below="@+id/detail_title" android:layout_margin="10dp" android:id="@+id/detail_yuan_price" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:textColor="#ff0000" android:layout_below="@+id/detail_yuan_price" android:layout_margin="10dp" android:id="@+id/detail_bargin_price" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <LinearLayout android:layout_alignParentBottom="true" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <View android:background="#000000" android:layout_width="match_parent" android:layout_height="0.3dp"/> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/watch_cart" android:padding="10dp" android:text="购物车" android:gravity="center" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" /> <TextView android:id="@+id/detai_add_cart" android:padding="10dp" android:text="加入购物车" android:gravity="center" android:layout_width="0dp" android:layout_weight="1" android:background="#ff0000" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout> </RelativeLayout>
ImageScaleActivity:
public class ImageScaleActivity extends AppCompatActivity { private ArrayList<String> imageUrls; private int position; private ViewPager iamge_scale_pager; private TextView image_scale_text; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_image_scale); iamge_scale_pager = findViewById(R.id.iamge_scale_pager); image_scale_text = findViewById(R.id.image_scale_text); //接收数据 Intent intent = getIntent(); imageUrls = intent.getStringArrayListExtra("imageUrls"); position = intent.getIntExtra("position", -1); //判断 集合不为空null 集合的长度大于0 position不是-1 //给viewPager设置适配器 if (imageUrls != null && imageUrls.size() > 0 && position != -1) { //1.textView展示第几张图片 image_scale_text.setText((position + 1) + "/" + imageUrls.size()); //2.设置适配器 ImageScaleAdapter imageScaleAdapter = new ImageScaleAdapter(); iamge_scale_pager.setAdapter(imageScaleAdapter); //3.设置显示的是点击的那张图片..setCurrentItem() //boolean smoothScroll...是否滚动 iamge_scale_pager.setCurrentItem(position, false); //设置监听 iamge_scale_pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { //设置文本显示 image_scale_text.setText((position + 1) + "/" + imageUrls.size()); } @Override public void onPageScrollStateChanged(int state) { } }); } } private class ImageScaleAdapter extends PagerAdapter { @Override public int getCount() { return imageUrls.size(); } @Override public boolean isViewFromObject(@NonNull View view, @NonNull Object object) { return view == object; } @NonNull @Override public Object instantiateItem(@NonNull ViewGroup container, int position) { //创建imageView(可以放大缩小的iamgeView...自定义的) ZoomImageView zoomImageView = new ZoomImageView(ImageScaleActivity.this); //使用glide加载图片 Glide.with(ImageScaleActivity.this).load(imageUrls.get(position)).into(zoomImageView); //添加到容器container container.addView(zoomImageView); //返回这个view return zoomImageView; } @Override public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) { //从容器中移除这个view container.removeView((View) object); } } }
ImageScaleActivity布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.bwie.yuekao.view.activity.ImageScaleActivity"> <android.support.v4.view.ViewPager android:id="@+id/iamge_scale_pager" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v4.view.ViewPager> <TextView android:id="@+id/image_scale_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_margin="10dp" android:text="2/5" android:textColor="#66000000" android:textSize="30sp" /> </RelativeLayout>
ProductListActivity:
public class ProductListActivity extends AppCompatActivity implements ProductListActivityInter, View.OnClickListener { private String keywords; private RecyclerView product_list_recycler; private RecyclerView product_grid_recycler; private ProductListPresenter productListPresenter; private int page = 1; private ProDuctListAdapter proDuctListAdapter; private ProDuctGridAdapter proDuctGridAdapter; private ImageView product_image_back; private LinearLayout linear_search; private ImageView image_change; private boolean isList = true;//是否是列表展示 private RefreshLayout refreshLayout; private List<ProductListBean.DataBean> listAll = new ArrayList<>();//装当前页面所有的数据 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_product_list); product_list_recycler = findViewById(R.id.product_list_recycler); product_grid_recycler = findViewById(R.id.product_grid_recycler); product_image_back = findViewById(R.id.product_image_back); linear_search = findViewById(R.id.linear_search); image_change = findViewById(R.id.image_change); refreshLayout = findViewById(R.id.refreshLayout); productListPresenter = new ProductListPresenter(this); //接收传递的关键词 keywords = getIntent().getStringExtra("keywords"); if (keywords != null) { //根据关键词和page去请求列表数据 productListPresenter.getProductData("https://www.zhaoapi.cn/product/searchProducts",keywords,page); } //设置列表布局 product_list_recycler.setLayoutManager(new LinearLayoutManager(ProductListActivity.this)); product_grid_recycler.setLayoutManager(new StaggeredGridLayoutManager(2, OrientationHelper.VERTICAL)); //设置点击事件 product_image_back.setOnClickListener(this); linear_search.setOnClickListener(this); image_change.setOnClickListener(this); //下拉刷新的监听 refreshLayout.setOnRefreshListener(new OnRefreshListener() { @Override public void onRefresh(RefreshLayout refreshlayout) { page = 1; //集合清空 listAll.clear(); //重新获取数据 productListPresenter.getProductData("https://www.zhaoapi.cn/product/searchProducts",keywords,page); } }); //上拉加载的监听 refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() { @Override public void onLoadmore(RefreshLayout refreshlayout) { page ++; //重新获取数据 productListPresenter.getProductData("https://www.zhaoapi.cn/product/searchProducts",keywords,page); } }); } @Override public void getProductDataSuccess(final ProductListBean productListBean) { runOnUiThread(new Runnable() { @Override public void run() { //先把数据添加到大集合 listAll.addAll(productListBean.getData()); //设置适配器就可以了 setAdapter(); //条目的点击事件 调到详情页面 proDuctListAdapter.setOnItemListner(new OnItemListner() { @Override public void onItemClick(int position) { //跳转详情 Intent intent = new Intent(ProductListActivity.this,DetailActivity.class); intent.putExtra("pid",listAll.get(position).getPid()); startActivity(intent); } @Override public void onItemLongClick(int position) { } }); proDuctGridAdapter.setOnItemListner(new OnItemListner() { @Override public void onItemClick(int position) { //跳转详情 Intent intent = new Intent(ProductListActivity.this,DetailActivity.class); intent.putExtra("pid",listAll.get(position).getPid()); startActivity(intent); } @Override public void onItemLongClick(int position) { } }); } }); } private void setAdapter() { //设置列表设备器 if (proDuctListAdapter == null) { proDuctListAdapter = new ProDuctListAdapter(ProductListActivity.this, listAll); product_list_recycler.setAdapter(proDuctListAdapter); }else { proDuctListAdapter.notifyDataSetChanged(); } //设置表格适配器 if (proDuctGridAdapter == null) { proDuctGridAdapter = new ProDuctGridAdapter(ProductListActivity.this, listAll); product_grid_recycler.setAdapter(proDuctGridAdapter); }else { proDuctGridAdapter.notifyDataSetChanged(); } //停止刷新和加载更多 refreshLayout.finishRefresh(); refreshLayout.finishLoadmore(); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.linear_search: Toast.makeText(this,"即将跳转搜索...",Toast.LENGTH_SHORT).show(); break; case R.id.product_image_back: finish(); break; case R.id.image_change: if (isList) {//表示当前展示的是列表..图标变成列表样式...表格进行显示,列表隐藏...isList---false image_change.setImageResource(R.drawable.kind_liner); product_grid_recycler.setVisibility(View.VISIBLE); product_list_recycler.setVisibility(View.GONE); isList = false; }else { image_change.setImageResource(R.drawable.kind_grid); product_list_recycler.setVisibility(View.VISIBLE); product_grid_recycler.setVisibility(View.GONE); isList = true; } break; } } }
ProductListActivity布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.bwie.yuekao.view.activity.ProductListActivity"> <!--标题--> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal" android:padding="5dp"> <ImageView android:id="@+id/product_image_back" android:layout_width="40dp" android:layout_height="40dp" android:padding="5dp" android:src="@drawable/leftjiantou" /> <LinearLayout android:id="@+id/linear_search" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginRight="5dp" android:layout_weight="1" android:background="@drawable/gray_back_conner" android:gravity="center_vertical" android:orientation="horizontal" android:padding="5dp"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:src="@drawable/a_4" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_weight="1" android:text="搜索京东商品/商铺" /> <ImageView android:layout_width="30dp" android:layout_height="30dp" android:layout_marginRight="5dp" android:scaleType="fitXY" android:src="@drawable/root" /> </LinearLayout> <ImageView android:id="@+id/image_change" android:layout_width="30dp" android:layout_height="30dp" android:scaleType="fitXY" android:src="@drawable/kind_grid" /> </LinearLayout> <com.scwang.smartrefresh.layout.SmartRefreshLayout android:id="@+id/refreshLayout" android:layout_width="match_parent" android:layout_height="match_parent"> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="none"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <!--商品列表--> <android.support.v7.widget.RecyclerView android:nestedScrollingEnabled="false" android:id="@+id/product_list_recycler" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> <android.support.v7.widget.RecyclerView android:nestedScrollingEnabled="false" android:id="@+id/product_grid_recycler" android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="gone"> </android.support.v7.widget.RecyclerView> </LinearLayout> </ScrollView> </com.scwang.smartrefresh.layout.SmartRefreshLayout> </LinearLayout>
Fragment:
FragmentFenLeiRight:
public class FragmentFenLeiRight extends Fragment implements IFenleiRight_view {private RecyclerView fen_lei_recycler_out;private FragmentFenLeiRightPresenter fragmentFenLeiRightPresenter;@Nullable@Overridepublic View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {View view = inflater.inflate(R.layout.fragment_fen_lei_right_layout, container, false);fen_lei_recycler_out = view.findViewById(R.id.fen_lei_recycler_out);return view ;}@Overridepublic void onActivityCreated(@Nullable Bundle savedInstanceState) {super.onActivityCreated(savedInstanceState);fragmentFenLeiRightPresenter = new FragmentFenLeiRightPresenter(this);//获取传递的cidint cid = getArguments().getInt("cid", -1);if(cid != -1){fragmentFenLeiRightPresenter.getChildData("https://www.zhaoapi.cn/product/getProductCatagory",cid);}}public static FragmentFenLeiRight getInstance(int cid) {FragmentFenLeiRight fragmentFenLeiRight = new FragmentFenLeiRight();//传值Bundle bundle = new Bundle();bundle.putInt("cid",cid);fragmentFenLeiRight.setArguments(bundle);return fragmentFenLeiRight;}@Overridepublic void postFenleiRight(final FenleiRightBean fenleiRightBean) {getActivity().runOnUiThread(new Runnable() {@Overridepublic void run() {fen_lei_recycler_out.setLayoutManager(new LinearLayoutManager(getActivity()));//设置适配器FenLeiRecyclerOutAdapter fenLeiRecyclerOutAdapter = new FenLeiRecyclerOutAdapter(fenleiRightBean,getActivity());fen_lei_recycler_out.setAdapter(fenLeiRecyclerOutAdapter);}});}}
Fragment布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><ScrollViewandroid:layout_width="match_parent"android:layout_height="match_parent"><LinearLayoutandroid:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><ImageViewandroid:src="@drawable/timg"android:scaleType="fitXY"android:layout_width="match_parent"android:layout_height="100dp" /><android.support.v7.widget.RecyclerViewandroid:id="@+id/fen_lei_recycler_out"android:layout_width="match_parent"android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView></LinearLayout></ScrollView></LinearLayout>
相关文章推荐
- TabLayout 和ViewPager和Fragment的多页面滑动(主要实现一个水平的布局用来展示Tabs加上ViewPager实现联动效果)
- 解决常见布局Viewpager+Fragment多页面切换销毁问题
- 无缝为Activity、Fragment、任何View设置加载(loading)、重试(retry)和无数据(empty)页面。
- Data binding 在Activity,Fragment中引用以及加载其他布局
- Fragment与ViewPager的结合做出局部滑动刷新页面,以及关于app.FragmentManager和v4包的FragmentPagerAdapter冲突
- ViewPager+Fragment实现主页面布局
- Fragment和activity的之间的数据传递以及页面跳转
- ViewPager 结合Fragment实现一个Activity里包含多个可滑动的标签页,每个标签页可以有独立的布局及响应。
- 使用viewpager+fragment,在activity启动模式为singleTask,跳转到当前页面重新加载数据fragment数据不更新
- TabLayout+ViewPager+Fragment主页面布局
- Activity获取布局中的view,以及LayoutInfalter实例化
- Viewpager+RadioButton+fragment页面布局
- 在Fragment中重写返回键以及WebView加载重定向页面返回问题
- 无缝为Activity、Fragment、任何View设置加载(loading)、重试(retry)和无数据(empty)页面
- Android开发从一个activity设置跳转到另一个activity中的一个fragment中的一个viewpager中的某一个页面
- 新闻详情页,scrollview view label 等控件的使用,以及页面布局
- Android使用Webview显示页面以及点击跳转startActivity问题
- Android Studio插件LayoutCreator在Activity/Fragment中自动生成findViewById等布局相关初始化代码 或者在Adapter中自动生成ViewHolder
- Android Fragment Activity View中集成React Native页面
- 左右RecyclerView,京东分类页面,静态Fragment传值。项目二升大实训必备!不用谢我,我叫雷锋!!!!