简体   繁体   English

Android:自定义ListView OnItemClickListener奇怪的问题

[英]Android: Custom ListView OnItemClickListener Weird Issues

I have a ListView that I'm populating with dummy strings right now. 我现在有一个用虚拟字符串填充的ListView。 It is currently holding 10 items, but the view itself is only large enough to show 5. So the list has to scroll of course. 它目前可容纳10个项目,但视图本身仅够显示5个。因此,列表当然必须滚动。 I've built the list using the following code: 我使用以下代码构建了列表:

ListView: 列表显示:

<RelativeLayout
    android:id="@+id/catList"
    android:layout_width="wrap_content"
    android:layout_height="216dip"
    android:layout_marginTop="120dip"
    >
    <ListView
        android:id="@+id/categoryList"
        android:layout_width="308dip"
        android:layout_height="216dip"
        android:layout_marginLeft="6dip"
        android:background="#ffffff"
        android:divider="#ffffff"
        android:choiceMode="singleChoice"
        android:cacheColorHint="#00000000"

        >
    </ListView>
    <ImageView
        android:id="@+id/catListFrame"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"    
        android:layout_alignParentTop="true"
        android:src="@drawable/selectorframe"
        />
</RelativeLayout>

So basically there is an image frame around the list. 因此,基本上列表周围有一个图像框。 This is all nested inside another layout. 这些全部嵌套在另一个布局中。

I have a custom item xml file as well that looks like this: 我也有一个自定义项目xml文件,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="?android:attr/listPreferredItemHeight"
   android:padding="10dip"
   android:background="#ffffff"
   >
   <TextView
       android:id="@+id/catTitle"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:textSize="20dip"
       android:textColor="#000000"
   >
   </TextView>
 </LinearLayout>

Here's the java that makes it all work: 这是使所有工作正常运行的Java:

private void setCategoryControls(){
    btnBackHome = (ImageView)findViewById(R.id.catHomeBtn);
    btnBackHome.setOnClickListener(goHome);
    catList = (ListView)findViewById(R.id.categoryList);
    catList.setOnItemClickListener(selectCat);
    demoCats = new ArrayList<String>();
    for(int i=1;i<=10;i++){
        demoCats.add("Item " + i);
    }
    m_catAdapter = new CatAdapter(this,R.layout.catitem,demoCats);
    catList.setAdapter(m_catAdapter);

}

private OnItemClickListener selectCat = new OnItemClickListener(){
    public void onItemClick(AdapterView<?> a, View v, int position, long id){
        if(selCatView!=null){
            selCatView.setBackgroundColor(Color.parseColor("#ffffff"));
        }
        v.setBackgroundColor(Color.parseColor("#ffaa00"));
        selCatView = v;
    }
};
private class CatAdapter extends ArrayAdapter<String>{

    private ArrayList<String> items;

    public CatAdapter(Context context, int textViewResourceId, ArrayList<String> items){
        super(context, textViewResourceId, items);
        this.items = items;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.catitem, null);
            }
            String cat = items.get(position);
            if (cat != null) {
                    TextView catName = (TextView) v.findViewById(R.id.catTitle);
                    if (catName != null) {
                          catName.setText(cat);
                    }

            }
            return v;
    }
}

What happens is when I click an item in the list it should simply change the background color of that item, instead it usually highlights 2 items (one that's visible and one that I have to scroll to), and most of the time it's not even the right item. 发生的事情是,当我单击列表中的某个项目时,它应该仅更改该项目的背景颜色,而通常会突出显示2个项目(一个可见,而我必须滚动至其中),并且大多数情况下甚至没有正确的项目。 Also if I scroll while an item is highlighted it will randomly change around which is highlighted as I scroll the highlighted item on and off the screen. 另外,如果在突出显示项目时滚动,当我在屏幕上和屏幕外滚动突出显示的项目时,它将围绕突出显示的内容随机变化。 If I had to guess, I'd say it's tied to the fact that the ListView apparantly only keeps track of the visible children. 如果我不得不猜测,我会说这与ListView显然只跟踪可见子项这一事实有关。 Even though it has an adapter with 10 items, if I run a getChildCount on the list view it only shows 5 or 6 depending on if one is partially visible or not, but it never shows more or less than the number of visible items. 即使它具有一个包含10个项目的适配器,但是如果我在列表视图上运行getChildCount,则它仅显示5或6,具体取决于一个项目是否部分可见,但它永远不会显示多于或少于可见项目的数量。

Indeed, ListView only keeps track of the currently displayed children. 实际上,ListView仅跟踪当前显示的子级。 This is why you recycle views in your getView method. 这就是为什么在getView方法中回收视图的原因。

The simplest way to do what you are trying to achieve is to give your views a state list drawable as a background. 做您要实现的目标的最简单方法是为视图提供一个可绘制的状态列表作为背景。 Make an XML state list drawable with a state_pressed and a default state (and optionally a state_focused) and put that as the background drawable of your catitem . 使一个带有state_pressed和默认状态(以及可选的state_focused)的XML状态列表可绘制,并将其作为catitem的背景可绘制catitem Of course you also need to remove the code that does it by hand =) 当然,您还需要手动删除执行此操作的代码=)

More documentation on state list drawables in here and here . 有关状态列表可绘制对象的更多文档,请参见此处此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM