热门标签 | HotTags
当前位置:  开发笔记 > Android > 正文

AndroidGridView实现横向列表水平滚动

这篇文章主要为大家详细介绍了AndroidGridView实现横向列表水平滚动,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Android GridView实现横向列表水平滚动的具体代码,供大家参考,具体内容如下

有时候根据项目需要,使用可横向滑动的GridView。仅以该文记录一下,毕竟没什么技术含量。

1.主界面布局代码:activity_main.xml。设置android:numColumns=”auto_fit”是因为可以不定项的添加子项。

<&#63;xml version="1.0" encoding="utf-8"&#63;>


 

  

   
  
 

2.主界面GridView列表子项布局文件:home_gridview_item.xml

<&#63;xml version="1.0" encoding="utf-8"&#63;>


 

 

3.java实现代码:MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.widget.GridView;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {
 AblGridView id_gridview_home;
 private int[] imgs = {R.mipmap.index1_nav_xc, R.mipmap.index1_nav_xc,
   R.mipmap.index1_nav_xc, R.mipmap.index1_nav_xc, R.mipmap.index1_nav_xc};
 private String[] titles = new String[]{"子项1", "子项2","子项3","子项4", "子项5"};
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  id_gridview_home= (AblGridView) findViewById(R.id.id_gridview_home);
  setGridView();
 }

 /**设置GirdView参数,绑定数据*/
 private void setGridView() {
  int size = imgs.length;
  int length = 100;
  DisplayMetrics dm = new DisplayMetrics();
  getWindowManager().getDefaultDisplay().getMetrics(dm);
  float density = dm.density;
  int gridviewWidth = (int) (size * (length + 4) * density);
  int itemWidth = (int) (length * density);
  LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
    gridviewWidth, LinearLayout.LayoutParams.FILL_PARENT);
  id_gridview_home.setLayoutParams(params); // 设置GirdView布局参数,横向布局的关键
  id_gridview_home.setColumnWidth(itemWidth); // 设置列表项宽
  id_gridview_home.setHorizontalSpacing(5); // 设置列表项水平间距
  id_gridview_home.setStretchMode(GridView.NO_STRETCH);
  id_gridview_home.setNumColumns(size); // 设置列数量=列表集合数
  AblGridViewBaseAdapter adapter = new AblGridViewBaseAdapter(this,imgs,titles);
  id_gridview_home.setAdapter(adapter);
 }
}

4.自定义控件:AblGridView

import android.content.Context;
import android.util.AttributeSet;
import android.widget.GridView;

/**
 * 继承GridView控件,为了更友好的显示
 * 解决在scrollview中只显示第一行数据的问题
 */
public class AblGridView extends GridView {
 public AblGridView(Context context, AttributeSet attrs) {
  super(context, attrs);
 }

 public AblGridView(Context context) {
  super(context);
 }

 public AblGridView(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
 }

 @Override
 public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
    MeasureSpec.AT_MOST);
  super.onMeasure(widthMeasureSpec, expandSpec);
 }

}

5.重写适配器:AblGridViewBaseAdapter

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 重写适配器
 */
public class AblGridViewBaseAdapter extends BaseAdapter {

 private Context mContext;
 private String[] Title;//显示标题数组
 private int[] Imgs;//显示图标数组

 public AblGridViewBaseAdapter(Context mContext,int[] imgs ,String[] titles) {
  super();
  this.mCOntext= mContext;
  Title = titles;
  Imgs = imgs;
 }

 @Override
 public int getCount() {
  return Title.length;
 }

 @Override
 public Object getItem(int position) {
  return position;
 }

 @Override
 public long getItemId(int position) {
  return position;
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  cOnvertView= loadHomeHyOrYs(position, convertView, parent);
  return convertView;
 }

 /**
  * 加载会员或医生的功能列表
  */
 public View loadHomeHyOrYs(int position, View convertView, ViewGroup parent) {
  if (cOnvertView== null) {
   cOnvertView= LayoutInflater.from(mContext).inflate(
     R.layout.home_gridview_item, parent, false);
  }
  TextView tv = get(convertView, R.id.id_tv_item);
  ImageView iv = get(convertView, R.id.id_iv_item);
  iv.setBackgroundResource(Imgs[position]);
  tv.setText(Title[position]);
  return convertView;
 }

 public  T get(View view, int id) {
  SparseArray viewHolder = (SparseArray) view.getTag();
  if (viewHolder == null) {
   viewHolder = new SparseArray();
   view.setTag(viewHolder);
  }
  View childView = viewHolder.get(id);
  if (childView == null) {
   childView = view.findViewById(id);
   viewHolder.put(id, childView);
  }
  return (T) childView;
 }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


推荐阅读
author-avatar
手机用户2602886175
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有