我尝试使用以下代码动态创建 listView:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder;
if (v == null) {
LayoutInflater vi =
(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.grid_item, null);
holder = new ViewHolder();
holder.one = (TextView) v.findViewById(R.id.one );
holder.two= (TextView) v.findViewById(R.id.two);
holder.three= (TextView) v.findViewById(R.id.three);
holder.four= (TextView) v.findViewById(R.id.four);
holder.five= (TextView) v.findViewById(R.id.five);
v.setTag(holder);
}
else
holder=(ViewHolder)v.getTag();
final Custom custom = entries.get(position);
if (custom != null) {
holder.one .setText(custom.getOne());
holder.two.setText(custom.getTwo());
holder.three.setText(custom.getThree());
holder.four.setText(custom.getFour());
holder.five.setText(custom.getFive());
}
return v;
}
grid_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="0dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" >
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_marginRight="4dp" />
<View android:id="@+id/separator"
android:background="#cccccc"
android:layout_width="1dip"
android:layout_height="45dip"
android:layout_marginRight="4dp"/>
</LinearLayout>
<TextView
android:id="@+id/one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/linearLayout1"
android:layout_toLeftOf="@+id/linearLayout2"
android:text=""
android:textSize="25dp"/>
<TextView
android:id="@+id/two"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/alarmTitle"
android:layout_toRightOf="@+id/linearLayout1"
android:layout_toLeftOf="@+id/linearLayout2"
android:text=""
android:textSize="16dp" />
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="3dp"
android:orientation="vertical" >
<TextView
android:id="@+id/three"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textSize="12dp" />
<TextView
android:id="@+id/four"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textSize="12dp" />
<TextView
android:id="@+id/five"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textSize="12dp" />
<TextView
android:id="@+id/six"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textSize="12dp" />
</LinearLayout>
</RelativeLayout>
我想在列表视图中制作项目,在我的示例中是相对布局,可点击。我尝试:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true">
和/或:
v.setClickable(true);
v.setEnabled(true);
v.setFocusable(true);
但这不起作用。
我在哪里犯错了?