机器人:延长CheckedTextView源$ C ​​$ C [英] android: extending CheckedTextView source code

查看:166
本文介绍了机器人:延长CheckedTextView源$ C ​​$ C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在谁已经拥有了文本和一个复选框一个ListView添加一个图标的项目:simple_list_item_multiple_choice.xml这不过是一个< CheckedTextView>与某些属性标签

I was thinking of adding an icon to the items in a ListView who already has a text and a checkbox: simple_list_item_multiple_choice.xml which is nothing but a < CheckedTextView > tag with some attributes

我知道了自定义适配器解决方案,但我真的希望,更直观的解决方案。
我知道与源$ C ​​$ C打不直观,但我的意思是只是在做这个难易程度:

I'm aware of the custom adapter solution, but I really want and more intuitive solution. I know playing with the source code is not intuitive, but what I meant is the easiness of just doing this:

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, // context
                android.R.layout.simple_list_item_multiple_choice,  // view for an item in the list
                myCursor,                                           // cursor used for populating list items
                new String[] {dBHelper.CONTACT_NAME},               // column in cursor we are getting data from
                new int[] {android.R.id.text1});                    // where to put this data in the item's view

和结果可以像这样采取:

and the results can be taken with something like this:

SparseBooleanArray checkedPositions = lv.getCheckedItemPositions();

写在不同的文件中任何code

with whatever code written in separate files

使用源$ C ​​$ C: http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/2.3.3_r1/android/widget/CheckedTextView.java/

我的问题是日食不能解析: R.styleable.CheckedTextView 的和的 mPaddingRight

My problem is that eclipse cannot resolve: R.styleable.CheckedTextView and mPaddingRight in:

TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.CheckedTextView, defStyle, 0);

Drawable d = a.getDrawable(R.styleable.CheckedTextView_checkMark);

boolean checked = a.getBoolean(R.styleable.CheckedTextView_checked, false);

mPaddingRight = mCheckMarkWidth + mBasePaddingRight;
            d.setState(getDrawableState());
        } else {
            mPaddingRight = mBasePaddingRight;
        }

@Override
    public void setPadding(int left, int top, int right, int bottom) {
        super.setPadding(left, top, right, bottom);
        mBasePaddingRight = mPaddingRight;
    }

感谢您...:)

推荐答案

如上评论中提到,你不需要去源,如果你想要做的就是添加一个图标到ListView项目。只要创建所描述你想要的一个新的布局。我已经包括下面的例子,但是这是做的只是一种方式。

As mentioned in the comments above, you don't need to go to source if all you want to do is add an icon to the ListView items. Just create a new layout that described what you want. I've included an example below, but that's just one way of doing it.

<?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="?android:attr/listPreferredItemHeight"
    android:paddingLeft="6dp"
    android:paddingRight="6dp">

    <ImageView
        android:id="@+id/icon1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:src="@drawable/ic_launcher" />

    <CheckedTextView
        android:id="@android:id/text1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:gravity="center_vertical"
        android:checkMark="?android:attr/listChoiceIndicatorMultiple"
        android:layout_toRightOf="@id/icon1"
    />
</RelativeLayout>

那么,在你的活动code做这样的事情:

then, in your Activity code do something like:

ListView listView = (ListView) findViewById(R.id.listView1);
// I'm just going to use an ArraySdapter, for simplicity...

listView.setAdapter(new ArrayAdapter<String>(this, R.layout.item, android.R.id.text1, getResources().getStringArray(R.array.items)));

// or, for the sake of example (note, not optimized at all)

listView.setAdapter(new ArrayAdapter<String>(this, R.layout.item, android.R.id.text1, getResources().getStringArray(R.array.items)) {
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        if (position % 2 == 0) {
            ((ImageView) view.findViewById(R.id.icon1)).setImageResource(R.drawable.ic_launcher);
        } else {
            ((ImageView) view.findViewById(R.id.icon1)).setImageResource(R.drawable.ic_launcher);
        }
        return view;
    }
});

请注意,上面提供了更多的灵活性,但也可能只是增加了一个机器人:drawableLeft归因于CheckedTextView,而不是添加的RelativeLayout和ImageView的的

Note, the above gives more flexibility, but you could also have just added an android:drawableLeft attribute to the CheckedTextView instead of adding the RelativeLayout and ImageView.

这篇关于机器人:延长CheckedTextView源$ C ​​$ C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆