Android使用Scroll+Fragment仿京东分类效果
作者:火炎焱燚- 发布时间:2022-11-17 16:56:48
标签:Android,京东分类
本文实例为大家分享了Android九宫格图片展示的具体代码,供大家参考,具体内容如下
实现思路:首先说下布局,整个是一个横向的线性布局,左边是一个ScrollView,右边是一个FrameLayout,在代码中动态向ScrollView中添加TextView,然后根据TextView的点击事件使用Fragment替换FrameLayout
首先看下布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context="www.jdsort.com.jdsort.MainActivity">
<ScrollView
android:id="@+id/scrollview"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scrollbars="none">
<LinearLayout
android:id="@+id/linearlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical" />
</ScrollView>
<FrameLayout
android:id="@+id/framelayout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"/>
</LinearLayout>
MainActivity代码:
public class MainActivity extends FragmentActivity implements View.OnClickListener {
private String[] titles={"常用分类","潮流女装","品牌男装","内衣配饰","家用电器","手机数码","电脑办公","个护化妆","母婴频道","食物生鲜","酒水饮料","家居家纺","整车车品","鞋靴箱包","运动户外","图书","玩具乐器","钟表","居家生活","珠宝饰品","音像制品","家具建材","计生情趣","营养保健","奢侈礼品","生活服务","旅游出行"};
private ScrollView mScrollView;
private FrameLayout mFrameLayout;
//装装ScrollView的item的TextView的数组
private TextView[] textViewArray;
//装ScrollView的item的数组
private View[] views;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
textViewArray=new TextView[titles.length];
views=new View[titles.length];
initView();
getSupportFragmentManager().beginTransaction().replace(R.id.framelayout,new FragmentOne()).commit();
}
private void initView() {
mScrollView= (ScrollView) findViewById(R.id.scrollview);
addView();
changeTextColor(0);
mFrameLayout= (FrameLayout) findViewById(R.id.framelayout);
}
/**
* 给ScrollView添加子View
*/
private void addView() {
LinearLayout mLinearLayout= (LinearLayout) findViewById(R.id.linearlayout);
View view;
for(int x=0;x<titles.length;x++){
view = View.inflate(this, R.layout.item_scrollview, null);
view.setId(x);
view.setOnClickListener(this);
TextView tv= (TextView) view.findViewById(R.id.textview);
tv.setText(titles[x]);
mLinearLayout.addView(view);
textViewArray[x]=tv;
views[x]=view;
}
}
@Override
public void onClick(View v) {
ToastUtils.showToast(this,titles[(int) v.getId()]);
changeTextColor((int) v.getId());
changeTextLocation((int) v.getId());
Fragment fragment=null;
switch (v.getId()){
case 0:
fragment=new FragmentOne();
break;
case 1:
fragment=new Fragment1();
break;
case 2:
fragment=new Fragment2();
break;
case 3:
fragment=new Fragment3();
break;
case 4:
fragment=new Fragment4();
break;
case 5:
fragment=new Fragment5();
break;
case 6:
fragment=new Fragment6();
break;
case 7:
fragment=new Fragment7();
break;
}
if(fragment!=null){
getSupportFragmentManager().beginTransaction().replace(R.id.framelayout,fragment).commit();
}
}
/**
* 改变textView的颜色
* @param id
*/
private void changeTextColor(int id) {
for (int i = 0; i < textViewArray.length; i++) {
if(i!=id){
textViewArray[i].setBackgroundResource(android.R.color.transparent);
textViewArray[i].setTextColor(0xff000000);
}else {
textViewArray[id].setBackgroundResource(android.R.color.white);
textViewArray[id].setTextColor(0xffff5d5e);
}
}
}
/**
* 改变栏目位置
*/
private void changeTextLocation(int index) {
//views[clickPosition].getTop()针对其父视图的顶部相对位置
int x = (views[index].getTop() - mScrollView.getHeight() / 2);
mScrollView.smoothScrollTo(0, x);
}
}
源码等上传上去会再次发布链接,如果那里写的不好,欢迎私信,评论指导
源码下载:Scroll+Fragment仿京东分类效果
来源:http://blog.csdn.net/Zheng_Jiao/article/details/51396647
0
投稿
猜你喜欢
- java 集合之Stack概要:Stack是栈,特性是先进后出(FILO,First In Last Out)。Stack是继承
- 在JSP里,获取客户端的IP地址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的。但是在通过了Ap
- 一、项目简述功能: 主页显示商品; 所有二手书商品展示,可进行商品搜索; 点击商品进入商品详情页,具有立即购买和加入购物车功能,可增减购买商
- java调用外部程序的方法 在一个java应用中,可能会遇到这样的需求,就是需要调用一些外部的应用做一些处理,比如调用excel,
- 百度百科说法:Servlet(Server Applet)是Java Servlet的简称,称为小服务程序或服务连接器,用Java编写的服务
- 写在前面 众所周知,kafka是现代流行的消息队列,它使用经典的消息订阅发布模式实现消息的流转,大部分代码结合kaf
- 顺序结构顺序结构就是按照代码从上往下执行,我们运行的程序就是从上往下的顺序结构,当遇到方法的时候,才去执行方法。例如:System.out.
- 1. 运算符是什么?1.1 定义:对常量和变量进行运算操作的符号程序对数据进行运算时要用运算符1.2 常见运算符的概述1.3 表达式1.3.
- FrameLayout 在这个布局中,所有的子元素都不能被指定放置的位置,他们统统防御这块区域的左上角, 并且后面的子元素直接覆盖在前面的子
- 如下所示:String beginDate="1328007600000";SimpleDateFormat sdf=n
- springboot多模块项目添加一新模块选择Maven Module,填写模块名若空白,catalog目录可选择internal更改包名完
- 单链表:每个数据是以节点的形式存在的每个节点分为数据域和指针域数据域中保存该节点的数据指针域中保存指向下一个节点的指针实现思路:节点类Sin
- static 表示静态,它可以修饰属性,方法和代码块。1.static修饰属性(类变量),那么这个属性就可以用类名.属性名来访问,也就是使这
- Jenkins集成Sonar过程中遇到的报错1、jenkins中无法添加sonarqube的token凭证因为添加的凭证类型错误,所以无法添
- 本文实例讲述了C#实现的阴历阳历互相转化类。分享给大家供大家参考,具体如下:最近郁闷地发现网上现有的相当一部分万年历上干支纪年的算法都是错误
- 主要为以下实现步骤:1.绑定域名先登录微信公众平台进入“公众号设置”的“功能设置”里填写“JS接口安全域名”。(特别提示不需要加上http或
- 本文实例讲述了C# Console利用mspaint打开图像并保存的方法。分享给大家供大家参考,具体如下:调用画图板压缩图片System.D
- 集成引入依赖在项目的pom.xml中增加以下依赖<dependency> <groupId>org.s
- 一、系统介绍 1.系统功能登录系统查询信息新增信息修改信息删除信息2.环境配置JDK版本:1.8Mysql:8.0.133.数据库
- 感觉很久不写模拟器代码了,昨天调试的时候碰了点壁,记录下来,避免大家再跟我犯同样的错误。加入Javascript脚本的地方:HtmlElem