歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android中GridView通過自定義適配器(未優化)實現圖文視圖排列

Android中GridView通過自定義適配器(未優化)實現圖文視圖排列

日期:2017/3/1 9:07:40   编辑:Linux編程

Android中GridView組件用來以網格方式排列視圖,與矩陣類似,當屏幕上有很多元素(文字、圖片或其他元素)需要顯示時,可以使用該組件。下面我們通過代碼實現如下圖例(為了方便截圖,將事件處理(土司)設置到屏幕頂部)

Layout下gridview.xml布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:id="@+id/gv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp">
</GridView>
</LinearLayout>

GridView屬性設置解析:

anroid:numColumns="3"GridView的列數設置為3

android:columnWidth="90dp",每列的寬度,也就是Item的寬度

android:stretMode="columnWidth" 縮放與列寬大小同步

android:verticalSpacing="10dp" 兩行之間的邊距

android:horizontalSpacing="10dp" 兩列之間的邊距

Layout下item_layout.xml布局文件(即為網格內每個單元格的布局):

<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"(此處設置gravity為center後便不需要在他處重復設置)
android:orientation="vertical">
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="功能" (測試使用此處文本框內的文本設置可不添加)/>
</LinearLayout>

java代碼實現功能(核心為自定義適配器):

public class GridViewDemo extends AppCompatActivity {
private GridView gv;//聲明GridView視圖
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gridview);
gv = (GridView) findViewById(R.id.gv);
gv.setAdapter(new MyAdapter(this));//通過設置適配器實現網格內布局
//為每個單元格(item)添加單擊事件
gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView tv = (TextView) view.findViewById(R.id.tv);
Toast t =Toast.makeText(GridViewDemo.this,tv.getText().toString(),Toast.LENGTH_SHORT);
t.setGravity(Gravity.TOP,0,0);
t.show();
}
});
}
//自定義適配器(通過繼承BaseAdapter)
class MyAdapter extends BaseAdapter {
Context context;//聲明適配器中引用的上下文
//將需要引用的圖片和文字分別封裝成數組
int[] images = {R.mipmap.ic_launcher, R.mipmap.
ic_launcher, R.mipmap.ic_launcher, R.mipmap.
ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,};
String[] names = {"功能1", "功能2", "功能3", "功能4", "功能5", "功能6"};
//通過構造方法初始化上下文
public MyAdapter(Context context) {
this.context = context;
}
@Override
public int getCount() {
return names.length;//images也可以
}
@Override
public Object getItem(int position) {
return names[position];//images也可以
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//通過布局填充器LayoutInflater填充網格單元格內的布局
View v = LayoutInflater.from(context).inflate(R.layout.item_layout, null);
//使用findViewById分別找到單元格內布局的圖片以及文字
ImageView iv = (ImageView) v.findViewById(R.id.iv);
TextView tv = (TextView) v.findViewById(R.id.tv);
//引用數組內元素設置布局內圖片以及文字的內容
iv.setImageResource(images[position]);
tv.setText(names[position]);
//返回值一定為單元格整體布局v
return v;
}
}
}

至此代碼全部完成,為了實現功能本次未對圖片以及文本樣式做精細化處理,感興趣的朋友們可以自己試著敲著玩玩。

Copyright © Linux教程網 All Rights Reserved