无法将自定义listSelector应用于ListView

SO用户

我已经检查了几个SO答案,现在通过自定义选择器
在自定义listview ListView项目背景中不可见Android listselector

甚至遵循这一点(当前使用这种方法)
http://cyrilmottier.com/2011/08/08/listview-tips-tricks-3-create-fancy-listviews/

我什drawSelectorOnTop至将true

但是,我无法listSelector上班。基本上,我是机智的人。我想这与清单项目的制作方式有关吗?这是XML:

list_entry.xml

<?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="wrap_content"
    android:padding="5dp">

    <ImageButton android:id="@+id/contextMenuIcon"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_action_overflow"
        android:background="@null"/>

    <TextView android:id="@+id/noteTitle"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@id/contextMenuIcon"
        android:layout_alignBottom="@id/contextMenuIcon"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_alignParentTop="true"
        android:textIsSelectable="false"
        android:ellipsize="end"
        android:singleLine="true"
        android:textSize="20sp"
        android:textColor="@android:color/black"/>

    <TextView android:id="@+id/noteCreationDate"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        android:layout_below="@id/noteTitle" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:textColor="#808080"/>

    <TextView android:id="@+id/notePrivacy"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_below="@id/contextMenuIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:textColor="#808080" />

</RelativeLayout>    

list_selector_pressed.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <gradient android:endColor="#ffc579" android:startColor="#fb9d23" android:angle="90"></gradient>
</shape>  

list_selector_focussed.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">

    <gradient android:endColor="#f7ddb8" android:startColor="#f5c98c" android:angle="90"></gradient>

</shape>  

list_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" android:drawable="@drawable/list_selector_pressed" />
    <item android:state_focused="true" android:drawable="@drawable/list_selector_focused" />
    <item android:drawable="@android:color/transparent" />

</selector>

请告诉我如何使列表选择器起作用!

欣特

您正在列表项内使用ImageButton。因此,无论何时单击该项目,ImageButton都会获得焦点。

但是在您的情况下,您将ImageButton设置为背景

  android:background="@null"

它使您看不到焦点。如果删除此行,则可以看到问题。

要从列表项获得焦点,可以在xml的根视图中使用此代码。

android:descendantFocusability="blocksDescendants"   

<?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="wrap_content"
    android:descendantFocusability="blocksDescendants" 
    android:padding="5dp">

    <ImageButton android:id="@+id/contextMenuIcon"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_action_overflow"
        android:background="@null"/>

    <TextView android:id="@+id/noteTitle"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@id/contextMenuIcon"
        android:layout_alignBottom="@id/contextMenuIcon"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_alignParentTop="true"
        android:textIsSelectable="false"
        android:ellipsize="end"
        android:singleLine="true"
        android:textSize="20sp"
        android:textColor="@android:color/black"/>

    <TextView android:id="@+id/noteCreationDate"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        android:layout_below="@id/noteTitle" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:textColor="#808080"/>

    <TextView android:id="@+id/notePrivacy"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_below="@id/contextMenuIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:textColor="#808080" />

</RelativeLayout>  

我用您的xml创建了一个快速测试,它可以正常工作。

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);

            ListView list = (ListView)rootView.findViewById(R.id.list);

            ArrayList<MyItem> listItem = genListItem(20);
            MyAdapter adapter = new MyAdapter(MainActivity.this, android.R.layout.simple_list_item_1, listItem);
            list.setAdapter(adapter);


            return rootView;
        }

        private ArrayList<MyItem> genListItem(int size) {
            ArrayList<MyItem> listItem = new ArrayList<MainActivity.MyItem>();
            for (int i = 0; i < size; i++) {
                MyItem item = new MyItem();
                item.setName("item " + i);
                listItem.add(item);
            }

            return listItem;
        }
    }

    public static class MyItem{
        String Name;
        /**
         * @return the name
         */
        public String getName() {
            return Name;
        }
        /**
         * @param name the name to set
         */
        public void setName(String name) {
            Name = name;
        }


    }

    public class MyAdapter extends ArrayAdapter<MyItem>{

        private LayoutInflater inflator;
        private Context mContext;
        public MyAdapter(Context context, int resource, List<MyItem> objects) {
            super(context, resource, objects);
            mContext = context;
            inflator = ((Activity) context).getLayoutInflater();
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            final ViewHolder holder;
            if (convertView == null) {
                convertView = inflator.inflate(R.layout.list_entry, null);
                holder = new ViewHolder();
                holder.Name = (TextView) convertView.findViewById(R.id.noteTitle);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            MyItem item = getItem(position);
            holder.Name.setText(item.getName());


            return convertView;
        }

        class ViewHolder {
            public TextView Name;
        }


    }

}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将自定义主题应用于Odoo应用

来自分类Dev

将自定义样式应用于微调器

来自分类Dev

将自定义函数应用于数据框

来自分类Dev

将自定义功能应用于熊猫df

来自分类Dev

将自定义累积功能应用于熊猫

来自分类Dev

将自定义累积功能应用于熊猫

来自分类Dev

如何将自定义主题应用于Google自定义搜索?

来自分类Dev

如何将自定义字体应用于Android应用中的每个TextView

来自分类Dev

将自定义属性应用于“代码优先实体”框架对象

来自分类Dev

将自定义函数应用于具有通用名称的任何数据集

来自分类Dev

AngularJs有条件地将自定义指令应用于HTML

来自分类Dev

如何将自定义样式应用于SwitchCompat

来自分类Dev

如何将自定义字体应用于TTTAttributedLabel

来自分类Dev

将自定义样式应用于页面上的所有按钮

来自分类Dev

将自定义主题应用于PreferenceFragment中的ActionBar

来自分类Dev

将自定义累积函数应用于熊猫数据框

来自分类Dev

如何在PyMC中将自定义函数应用于变量?

来自分类Dev

如何在phoenix框架中将自定义验证规则应用于模型

来自分类Dev

如何将自定义材质设计主题应用于Bootstrap组件

来自分类Dev

熊猫groupby将自定义功能应用于每个组

来自分类Dev

如何将自定义函数应用于熊猫数据框的2列?

来自分类Dev

将自定义函数应用于pandas Series会产生AttributeError

来自分类Dev

如何仅将自定义属性应用于元素?

来自分类Dev

将自定义函数应用于r中的每一行

来自分类Dev

将自定义排序应用于打字稿中的对象数组

来自分类Dev

将自定义numba njit函数应用于熊猫滚动对象

来自分类Dev

如何使用bootstrap / css将自定义宽度应用于表格标题?

来自分类Dev

SwiftUI不将自定义字体应用于选项卡式视图

来自分类Dev

将自定义标记应用于pytest参数化标记的特定值

Related 相关文章

  1. 1

    将自定义主题应用于Odoo应用

  2. 2

    将自定义样式应用于微调器

  3. 3

    将自定义函数应用于数据框

  4. 4

    将自定义功能应用于熊猫df

  5. 5

    将自定义累积功能应用于熊猫

  6. 6

    将自定义累积功能应用于熊猫

  7. 7

    如何将自定义主题应用于Google自定义搜索?

  8. 8

    如何将自定义字体应用于Android应用中的每个TextView

  9. 9

    将自定义属性应用于“代码优先实体”框架对象

  10. 10

    将自定义函数应用于具有通用名称的任何数据集

  11. 11

    AngularJs有条件地将自定义指令应用于HTML

  12. 12

    如何将自定义样式应用于SwitchCompat

  13. 13

    如何将自定义字体应用于TTTAttributedLabel

  14. 14

    将自定义样式应用于页面上的所有按钮

  15. 15

    将自定义主题应用于PreferenceFragment中的ActionBar

  16. 16

    将自定义累积函数应用于熊猫数据框

  17. 17

    如何在PyMC中将自定义函数应用于变量?

  18. 18

    如何在phoenix框架中将自定义验证规则应用于模型

  19. 19

    如何将自定义材质设计主题应用于Bootstrap组件

  20. 20

    熊猫groupby将自定义功能应用于每个组

  21. 21

    如何将自定义函数应用于熊猫数据框的2列?

  22. 22

    将自定义函数应用于pandas Series会产生AttributeError

  23. 23

    如何仅将自定义属性应用于元素?

  24. 24

    将自定义函数应用于r中的每一行

  25. 25

    将自定义排序应用于打字稿中的对象数组

  26. 26

    将自定义numba njit函数应用于熊猫滚动对象

  27. 27

    如何使用bootstrap / css将自定义宽度应用于表格标题?

  28. 28

    SwiftUI不将自定义字体应用于选项卡式视图

  29. 29

    将自定义标记应用于pytest参数化标记的特定值

热门标签

归档