android: extending CheckedTextView source code

烈酒焚心 提交于 2019-12-25 01:16:34

问题


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

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();

with whatever code written in separate files


Using the source code from: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.3.3_r1/android/widget/CheckedTextView.java/

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

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

and

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

and

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

AND

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

and

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

Thank you... :)


回答1:


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>

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;
    }
});

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.



来源:https://stackoverflow.com/questions/11712978/android-extending-checkedtextview-source-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!