过期Gallery取代方案horizontalscrollView之菜单导航效果
效果
-------------------------------------------------------------------------------------
素材
-------------------------------------------------------------------------------------
activity_main.xml
-------------------------------------------------------------------------------------
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:background="@drawable/category_background" >
<Button
android:id="@+id/right"
android:layout_width="32dip"
android:layout_height="40dip"
android:layout_alignParentRight="true"
android:background="@drawable/category_array_right" />
<HorizontalScrollView
android:id="@+id/category_scrollview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="6dip"
android:layout_toLeftOf="@id/right"
android:scrollbars="none" >
<LinearLayout
android:id="@+id/category_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical" android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
category_title.xml
-------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/category_title"
android:layout_width="wrap_content"
android:layout_height="40.0dip"
android:textSize="15.0sp"
android:textStyle="bold"
android:gravity="center"
android:singleLine="true"
/>
MainActivity.java
-------------------------------------------------------------------------------------
package com.horizontalscrollView04;
import java.util.ArrayList;
import java.util.List;
import android.annotation.SuppressLint;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
*
* 项目名称:com.horizontalscrollView04
* 类 名 称:MainActivity
* 类 描 述: 滚动导航菜单
* 创 建 人:fy
* 创建时间:2014-3-23 下午3:23:29
* Copyright (c) 方勇-版权所有
*/
@SuppressLint("NewApi")
public class MainActivity extends Activity implements OnItemClickListener, OnClickListener {
private HorizontalScrollView horizontalScrollView;
/* 右边更多导航菜单 */
private Button right;
/* 导航菜单集合 */
private List<String> array;
/* 导航菜单适配器 */
private TitleAdapter titleAdapter;
/* 列宽 */
private final int COLUMNWIDTH = 75;
/* 导航菜单布局 */
private GridView category;
/* 导航菜单容器,存放导航菜单布局 */
private LinearLayout categoryLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViews();
setListeners();
initViews();
}
private void findViews() {
horizontalScrollView = (HorizontalScrollView) findViewById(R.id.category_scrollview);
right = (Button) findViewById(R.id.right);
categoryLayout = (LinearLayout) findViewById(R.id.category_layout);
// 新建一个GridView
category = new GridView(getApplicationContext());
}
private void setListeners() {
// 点击右边的滑动按钮可以让horizontalScrollView滑动
right.setOnClickListener(this);
// 设置GridView的点击事件
category.setOnItemClickListener(this);
}
private void initViews() {
// 栏目内容存放的数组
array = new ArrayList<String>();
array.add("tab1");
array.add("tab2");
array.add("tab3");
array.add("tab4");
array.add("tab5");
array.add("tab6");
array.add("tab7");
// 最简单的一个适配器,里面就一个TextView
titleAdapter = new TitleAdapter(getApplicationContext(), array);
// 设置内部子栏目的宽度
category.setColumnWidth(COLUMNWIDTH);
// 设置内部子栏目个数为自动适应
category.setNumColumns(GridView.AUTO_FIT);
// 设置Gravity为Center
category.setGravity(Gravity.CENTER);
// 设置Selector为透明
category.setSelector(new ColorDrawable(Color.TRANSPARENT));
int width = COLUMNWIDTH * array.size();
LayoutParams layoutParams = new LayoutParams(width, LayoutParams.WRAP_CONTENT);
// 设置GridView的LayoutParams为子栏目的宽度乘以栏目个数
category.setLayoutParams(layoutParams);
// 设置适配器
category.setAdapter(titleAdapter);
// 将新建的GridView添加到布局中
categoryLayout.addView(category);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
horizontalScrollView.fling(550);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView categoryTitle;
// 每次都循环的将子栏目的颜色和背景还原
for (int i = 0; i < parent.getCount(); i++) {
categoryTitle = (TextView) parent.getChildAt(i);
categoryTitle.setTextColor(0XFFADB2AD);
//categoryTitle.setBackground(null);
}
// 设置点击的栏目的文字颜色和背景图片
categoryTitle = (TextView) view;
categoryTitle.setTextColor(0XFFFFFFFF);
categoryTitle.setBackgroundResource(R.drawable.categorybar_item_background);
}
}
TitleAdapter.java
-------------------------------------------------------------------------------------
package com.horizontalscrollView04;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
/**
*
* 项目名称:com.horizontalscrollView04
* 类 名 称:TitleAdapter
* 类 描 述:导航菜单适配器
* 创 建 人:fy
* 创建时间:2014-3-23 下午3:27:59
* Copyright (c) 方勇-版权所有
*/
public class TitleAdapter extends BaseAdapter {
private Context context;
private List<String> titles;
public TitleAdapter(Context context, List<String> strings) {
this.context = context;
titles = strings;
}
@Override
public int getCount() {
return titles.size();
}
@Override
public Object getItem(int position) {
return titles.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(context).inflate(R.layout.category_title, null);
TextView title = (TextView) convertView.findViewById(R.id.category_title);
title.setText(titles.get(position).toString());
return convertView;
}
}
上一篇: php分页实例程序函数