温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android使用Gridview单行横向滚动显示

发布时间:2020-09-23 16:16:54 来源:脚本之家 阅读:171 作者:风吹起如花般细碎流年 栏目:移动开发

本文实例为大家分享了Android使用Gridview单行横向滚动显示的具体代码,供大家参考,具体内容如下

要想实现滚动显示,layout布局里必须要使用HorizontalScrollView,才能实现横向滑动,但HorizontalScrollView标签里要嵌套一个LinearLayout布局

activity_main.xml,如下:

<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"
 tools:context=".MainActivity"
 android:orientation="vertical"
 android:weightSum="2" >

 <HorizontalScrollView
  android:id="@+id/horizontal_scrollview"
  android:layout_height="0dp"
  android:layout_width="fill_parent"
  android:layout_weight="1"
  android:layout_gravity="center"
  android:background="@android:color/darker_gray"
  android:scrollbars="none">
   <LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="horizontal">
     <GridView
      android:id="@+id/test_gridview"
      android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_gravity="center"/>
   </LinearLayout>
 </HorizontalScrollView>
</LinearLayout>

gridview中的item的布局如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="80dp"
 android:layout_height="match_parent"
 android:orientation="vertical" >
 <ImageView 
  android:id="@+id/item_img"
  android:layout_width="60dp"
  android:layout_height="60dp"
  android:layout_gravity="center_horizontal"
  android:scaleType="fitXY"
  android:background="#00000000"/>
 <TextView 
  android:id="@+id/item_text"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:gravity="center_horizontal"
  android:textSize="20dp"
  android:text="233"
  android:textColor="@android:color/white"/>
</LinearLayout>

实现类如下:

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

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI