橫向Gridview使用
1.主介面佈局程式碼:activity_main.xml
a.包裹HorizontalScrollView控制元件是GirdView橫向滾動的基本條件
b.GirdView外包裹LinearLayout是java程式碼中引數設定的必要條件
`<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_margin="10dp" >
<GridView
android:id="@+id/grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="spacingWidthUniform" >
</GridView>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>`
2.主介面GridView列表子項佈局檔案:list_item.xml
<!--?xml version="1.0" encoding="utf-8"?-->
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/itemlayout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical">
<imageview android:id="@+id/ItemImage" android:layout_width="80.0dip" android:layout_height="80.0dip" android:layout_gravity="center_horizontal" android:src="@drawable/china">
</imageview>
<textview android:id="@+id/tvCity" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="Title" android:textcolor="#000000">
</textview>
<textview android:id="@+id/tvCode" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="Info" android:textcolor="#000000">
</textview>
</linearlayout>
3.java實現程式碼:MainActivity.java
package com.example.horizontallistview;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
/***
*
*/
public class MainActivity extends Activity {
List<CityItem> cityList;
RelativeLayout itmel;
GridView gridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LayoutInflater layoutInflater = (LayoutInflater) this
.getSystemService("layout_inflater");
gridView = (GridView) findViewById(R.id.grid);
setData();
setGridView();
}
/**設定資料*/
private void setData() {
cityList = new ArrayList<CityItem>();
CityItem item = new CityItem();
item.setCityName("深圳");
item.setCityCode("0755");
cityList.add(item);
item = new CityItem();
item.setCityName("上海");
item.setCityCode("021");
cityList.add(item);
item = new CityItem();
item.setCityName("廣州");
item.setCityCode("020");
cityList.add(item);
item = new CityItem();
item.setCityName("北京");
item.setCityCode("010");
cityList.add(item);
item = new CityItem();
item.setCityName("武漢");
item.setCityCode("027");
cityList.add(item);
item = new CityItem();
item.setCityName("孝感");
item.setCityCode("0712");
cityList.add(item);
cityList.addAll(cityList);
}
/**設定GirdView引數,繫結資料*/
private void setGridView() {
int size = cityList.size();
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);
gridView.setLayoutParams(params); // 設定GirdView佈局引數,橫向佈局的關鍵
gridView.setColumnWidth(itemWidth); // 設定列表項寬
gridView.setHorizontalSpacing(5); // 設定列表項水平間距
gridView.setStretchMode(GridView.NO_STRETCH);
gridView.setNumColumns(size); // 設定列數量=列表集合數
GridViewAdapter adapter = new GridViewAdapter(getApplicationContext(),
cityList);
gridView.setAdapter(adapter);
}
/**GirdView 資料介面卡*/
public class GridViewAdapter extends BaseAdapter {
Context context;
List<CityItem> list;
public GridViewAdapter(Context _context, List<CityItem> _list) {
this.list = _list;
this.context = _context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
convertView = layoutInflater.inflate(R.layout.list_item, null);
TextView tvCity = (TextView) convertView.findViewById(R.id.tvCity);
TextView tvCode = (TextView) convertView.findViewById(R.id.tvCode);
CityItem city = list.get(position);
tvCity.setText(city.getCityName());
tvCode.setText(city.getCityCode());
return convertView;
}
}
public class CityItem {
private String cityName;
private String cityCode;
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public String getCityCode() {
return cityCode;
}
public void setCityCode(String cityCode) {
this.cityCode = cityCode;
}
}
}
相關文章
- Android 實現GridView的橫向滾動,實現仿京東秒殺效果AndroidView
- 橫向無檔案移動--SCshell使用
- GridView正反雙向排序View排序
- excel怎麼橫向自動求和 excel表格橫向求和公式Excel公式
- 縱向控制的橫向滾動
- HTML橫向導航欄HTML
- 資料夾橫向排版
- EXCEL橫向變縱向(不需要VBA)Excel
- bootstrap table 橫向滾動條boot
- 9.5內網橫向&代理&隧道內網
- 使用rediobutton實現多個標題橫向划動
- oracle縱向顯示變為橫向顯示Oracle
- 矩陣:橫向關係和縱向關係矩陣
- Win10如何將音量條從橫向變為豎向 win10音量條怎麼從橫向變為豎向Win10
- iOS 可以縱向橫向滑動的表格實現iOS
- BIRT 如何處理橫向分欄
- Servlet與Netty橫向對比ServletNetty
- iOS UICollectionView 橫向分頁佈局iOSUIView
- jQuery內容橫向拖拽滾動jQuery
- webuploader橫向按鈕樣式Web
- 使用GridView管理類View
- excel文字橫向變縱向快捷鍵 excel文字方向改為縱向Excel
- python實現橫向拼接圖片Python
- Excel怎麼橫向輸入資料?Excel
- EXCEL中如何橫向輸入資料?Excel
- 程式設計師的“橫向發展”程式設計師
- 如何用CSS製作橫向選單?CSS
- js使用waterfall橫向載入瀑布流,一行程式碼完成JS行程
- CSS3橫向導航選單效果CSSS3
- 內網滲透(八)橫向移不動內網
- JavaScript橫向二級導航選單效果JavaScript
- 兩種方式實現橫向滾動條
- HTML中的標籤中li橫向排列HTML
- iOS Swift UICollectionView橫向分頁的問題iOSSwiftUIView
- GridView 中 DataFormatString 的使用ViewORM
- Windows10系統音量調節橫向怎麼改為豎向Windows
- 程式語言對比手冊(橫向版)[-PHP-]PHP
- 內網滲透-橫向移動($IPC&at&schtasks)內網