我得到了SD卡的文件列表,并将其显示在ListVIEw中,就像在自定义适配器的帮助下一样:
adapter = new ArrayAdapter<Item>(this, R.layout.file_manager, R.ID.checkedTextItem, fileList) { @OverrIDe public VIEw getVIEw(int position, VIEw convertVIEw, VIEwGroup parent) { // creates vIEw LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); VIEw vIEw = inflater.inflate(R.layout.check_item, null); CheckedTextVIEw textVIEw = (CheckedTextVIEw) vIEw .findVIEwByID(R.ID.checkedTextItem); // put the image on the text vIEw textVIEw.setCompoundDrawablesWithIntrinsicBounds( fileList[position].icon, 0, 0, 0); textVIEw.setTextcolor(color.WHITE); textVIEw.setText(fileList[position].file); if(fileList[position].icon == R.drawable.directory_icon) textVIEw.setcheckmarkDrawable(null); // add margin between image and text (support varIoUs screen // densitIEs) int dp5 = (int) (5 * getResources().getdisplayMetrics().density + 0.5f); textVIEw.setCompoundDrawablepadding(dp5); return vIEw; } };
我想实现setonitemclickListener或类似的东西来监听检测项目点击事件.我在Activity中的onCreate()方法:
public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.file_manager); lv = (ListVIEw)findVIEwByID(R.ID.fileManagerList); loadfileList(); file_List = findVIEwByID(R.ID.filesList); lv.setAdapter(adapter); lv.setonItemClickListener(new androID.Widget.AdapterVIEw.OnItemClickListener() { public voID onItemClick(AdapterVIEw<?> myAdapter, VIEw myVIEw, int myItemInt, long mylng) { //String selectedFromList = (lv.getItemAtposition(myItemInt).toString()); Toast toast = Toast.makeText(getApplicationContext(), "Hello World!", Toast.LENGTH_LONG); toast.show(); } });}
我的xml活动:
<?xml version="1.0" enCoding="UTF-8"?> <relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:ID="@+ID/fileManager" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:orIEntation="vertical" > <ListVIEw androID:background="#000000" androID:focusable="false" androID:ID="@+ID/fileManagerList" androID:layout_wIDth="fill_parent" androID:layout_above="@+ID/closecalmlayout" androID:layout_height="wrap_content" > </ListVIEw> <linearLayout androID:ID="@+ID/closecalmlayout" androID:layout_alignParentBottom="true" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:weightSum="1.0" > <button androID:ID="@+ID/btnOk" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" androID:layout_marginleft="5dip" androID:layout_margintop="5dip" androID:layout_weight=".50" androID:text="Attach files" /> <button androID:ID="@+ID/btnCancel" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" androID:layout_marginleft="5dip" androID:layout_marginRight="5dip" androID:layout_margintop="5dip" androID:layout_weight=".50" androID:text="Do not attach" /> </linearLayout></relativeLayout>
和我的CheckedTextVIEw活动
<?xml version="1.0" enCoding="UTF-8"?> <linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:orIEntation="horizontal" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" androID:padding="10dp"> <CheckedTextVIEw xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:ID="@+ID/checkedTextItem" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:gravity="center_vertical" androID:checkmark="?androID:attr/ListChoiceIndicatorMultiple" androID:textcolor="#000000" androID:focusable="false" androID:paddingleft="10dip" androID:paddingRight="6dip" androID:typeface="sans" androID:textSize="16dip"/> </linearLayout>
但是当我点击项目时,没有任何反应.我试图在onResume()方法中创建setonItemClickListener,但效果相同.我也试过onclickListener – 效果一样.这是什么原因?
解决方法:
在我看来,一些视图从列表视图中获得焦点,当您知道它是哪个视图时,在xml中的该视图上使用androID:focusable =“false”,它应该可以解决问题.
我尝试了你的代码并且正在调用onItemClicked,这是我的getVIEw():
@OverrIDe public VIEw getVIEw(int position, VIEw convertVIEw, VIEwGroup parent) { VIEw vIEw = convertVIEw; if(vIEw == null){ LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); vIEw = vi.inflate(R.layout.row, null); } CheckedTextVIEw textVIEw = (CheckedTextVIEw) vIEw.findVIEwByID(R.ID.checked); textVIEw.setText("Hello"); //test, you can do whatever you want with this int dp5 = (int) (5 * getResources().getdisplayMetrics().density + 0.5f); textVIEw.setCompoundDrawablepadding(dp5); return vIEw; }
我如何设置适配器(MyAdapter):
MyAdapter adapter = new MyAdapter(this, 0, arrayList);lv.setAdapter(adapter);lv.setonItemClickListener(new androID.Widget.AdapterVIEw.OnItemClickListener() { public voID onItemClick(AdapterVIEw<?> arg0, VIEw arg1, int arg2, long arg3) { // Todo auto-generated method stub Toast.makeText(PlayingAroundActivity.this, "Hello", Toast.LENGTH_LONG).show(); } });
总结 以上是内存溢出为你收集整理的android – ListView上的setOnItemClickListener不会触发全部内容,希望文章能够帮你解决android – ListView上的setOnItemClickListener不会触发所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)