Android - 滚动上的RecyclerView项目复制

时间:2016-09-13 18:23:40

标签: android android-recyclerview

我正在测试RecyclerView,并希望显示字符串列表。但是我一直遇到一个奇怪的问题。当我滚动我的回收物品时重复。 这是我的问题的图像: enter image description here 这是我的适配器:

$raw_html = file_get_contents('foo.html');
$var = 'hi mom!';
$complete_html = str_replace('%VAR%', $var, $raw_html);

}

每个RecyclerItem的布局:

public class MyAdapter extends RecyclerView.Adapter<LessonAdapter.TopicViewHolder> {


Context mContext;
ArrayList<String> mData;

LessonAdapter(Context context, ArrayList<ListData> list) {
    this.mContext = context;
    initData();
}

public class TopicViewHolder extends RecyclerView.ViewHolder {

    TextView mTopicIndexText;
    TextView mTopicNameText;

    public TopicViewHolder(View itemView) {
        super(itemView);
        mTopicIndexText = (TextView) itemView.findViewById(R.id.index);
        mTopicNameText = (TextView) itemView.findViewById(R.id.topic);
    }
}

private void initData(){

    mData = new ArrayList<String>();
    mData.add("Africa");
    mData.add("Antartica");
    mData.add("Asia");
    mData.add("Australia");
    mData.add("Europe");
    mData.add("North America");
    mData.add("South America");
    mData.add("Africa");
    mData.add("Antartica");
    mData.add("Asia");
    mData.add("Australia");
    mData.add("Europe");
    mData.add("North America");
    mData.add("South America");
    mData.add("Africa");
    mData.add("Antartica");
    mData.add("Asia");
    mData.add("Australia");
    mData.add("Europe");
    mData.add("North America");
    mData.add("South America");
}


@Override
public TopicViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.lesson_item, parent, false);
    TopicViewHolder viewHolder = new TopicViewHolder(view);
    return viewHolder;
}

@Override
public void onBindViewHolder(TopicViewHolder holder, int position) {
    holder.mTopicNameText.setText(mData.get(position));
}

@Override
public int getItemCount() {
    return mData.size();
}

我如何初始化我的RecyclerView:

<?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="55dp"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:id="@+id/index"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1.0"
        android:layout_alignParentLeft="true"
        android:layout_centerInParent="true"
        android:layout_marginLeft="24dp"
        android:textColor="#393C3D"
        android:textSize="16sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/topic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_toRightOf="@id/index"
        android:layout_marginLeft="25dp"
        android:textSize="16sp"
        android:textColor="#393C3D"
        android:maxLines="1"
        android:ellipsize="end"
        android:layout_marginRight="24dp" />

</RelativeLayout>

RecyclerView的布局:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_lesson, container, false);
        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.lesson_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        MyAdapter adapter = new MyAdapter(getActivity(), mData);
        recyclerView.setAdapter(adapter);
        return view;
    }

1 个答案:

答案 0 :(得分:2)

您的活动创建了两次片段