小編給大家分享一下Android開發(fā)如何實(shí)現(xiàn)橫向列表GridView橫向滾動(dòng),希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
創(chuàng)新互聯(lián)成立與2013年,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元中江做網(wǎng)站,已為上家服務(wù),為中江各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18980820575
具體如下:
Android 橫向列表實(shí)現(xiàn),可左右滑動(dòng),如下圖
1. 主界面布局代碼:activity_main.xml
a.包裹HorizontalScrollView控件是GirdView橫向滾動(dòng)的基本條件
b.GirdView外包裹LinearLayout是java代碼中參數(shù)設(shè)置的必要條件
2.主界面GridView列表子項(xiàng)布局文件:list_item.xml
3.java實(shí)現(xiàn)代碼: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; /*** * * @author ymw */ public class MainActivity extends Activity { ListcityList; 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(); } /**設(shè)置數(shù)據(jù)*/ private void setData() { cityList = new ArrayList (); 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); } /**設(shè)置GirdView參數(shù),綁定數(shù)據(jù)*/ 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); // 設(shè)置GirdView布局參數(shù),橫向布局的關(guān)鍵 gridView.setColumnWidth(itemWidth); // 設(shè)置列表項(xiàng)寬 gridView.setHorizontalSpacing(5); // 設(shè)置列表項(xiàng)水平間距 gridView.setStretchMode(GridView.NO_STRETCH); gridView.setNumColumns(size); // 設(shè)置列數(shù)量=列表集合數(shù) GridViewAdapter adapter = new GridViewAdapter(getApplicationContext(), cityList); gridView.setAdapter(adapter); } /**GirdView 數(shù)據(jù)適配器*/ public class GridViewAdapter extends BaseAdapter { Context context; List list; public GridViewAdapter(Context _context, List _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; } } }
看完了這篇文章,相信你對(duì)“Android開發(fā)如何實(shí)現(xiàn)橫向列表GridView橫向滾動(dòng)”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!