ListView中的可选TextView(缺少动画)

时间:2012-06-22 13:03:12

标签: android android-layout android-listview textview

我使用自定义适配器构建了一个ListView,以便我有一个Checkbox(当时不可见)和一个TextView来显示一些文本。

现在,我希望在列表中选择项目时可以获得漂亮的动画(当您触摸元素时,背景为蓝色)。这可能吗?

我已经添加了OnLongTouchListener,但它没有动画!

获得一些帮助会很酷:)

布局-XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <CheckBox
        android:id="@+id/list_checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="false"
        android:visibility="gone"
        android:text="" 
        android:focusable="false"
        android:clickable="false">
    </CheckBox>

    <TextView
        android:id="@+id/list_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text=""
        android:clickable="true"
        android:longClickable="true" >
    </TextView>

</LinearLayout>

Java代码:

private class CommunityListAdapter extends ArrayAdapter<Community> {

    private List<Community> items;
    private Context context;

    public CommunityListAdapter(Context context, List<Community> communities) {
        super(context, R.layout.list_item, communities);
        this.items = communities;
        this.context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.list_item, null);
        }

        Community item = items.get(position);
        if(item != null) {
            TextView textView = (TextView) view.findViewById(R.id.list_text);
            if(textView != null) {
                textView.setText(item.getName());
                textView.setOnLongClickListener(new OnLongClickListener() {

                    public boolean onLongClick(View view) {
                        startActionMode(new MultiChoiseCABListener());
                        Toast.makeText(context, "TEST", 10).show();
                        return false;
                    }

                });
            }
        }

        return view;
    }
}

2 个答案:

答案 0 :(得分:0)

您尚未定义动画请定义动画并与此视图关联。

        Animation a = new AlphaAnimation(1.00f, 0.00f);
        a.setDuration(1000);
        YOURTEXTVIEW.startAnimation(a);

答案 1 :(得分:0)

在您的drawable中创建按下状态xml,您可以在其中设置不同的位图以在不同的tap状态下显示,并将该xml设置为主要布局背景。

示例状态xml。

list_item_bg_state.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item  android:state_pressed="true" android:drawable="@drawable/blue_bg" />
    <item android:drawable="@drawable/white_bg"/>
</selector>

其中blue_bg和white_bg是drawable中的两个.png图像。

注意上面的xml应该在你的drawables文件夹中。现在将此xml设置为列表项主布局的背景。等,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/list_item_bg_state"
    android:orientation="horizontal" >
</LinearLayout>

的问候, Aqif