滚动时,ListView项目在彼此之上绘制(Android)

时间:2014-07-01 01:20:51

标签: android android-layout android-listview parse-platform

在片段中,我有一个具有自定义ParseQueryAdapter<T>的ListView。问题可能与Parse没有任何关系,虽然我不确定。

当我测试我的应用时,我注意到一些非常奇怪的事情。当我向下滚动ListView时,所有可见的ListView项目都将绘制在下一个ListView项目的顶部,如下面第二张图所示。

列表正确初始化:

Initialized correctly

如您所见,在我的列表项布局中,我有一个ImageView(ParseImageView是特定的)和TextView。 TextView现在显示一些注释(不介意ID user_name_text_view),ImageView显示占位符空白配置文件图片。

当我向下滚动时,列表看起来像:

Stacked View here

这是名为fragment_post_view_list_view列表视图布局

<?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="vertical" >

    <ListView
        android:id="@+id/post_list_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

这是名为list_item_post_view列表项目布局

<?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">

    <com.parse.ParseImageView
        android:id="@+id/icon"
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:layout_alignParentLeft="true"
        android:background="@drawable/com_facebook_profile_picture_blank_square" />

    <TextView
        android:id="@+id/user_name_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/icon"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@color/link_blue" />

</RelativeLayout>

这是名为PostViewListViewAdapter适配器

public class PostViewListViewAdapter extends ParseQueryAdapter<Post> {
    // call superclass with a query to populate list view
    public PostViewListViewAdapter(Context context, final String[] postsObjectIds) {
        super(context, new ParseQueryAdapter.QueryFactory<Post>(){
            public ParseQuery<Post> create() {
                ParseQuery<Post> query = Post.getQuery();
                query.whereContainedIn("objectId", Arrays.asList(postsObjectIds));
                return query;
            }
        });
    }

    // this is similar to getView method in an adapter
    @Override
    public View getItemView(Post post, View v, ViewGroup parent) {
        if(v == null) {
            v = View.inflate(getContext(), R.layout.list_item_post_view, null);
        }

        super.getItemView(post, v, parent);
        TextView usernameTextView = (TextView) v.findViewById(R.id.user_name_text_view);
        usernameTextView.setText(post.getNotes()); // some string

        return v;
    }
}

如何解决此问题?

这是 XML Java 的问题吗?

我正在关注Parse和example from the Parse docs的两个教程:

我在这里设置了适配器和ListView:

View rootView = inflater.inflate(R.layout.fragment_post_view_list_view, container, false);
mPostsObjectIds = SOME_STRING[];

PostViewListViewAdapter adapter = new PostViewListViewAdapter(getActivity(), mPostsObjectIds);

ListView listView = (ListView) rootView.findViewById(R.id.post_list_view);
listView.setAdapter(adapter);

我已经尝试删除列表项布局中的ParseImageView,但滚动时我的TextView仍然相互叠加。

修改

我忘了提及在方向更改后,列表项会显示在彼此之上

我在我的Galaxy S5(Android版本4.4.2和Parse 1.4.1)上测试了这个。

在我的活动中,我在这里显示片段(称为PostViewListViewFragment):

getSupportFragmentManager().beginTransaction().add(android.R.id.content, new PostViewListViewFragment()).commit();

6 个答案:

答案 0 :(得分:0)

尝试以下布局:

<?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="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/post_list_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:scrollbars="none"    >

    </ListView>

</RelativeLayout >

答案 1 :(得分:0)

确保你的适配器是这样的:

@Override
  public View getView(int position, View convertView, ViewGroup parent) {
    View rowView = convertView;
    // reuse views
    if (rowView == null) {
      LayoutInflater inflater = context.getLayoutInflater();
      rowView = inflater.inflate(R.layout.rowlayout, null);
      // configure view holder
      ViewHolder viewHolder = new ViewHolder();
      viewHolder.text = (TextView) rowView.findViewById(R.id.TextView01);
      rowView.setTag(viewHolder);
    }

    // fill data
    ViewHolder holder = (ViewHolder) rowView.getTag();
    String s = names[position];
    holder.text.setText(s);

    return rowView;
  }
} 

PS:你应该看一下关于Listview的Google IO video,这里是slides

答案 2 :(得分:0)

首先创建一个ViewHolder类

static class ViewHolder {

        protected TextView usernameTextView;
    }

然后更改你的getItemView方法,如下所示

public View getItemView (Post post, View convertView , ViewGroup parent)
{

    ViewHolder viewHolder = null;

    if (convertView == null)
    {
        LayoutInflater inflator = context.getLayoutInflater();
        convertView = inflator.inflate(R.layout.list_item_post_view, null);
        viewHolder = new ViewHolder();
        viewHolder.usernameTextView = (TextView)convertView.findViewById(R.id.user_name_text_view);

        convertView.setTag(viewHolder);

        convertView.setTag(R.id.user_name_text_view, viewHolder.usernameTextView);


    }

    else
    {
        viewHolder = (ViewHolder) convertView.getTag();
    }


    viewHolder.usernameTextView.setText(post.getNotes()); // some string


    return convertView;


}

答案 3 :(得分:0)

问题似乎出现在列表项目布局中 -

将其更改为 -

<?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">

    <com.parse.ParseImageView
        android:id="@+id/icon"
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:layout_alignParentLeft="true" />

    <TextView
        android:id="@+id/user_name_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/icon"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@color/link_blue" />

</RelativeLayout>

对于导致此类效果的每个列表项集,可能有额外的背景。 改变并观看。

希望这会给你一些想法!

答案 4 :(得分:0)

尝试将列表视图布局高度更改为match_parent。

答案 5 :(得分:0)

感谢@VedPrakash帮助我解决这个问题。

如果它对任何人有帮助,我通过替换片段而非添加来解决问题。所以我从:

更改了这行
getSupportFragmentManager().beginTransaction().add(android.R.id.content, new PostViewListViewFragment()).commit();

getSupportFragmentManager().beginTransaction().replace(android.R.id.content, new PostViewListViewFragment()).commit();