十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
本文实例为大家分享了Android使用Gridview单行横向滚动显示的具体代码,供大家参考,具体内容如下
创新互联主要从事成都网站设计、成都做网站、网页设计、企业做网站、公司建网站等业务。立足成都服务正镶白,10余年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:13518219792
要想实现滚动显示,layout布局里必须要使用HorizontalScrollView,才能实现横向滑动,但HorizontalScrollView标签里要嵌套一个LinearLayout布局
activity_main.xml,如下:
gridview中的item的布局如下:
<?xml version="1.0" encoding="utf-8"?>
实现类如下:
package com.example.scrollgridview; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.util.DisplayMetrics; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; public class MainActivity extends Activity { private GridView gridview; private int imgs[]={R.drawable.remote_tv_0, R.drawable.remote_tv_1,R.drawable.remote_tv_2, R.drawable.remote_tv_3,R.drawable.remote_tv_4, R.drawable.remote_tv_5,R.drawable.remote_tv_6, R.drawable.remote_tv_7,R.drawable.remote_tv_8, R.drawable.remote_tv_9}; private GridviewAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gridview = (GridView)findViewById(R.id.test_gridview); adapter = new GridviewAdapter(); DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); float density = dm.density; int size = imgs.length;//要显示数据的个数 //gridview的layout_widht,要比每个item的宽度多出2个像素,解决不能完全显示item的问题 int allWidth = (int) (82 * size * density); //int allWidth = (int) ((width / 3 ) * size + (size-1)*3);//也可以这样使用,item的总的width加上horizontalspacing int itemWidth = (int) (80 * density);//每个item宽度 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(allWidth,LinearLayout.LayoutParams.MATCH_PARENT); gridview.setLayoutParams(params); gridview.setColumnWidth(itemWidth); gridview.setHorizontalSpacing(3); gridview.setStretchMode(GridView.NO_STRETCH); gridview.setNumColumns(size); gridview.setAdapter(adapter); adapter.setindex(0); adapter.notifyDataSetChanged(); gridview.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub adapter.setindex(position); adapter.notifyDataSetChanged(); } }); } class GridviewAdapter extends BaseAdapter{ private int index = 0; @Override public int getCount() { // TODO Auto-generated method stub return imgs.length; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return imgs[position]; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } public void setindex(int index){ this.index = index; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); ViewHolder viewHolder; if (convertView == null) { convertView = mInflater.inflate(R.layout.gridview_itme, null); viewHolder = new ViewHolder(); viewHolder.img = (ImageView)convertView.findViewById(R.id.item_img); viewHolder.text = (TextView)convertView.findViewById(R.id.item_text); convertView.setTag(viewHolder); }else{ viewHolder = (ViewHolder)convertView.getTag(); } if(this.index == position){ convertView.setBackgroundResource(R.drawable.list_item_bg_focus); } else{ convertView.setBackgroundResource(R.drawable.list_item_bg); } viewHolder.img.setImageResource(imgs[position]); viewHolder.text.setText(position+""); return convertView; } class ViewHolder{ ImageView img; TextView text; } } }
代码下载地址:AndroidGridviewScroll(jb51.net).rar
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。