问题
I try to create listView dynamically from code with:
@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>
I want to make item in listview, in my example that is relativelayout, as clickable. I trying with:
<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">
and/or:
v.setClickable(true);
v.setEnabled(true);
v.setFocusable(true);
But this is not working.
Where I made mistake?
回答1:
you can add your click listener to your v
view returned within the getView() method
v.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
}....
this should work - works for me. the methods called within should be moved elsewhere. not sure if you want to implement the View.OnClickListener in the Adapter but it's up to you i guess.
Cheers
来源:https://stackoverflow.com/questions/8886819/dynamically-relativelayout-and-clickable