【问题标题】:Custom RelativeLayout class in ListViewListView中的自定义RelativeLayout类
【发布时间】:2012-12-20 17:07:48
【问题描述】:

我在我的自定义 CursorAdapter 中关注这篇博文 InfamousViewHolder Pattern 在 newView 方法中,我创建了一个 Custom RelativeLayout 类,并在 bindView 中填充了数据。

问题在于这些方法从未被调用过。我正确设置了 setListAdapter,因为如果我实现 ViewHolder 模式,列表将正确填充。

实施。这里是代码:

public class SearchUserListViewRow extends RelativeLayout{
    public ImageView avatar = null;
    public TextView name = null;
    public ToggleButton inviteButton = null;
    public ToggleButton followButton = null;
    public FoundUser user;

    public SearchUserListViewRow(Context context) {
        super(context);
        LayoutInflater.from(context).inflate(R.layout.search_friend_row, this);
        avatar = (ImageView) findViewById(R.id.avatar);
        name = (TextView) findViewById(R.id.user_name);
        inviteButton = (ToggleButton) findViewById(R.id.invite_button);
        followButton = (ToggleButton) findViewById(R.id.follow_button);
    }


    public void init(Cursor cursor) {
        user = UsersProcessor.toFoundUser(cursor);

        name.setText(user.alias);
        inviteButton.setChecked(user.invited);
        inviteButton.setVisibility(Section.TO_INVITE.equals(user.section) ? 
                View.VISIBLE : View.GONE);
        inviteButton.setTag(user);

        followButton.setEnabled(!user.invited);
        followButton.setChecked(user.isGuru);
        followButton.setVisibility(Section.TO_FOLLOW.equals(user.section) ? 
                View.VISIBLE : View.GONE);
        followButton.setTag(user);
    }
}

和适配器:

public class UserCursorAdapter extends CursorAdapter {

    public UserCursorAdapter(Context context, Cursor c, int flags) {
        super(context, c, flags);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {

        View v = new SearchUserListViewRow(context);       
        return v;
    }


    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        SearchUserListViewRow row = (SearchUserListViewRow)v;
        //ViewHolder like stuff
        row.text = "foo";
    }
}

和 serarch_friend_row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/txt_box"
    android:orientation="horizontal"
    android:layout_marginTop="-3px"
    android:padding="@dimen/padding_row" >

    <ImageView
        android:id="@+id/avatar"
        android:layout_width="@dimen/image_user_avatar"
        android:layout_height="@dimen/image_user_avatar"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true" />

    <FrameLayout
        android:id="@+id/button_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true">

        <ToggleButton
            android:id="@+id/follow_button"
            android:visibility="gone" />

        <ToggleButton
            android:id="@+id/invite_button"0000000000000000000
            android:visibility="gone" />

    </FrameLayout>

    <TextView
        android:id="@+id/user_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="@dimen/content_padding_normal"
        android:layout_toLeftOf="@id/button_container"
        android:layout_toRightOf="@id/avatar"
        android:ellipsize="marquee"
        android:singleLine="true"
        android:textSize="@dimen/text_size_medium"
        android:textStyle="bold" />

</RelativeLayout>

提前致谢。

【问题讨论】:

  • 问题在于这些方法从未被调用过 - 哪些方法? newView 和/或bindView?如果是,请仔细检查您的 Cursor 并确保其中包含行。
  • 是的,如果我用它显示的列表和调用 newView/bindView 的视图实现相同的行,它就会有行
  • 我不太明白你面临什么问题。尝试更好地解释有问题的行为。
  • 问题在于光标打开并正确填充,光标不是问题,ListView 不显示任何行,并且方法 newView/BindView 从未使用提供的源代码调用,我不知道为什么
  • ListView 不显示任何行,并且 newView/BindView 方法从未使用提供的源代码调用,我不知道为什么 - 你确定你使用代码中的正确适配器实例(如果使用Loader 确保您在正确的适配器上设置数据)?此外,在您的SearchUserListViewRow 中,您不应该调用init 方法或更新其中一个视图吗? row.text 不会更新使用该字符串的视图。

标签: android android-listview android-relativelayout android-custom-view android-cursoradapter


【解决方案1】:

newViewbindView 在底层游标为空的情况下可能不会被调用。 先检查一下。

第二点,您的代码不代表 ViewHolder 模式,因为它的主要思想是将项目视图的引用存储在视图标记中。您应该创建/膨胀一个新项目,并在调用findViewById() 之后将返回值放入持有者实例。然后将持有者实例保存到标签中。

最后,当您将光标的值绑定到视图中时,从标签获取持有者并填充,例如setText()setImage() 等等。

【讨论】:

  • 是的,我的光标有行。如果我使用 ViewHolder 模式实现相同的 CursorAdapter,则显示它的列表并调用 newView/bindView。我想知道为什么我的代码没有显示列表并且 ViewHolder 是的。
猜你喜欢
  • 2010-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-27
  • 1970-01-01
  • 1970-01-01
  • 2013-06-18
  • 2011-12-18
相关资源
最近更新 更多