gpt4 book ai didi

Android ListView 项目点击不起作用

转载 作者:行者123 更新时间:2023-11-29 14:19:48 25 4
gpt4 key购买 nike

我看到过类似的问题,试过设置 focusable 为 false 等解决方案,但还是不行。这是我的包含 ListView 的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >


<ListView
android:id="@+id/listView_open_groups"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/button_add_group"
android:layout_alignParentTop="true"
></ListView>

<Button
android:id="@+id/button_add_group"
android:text="Add Group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:focusable="false"
/>

<Button
android:id="@+id/button_add_entry"
android:text="Add Entry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/button_add_group"
android:focusable="false"
/>


</RelativeLayout>

还有我的 custom_list_row

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:longClickable="true" >


<ImageView
android:id="@+id/imgView_group_entry_icon"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:focusable="false"
/>
<TextView
android:id="@+id/textView_group_entry_name"
android:layout_toRightOf="@id/imgView_group_entry_icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"/>
<TextView
android:id="@+id/textView_group_entry_type"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"/>


</RelativeLayout>

还有我的自定义适配器:

public class OpenDbListAdapter extends ArrayAdapter<KeepassDBGroupv1>{

Context context;

public OpenDbListAdapter(Context context, int resource,
List<KeepassDBGroupv1> objects) {
super(context, resource, objects);
this.context=context;
}

public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
KeepassDBGroupv1 rowItem = getItem(position);

LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_list_row, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.textView_group_entry_name);
holder.imageView = (ImageView) convertView.findViewById(R.id.imgView_group_entry_icon);
holder.type = (TextView) convertView.findViewById(R.id.textView_group_entry_type);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();

holder.name.setText(rowItem.getGroupName());
holder.type.setText("Group");
Drawable d = context.getResources().getDrawable(context.getResources().getIdentifier("ic"+Integer.toString(rowItem.getImageId()), "drawable", context.getPackageName()));
holder.imageView.setImageDrawable(d);
//App.getDB().drawFactory.assignDrawableTo(holder.imageView, context.getResources(), rowItem.icon);

return convertView;
}

private class ViewHolder {
ImageView imageView;
TextView name;
TextView type;
}

}

最后,这是我设置监听器的方式:

public class OpenDbActivity extends Activity{    
static ListView lv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_opendb);
db = MyApplication.keepassdb;
InitializeComponents();

}

private void InitializeComponents() {

lv = (ListView) findViewById(R.id.listView_open_groups);
OpenDbListAdapter adapter = new OpenDbListAdapter(this, R.layout.custom_list_row, childGroups);
lv.setAdapter(adapter);

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {

Log.d("Something");

}
});



}

谁能看出是什么问题?

谢谢

最佳答案

我也遇到过这个问题,我通过将点击监听器设置为 custom adapterconvertView 来克服这个问题。我不知道这是不是好方法,但它解决了我的问题。

public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
KeepassDBGroupv1 rowItem = getItem(position);

LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_list_row, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.textView_group_entry_name);
holder.imageView = (ImageView) convertView.findViewById(R.id.imgView_group_entry_icon);
holder.type = (TextView) convertView.findViewById(R.id.textView_group_entry_type);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();

holder.name.setText(rowItem.getGroupName());
holder.type.setText("Group");
Drawable d = context.getResources().getDrawable(context.getResources().getIdentifier("ic"+Integer.toString(rowItem.getImageId()), "drawable", context.getPackageName()));
holder.imageView.setImageDrawable(d);
//App.getDB().drawFactory.assignDrawableTo(holder.imageView, context.getResources(), rowItem.icon);
convertView.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Log.v("OpenDbListAdapter ","List View Clicked");
}
});
return convertView;
}

关于Android ListView 项目点击不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23602924/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com