Android使用Gridview单行横向滚动显示
作者:风吹起如花般细碎流年 时间:2021-07-28 17:40:43
本文实例为大家分享了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
来源:https://blog.csdn.net/gaoxiaomin123456/article/details/49616109
标签:Android,Gridview,滚动
0
投稿
猜你喜欢
SpringBoot 整合 Shiro 密码登录的实现代码
2023-11-10 11:27:59
举例解析Java的设计模式编程中里氏替换原则的意义
2021-06-30 18:37:17
Mybatis-Plus的SQL语句组拼原理说明
2021-07-03 20:39:22
Android RecyclerView使用ListAdapter高效刷新数据的操作方法
2023-06-24 22:22:09
Android之FanLayout制作圆弧滑动效果
2023-01-14 16:58:29
SpringBoot自定义MessageConverter与内容协商管理器contentNegotiationManager详解
2023-11-28 11:35:54
Android7.0开发实现Launcher3去掉应用抽屉的方法详解
2021-07-24 12:31:38
Spring Boot 自动配置的实现
2023-07-21 18:18:55
Android获取经纬度计算距离介绍
2022-04-30 20:00:04
Android自定义ViewGroup实现可滚动的横向布局(2)
2022-10-08 17:25:27
Android聊天工具基于socket实现
2021-12-06 04:40:01
Java并发线程池实例分析讲解
2022-08-05 20:25:40
Java Swing中JDialog实现用户登陆UI示例
2021-10-12 13:58:00
springmvc 防止表单重复提交的两种方法
2023-03-27 17:57:18
Java 六类运算符详解
2023-08-27 20:37:37
90分钟实现一门编程语言(极简解释器教程)
2022-01-15 05:11:56
C#使用smtp发送带附件的邮件实现方法
2022-11-03 17:53:39
Android 调用系统照相机拍照和录像
2023-10-30 05:40:35
C#实现伪装文件夹功能
2023-04-28 14:21:38
Springboot打包部署代码实例
2022-08-15 14:41:46