Android uses Gridview single line horizontal scroll display

  • 2021-09-16 08:14:44
  • OfStack

This article example for everyone to share Android using Gridview single-line horizontal scrolling display of the specific code, for your reference, the specific content is as follows

To realize scrolling display, HorizontalScrollView must be used in layout layout to realize horizontal sliding, but one LinearLayout layout should be nested in HorizontalScrollView tag

activity_main. xml, as follows:


<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>

The layout of item in gridview is as follows:


<?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>

The implementation classes are as follows:


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;// Number of data to display 
  //gridview Adj. layout_widht, Than every one item The width of is excessive 2 Pixels, solve the problem of incomplete display item The question of 
  int allWidth = (int) (82 * size * density);
  //int allWidth = (int) ((width / 3 ) * size + (size-1)*3);// It can also be used in this way, item Total of width Plus horizontalspacing
  int itemWidth = (int) (80 * density);// Each item Width 
  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;
  }
 }

}

Code download address: AndroidGridviewScroll (ofstack. com). rar


Related articles: