我需要点击列表项而不是单独的textview或图像视图。我的活动代码调用适配器如下:
@Override
public void onRequestTaskCompleted(String response) {
// TODO Auto-generated method stub
locList = new ArrayList<LocationData>();
locList = LocationData.getDataFromJSONArray(response);
list = (ListView) findViewById(R.id.locationlist_listview);
adapter = new LocationListAdapter(this, locList);
list.setAdapter(adapter);
}
适配器类如下:
public class LocationListAdapter extends BaseAdapter {
Context context;
ArrayList<LocationData> data;
LayoutInflater inflater;
ViewHolder holder;
private LayoutInflater mLayoutInflater;
public LocationListAdapter(Context context, ArrayList<LocationData> arrayList
) {
this.context = context;
data = arrayList;
// get the layout inflater
mLayoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int arg0) {
return 0;
}
private class ViewHolder {
TextView locName,locAddress;
ImageView locPic;
}
@Override
public View getView(final int position, View convertView, ViewGroup arg2) {
// TODO Auto-generated method stub
if (convertView == null) {
holder = new ViewHolder();
convertView = mLayoutInflater.inflate(
R.layout.locationlist_listitem, null);
holder.locName = (TextView) convertView
.findViewById(R.id.loclistitem_name);
holder.locAddress = (TextView) convertView
.findViewById(R.id.loclistitem_address);
holder.locPic = (ImageView) convertView
.findViewById(R.id.loclistitem_pic);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.locName.setText(data.get(position).getLocationName());
holder.locName.setId(data.get(position).getLocationId());
holder.locAddress.setText(data.get(position).getAddress1().concat(data.get(position).getAddress2()));
holder.locPic.setBackgroundResource(R.drawable.songs);
return convertView;
}
如果设置onclick任何textview它工作正常但我需要整个列表项可点击。
我从活动中调用我的适配器之前尝试了list.setOnItemClickListener
,但这并不起作用。以下是我试过的内容
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
LocationData lc = new LocationData();
lc = locList.get(position);
int locId = lc.getLocationId();
AppSession sInstance = AppSession.getInstance();
sInstance.setLocId(locId);
}
});
ListItem XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://splashurl.com/m22ydvb
android:id="@+id/itemdisplaylist_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="@dimen/loclist_item_minumum_height"
android:orientation="horizontal"
android:weightSum="1" >
<ImageView
android:id="@+id/loclistitem_pic"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.2"
android:contentDescription="@null" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.7"
android:orientation="vertical"
android:weightSum="1" >
<TextView
android:id="@+id/loclistitem_name"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.3"
android:textColor="@color/loclistitem_text"
android:textIsSelectable="true" >
</TextView>
<TextView
android:id="@+id/loclistitem_address"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:textIsSelectable="true" >
</TextView>
</LinearLayout>
更改了GetView方法:
@Override
public View getView(final int position, View convertView, ViewGroup arg2) {
// TODO Auto-generated method stub
if (convertView == null) {
holder = new ViewHolder();
convertView = mLayoutInflater.inflate(
R.layout.locationlist_listitem, null);
holder.locName = (TextView) convertView
.findViewById(R.id.loclistitem_name);
holder.locAddress = (TextView) convertView
.findViewById(R.id.loclistitem_address);
holder.locPic = (ImageView) convertView
.findViewById(R.id.loclistitem_pic);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.locName.setTag(position);
holder.locName.setText(data.get(position).getLocationName());
holder.locName.setId(data.get(position).getLocationId());
holder.locAddress.setText(data.get(position).getAddress1().concat(data.get(position).getAddress2()));
holder.locPic.setBackgroundResource(R.drawable.songs);
convertView.setOnClickListener(this);
return convertView;
}
@Override
public void onClick(View v) {
int position = (Integer)v.findViewById(R.id.loclistitem_name).getTag();
Log.d("list item clicked on :" , Integer.toString(position));
}
此外,我尝试实现从适配器到活动的回调,但这也不起作用。可能是我没有正确行事。
我已经阅读了很多类似的帖子并尝试了很多东西,但似乎错过了以正确的方式实现它的东西。
请告知。
答案 0 :(得分:0)
@Override
public View getView(final int position, View convertView, ViewGroup arg2) {
// TODO Auto-generated method stub
if (convertView == null) {
holder = new ViewHolder();
convertView = mLayoutInflater.inflate(
R.layout.locationlist_listitem, null);
holder.locName = (TextView) convertView
.findViewById(R.id.loclistitem_name);
holder.locAddress = (TextView) convertView
.findViewById(R.id.loclistitem_address);
holder.locPic = (ImageView) convertView
.findViewById(R.id.loclistitem_pic);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.locName.setText(data.get(position).getLocationName());
holder.locName.setId(data.get(position).getLocationId());
holder.locAddress.setText(data.get(position).getAddress1().concat(data.get(position).getAddress2()));
holder.locPic.setBackgroundResource(R.drawable.songs);
// apply on your getview like this for image view also
holder.locName.setOnclickListener(){
}
return convertView;
}
答案 1 :(得分:0)
您可以在convertView
方法中设置getView
的点击监听器。
使用以下代码:
convertView.setOnClickListener(this);
// you need implements OnClickListener on your Adapter class or you can use (new onClick.....) instead of `this`
获取点击项目的位置,您可以使用TAG。喜欢以下代码:
holder.locName.setTag(position); // paste this after else method in getView
然后在onClick
方法中:
@Override
public void onClick(View v) {
int position = (Integer)v.findViewById(R.id.loclistitem_name).getTag();
Log.d("list item clicked on :" , Integer.toString(position));
}
所以你的getView必须像下面的代码一样:
@Override
public View getView(final int position, View convertView, ViewGroup arg2) {
// TODO Auto-generated method stub
if (convertView == null) {
holder = new ViewHolder();
convertView = mLayoutInflater.inflate(
R.layout.locationlist_listitem, null);
holder.locName = (TextView) convertView
.findViewById(R.id.loclistitem_name);
holder.locAddress = (TextView) convertView
.findViewById(R.id.loclistitem_address);
holder.locPic = (ImageView) convertView
.findViewById(R.id.loclistitem_pic);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.locName.setTag(position);
holder.locName.setText(data.get(position).getLocationName());
holder.locName.setId(data.get(position).getLocationId());
holder.locAddress.setText(data.get(position).getAddress1().concat(data.get(position).getAddress2()));
holder.locPic.setBackgroundResource(R.drawable.songs);
convertView.setOnClickListener(this);
return convertView;
}
通过这种方式,您在Activity类中不需要list.setOnItemClickListener
。
另一种方式:
只要ListView元素中存在按钮或ImageButtons
等可点击元素,它们就会控制点击事件。因此,ListView
无法获得接受点击事件的机会。
<强>溶液强>
设置imageButton's
属性:
android:focusable="false"
或者您可以添加:
android:descendantFocusability="blocksDescendants"
到父版面
然后,您可以在活动类
上使用list.setOnItemClickListener
答案 2 :(得分:0)
android:focusable="false"
android:focusableInTouchMode="false"
并在充气机的父布局中添加:
android:descendantFocusability="blocksDescendants"
现在您的列表视图项目可聚焦,当您点击listitem时它就可以使用。