更新:我找到了解决方法 删除
v.setFocusable(true);
代码中的v.setClickable(true);
并且只添加
v.setEnabled(true);
并在我的xml(ListView)中添加
android:drawSelectorOnTop="true"
android:focusable="true"
当我点击它时没有任何反应,甚至列表视图也不会聚焦。
我试图添加所有这些:
v.setClickable(true);
v.setEnabled(true);
v.setFocusable(true);
只有这样才能添加以下代码:
但这并不能确定被点击的项目
How to handle ListView click in Android
,结果仍然相同。
以下是代码:
public class AppsInspectorActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//[...]
ListView app_listView = (ListView)findViewById(R.id.listview);
// I try setOnItemClickListene here - 1
app_listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(), "I Clicked on Row " + position + ".", Toast.LENGTH_SHORT).show();
}
});
}
适配器
public class AppAdapter extends BaseAdapter {
Context context;
ArrayList<AppInfo> dataList=new ArrayList<AppInfo>();
public AppAdapter(Context context,ArrayList<AppInfo> inputDataList)
{
this.context = context;
dataList.clear();
for(int i=0;i<inputDataList.size();i++)
{
dataList.add(inputDataList.get(i));
}
}
public int getCount() {
// TODO Auto-generated method stub
return dataList.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return dataList.get(position);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v=convertView;
final AppInfo appUnit = dataList.get(position);
ListView app_listView = (ListView)findViewById(R.id.listview);
/** Remove this , i just try to add to see setOnItemClickListener will work at here or not **/
app_listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(), "I Clicked on Row " + position + ".", Toast.LENGTH_SHORT).show();
}
});
if(v==null)
{
LayoutInflater vi=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v=vi.inflate(R.layout.app_row, null);
v.setClickable(true);
v.setEnabled(true);
v.setFocusable(true);
}
TextView appName=(TextView)v.findViewById(R.id.appName);
ImageView appIcon=(ImageView)v.findViewById(R.id.AppIcon);
if(appName!=null)
appName.setText(appUnit.appName);
if(appIcon!=null)
appIcon.setImageDrawable(appUnit.appIcon);
return v;
}
}
}
}
答案 0 :(得分:1)
您能否发布main.xml
布局。在我看来,您在列表视图的顶部有另一个视图正在消耗触摸事件。
答案 1 :(得分:1)
我通过删除
找到了解决方案 v.setFocusable(true); v.setClickable(true); in the code
并且只添加
v.setEnabled(true);
并在我的xml(ListView)中添加
android:drawSelectorOnTop="true" android:focusable="true"
答案 2 :(得分:0)
您好在适配器的getView中删除setOnItemClickListener。请参阅此listView
答案 3 :(得分:0)
使用AppsInspectorActivity.this
代替getApplicationContext()
public class AppsInspectorActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//[...]
ListView app_listView = (ListView)findViewById(R.id.listview);
// I try setOnItemClickListene here - 1
app_listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(AppsInspectorActivity.this, "I Clicked on Row " + position + ".", Toast.LENGTH_SHORT).show();
}
});
}
并从适配器中的setOnItemClickListener
方法中删除getView()
答案 4 :(得分:0)
将您的getView()
更改为低于1,并判断其是否有效
public View getView(int position, View convertView, ViewGroup parent) {
View v=convertView;
final AppInfo appUnit = dataList.get(position);
if(v==null)
{
LayoutInflater vi=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v=vi.inflate(R.layout.app_row, null);
v.setClickable(true);
v.setEnabled(true);
v.setFocusable(true);
}
TextView appName=(TextView)v.findViewById(R.id.appName);
ImageView appIcon=(ImageView)v.findViewById(R.id.AppIcon);
if(appName!=null)
appName.setText(appUnit.appName);
if(appIcon!=null)
appIcon.setImageDrawable(appUnit.appIcon);
return v;
}
答案 5 :(得分:0)
你必须在设置onItemClick之前将适配器设置为listview。这样你只需获取listview但没有items.so当你点击listview时,它的onItemClick永远不会被调用。