android - Recyclerview 滚动上的重复项

标签 android android-recyclerview

这似乎是一个很常见的问题,但其他问题都没有帮助我解决这个问题。所以,问题是,我有一个 recyclerview,它在我滚动时复制项目。像这样: duplicated items.

看起来可滚动列表下方有一个固定列表。 这是我的适配器:

public class FriendsListAdapter extends   RecyclerView.Adapter<FriendsListAdapter.ViewHolder> {
    private ArrayList<Friend> mDataset;
    private Context ctx;

    public class ViewHolder extends RecyclerView.ViewHolder {

        public TextView friendName;
        public ImageView friendPicture;

        public ViewHolder(View v) {
            super(v);
            friendName = (TextView) v.findViewById(R.id.friendName);
            friendPicture = (ImageView) v.findViewById(R.id.friendPicture);
        }
    }

    public FriendsListAdapter(ArrayList<Friend> myDataset, Context context) {
        mDataset = myDataset;
        ctx = context;
    }

    @Override
    public FriendsListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,    int viewType) {
        View v =   LayoutInflater.from(parent.getContext()).inflate(R.layout.friend_item,   parent, false);
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }


    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.friendName.setText(mDataset.get(position).getName());
        holder.friendName.setTag(mDataset.get(position));

//
//        if(mDataset.get(position).getImageUrl() != null &&    !mDataset.get(position).getImageUrl().isEmpty()) {
//            holder.friendPicture.setImageBitmap(null);
//
//            Transformation transformation = new       RoundedTransformationBuilder()
//                    .cornerRadiusDp(50)
//                    .oval(false)
//                    .build();
//
//            Picasso.with(ctx)
//                    .load(mDataset.get(position).getImageUrl())
//                    .fit()
//                    .transform(transformation)
//                    .into(holder.friendPicture);
//        }

    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mDataset.size();
    }

}

picasso 部分被评论是因为我正在测试看是否是问题所在。

这是我 fragment 的 onCreateView:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    super.onCreateView(inflater, container, savedInstanceState);

    View view = inflater.inflate(R.layout.fragment_friends_list, container, false);
    mRecyclerView = (RecyclerView) view.findViewById(R.id.my_recycler_view);
    mLayoutManager = new LinearLayoutManager(getActivity());
    mRecyclerView.setLayoutManager(mLayoutManager);
    mDataset = new ArrayList<>();

    final ArrayList<String> values = new ArrayList<String>();

    if(MyApp.getConfigHelper().getFriends() == null || MyApp.getConfigHelper().getFriends().isEmpty()) {
        Bundle params = new Bundle();
        params.putString("fields", "id, name, picture");

        GraphRequest request = GraphRequest.newMyFriendsRequest(
                AccessToken.getCurrentAccessToken(),
                new GraphRequest.GraphJSONArrayCallback() {
                    @Override
                    public void onCompleted(JSONArray array, GraphResponse response) {
                        FacebookRequestError error = response.getError();
                        if (error == null) {
                            try {
                                Friend pictureByName;
                                for (int i = 0; i < array.length(); i++) {
                                    values.add(array.getJSONObject(i).getString("name"));
                                    JSONObject arrayObject = array.getJSONObject(i);
                                    JSONObject picObject = arrayObject.getJSONObject("picture");
                                    String fbId = arrayObject.getString("id");
                                    String picUrl = picObject.getJSONObject("data").getString("url");
                                    pictureByName = new Friend(fbId, values.get(i), picUrl);
                                    mDataset.add(pictureByName);
                                }

                                Collections.sort(mDataset);
                                MyApp.getConfigHelper().setFriends(mDataset);

                                mAdapter = new FriendsListAdapter(mDataset, getActivity());
                                mRecyclerView.setAdapter(mAdapter);

                            } catch (JSONException e) {

                            }
                        }

                    }
                });
        request.setParameters(params);
        request.executeAsync();
    } else {
        mDataset = MyApp.getConfigHelper().getFriends();
        mAdapter = new FriendsListAdapter(mDataset, getActivity());
        mRecyclerView.setAdapter(mAdapter);
    }
    return view;
}

这是框架:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent"
    tools:context="com.myApp.Activity.FriendsListActivity">


<android.support.v7.widget.RecyclerView
    android:id="@+id/my_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical" />


</RelativeLayout>

你们知道发生了什么事吗?

最佳答案

努力改变

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent"
    tools:context="com.myApp.Activity.FriendsListActivity">

<android.support.v7.widget.RecyclerView
    android:id="@+id/my_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical" />

</RelativeLayout>

通过:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent"
    tools:context="com.myApp.Activity.FriendsListActivity">


<android.support.v7.widget.RecyclerView
    android:id="@+id/my_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical" />


</LinearLayout>

我认为 RelativeLayout 叠加了你的 Recycler 的每一页

关于android - Recyclerview 滚动上的重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36240878/

相关文章:

android - 在搜索栏中创建垂直线

Android 自动链接以启动 WebView

android - 如何缓存一个简单的XML文件?

android - 为什么第二个代码可以安全地在 map 中进行网络调用,因为它已被缓存?

java - 我如何使用来自 firebase 的数据制作一个可点击的 recyclerview

java - 使用 Retrofit2 在 Recycleview 中得到空响应?

java - 将多个 RecyclerView 与一个 Adapter 类一起使用

android - 我可以删除旧的 Android SDK 构建工具包吗?

java - Room 数据库中的项目未显示在 RecyclerView 中

android - 为 recyclerview 实现上下文操作栏