我知道如何将gridview中的每个单元格变成一个按钮,但这不是我所追求的。 我有一个用这个适配器填充的gridview。
public class TaskAdapter extends BaseAdapter{
private Context mContext;
public TaskAdapter(Context c)
{
mContext = c;
}
@Override
public int getCount() {
return nThumbsIds.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View sView = convertView;
if(convertView == null){
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
sView = inflater.inflate(R.layout.task_square, parent, false);
TextView tView = (TextView) sView.findViewById(R.id.textView);
tView.setText(nThumbsIds.get(position));
}
return sView;
}
}
nThumbsIds充满了Strings,其内容在几乎每个版本都会有所不同。
我想添加一个按钮作为网格的最后一个元素,总是在所有字符串之后。有办法吗?
答案 0 :(得分:0)
试试这样:
创建适配器
public class TaskAdapter extends BaseAdapter
{
private Context mContext;
public TaskAdapter(Context c)
{
mContext = c;
}
@Override
public int getCount()
{
return nThumbsIds.size();
}
@Override
public Object getItem(int position)
{
// TODO Auto-generated method stub
return nThumbsIds.get(position);
}
@Override
public long getItemId(int position)
{
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
TextView tView =null;
Button tBtn =null;
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(R.layout.task_square, parent, false);
tView = (TextView) convertView.findViewById(R.id.grid_item_txv);
tBtn = (Button) convertView.findViewById(R.id.grid_item_btn);
if(position < nThumbsIds.size()-1)
{
tView.setText(nThumbsIds.get(position));
tView.setVisibility(View.VISIBLE);
tBtn.setVisibility(View.GONE);
}
else
{
tBtn.setText(nThumbsIds.get(position));
tView.setVisibility(View.GONE);
tBtn.setVisibility(View.VISIBLE);
}
return convertView;
}
你的网格项目布局xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/grid_item_txv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:layout_centerInParent="true"
android:gravity="center"
android:text="Smaple"/>
<Button
android:id="@+id/grid_item_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:gravity="center"
android:layout_centerInParent="true"
android:text="sample"
android:visibility="gone" />
</RelativeLayout>
GridView在您的活动布局中:
<GridView
android:id="@+id/test_grid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnWidth="100dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" >
</GridView>