滚动

时间:2018-04-23 16:48:16

标签: android android-recyclerview

我正在尝试学习如何创建一个Recycler View,这是我第一次按照教程进行操作并对Recycler View中的项目内容进行硬编码时一切正常;但当我将内容移动到strings.xml并发送值时出错了。 首先屏幕会很好,看起来像 this(记住我总共有六个项目),滚动后; 任何离开屏幕的项目会在this返回时发生变化,任何帮助都会感激不尽。

注意:当我从列表中删除第六项时,一切都很好;因为没有项目离开屏幕所以没有发生任何变化。

主要活动:

public class MainActivity extends AppCompatActivity {

private RecyclerView mRecycler;
private MyAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private List<Contacts> contacts;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    contacts = new ArrayList<>();

    mRecycler = findViewById(R.id.recyclerTut);

    mRecycler.setHasFixedSize(false);

    DataEntry dataEntry = new DataEntry(getResources().getStringArray(R.array.contact_names),
            getResources().getStringArray(R.array.contact_numbers),
            getResources().obtainTypedArray(R.array.contact_photos));

    contacts = dataEntry.individualContacts();

    mLayoutManager = new LinearLayoutManager(this);
    mRecycler.setLayoutManager(mLayoutManager);

    mAdapter = new MyAdapter(this, contacts);
    mRecycler.setAdapter(mAdapter);
}

}

适配器:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

private Context mContext;
private List<Contacts> mDataset;

public MyAdapter(Context mContext, List<Contacts> myDataset) {
    this.mContext = mContext;
    mDataset = myDataset;
}



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

    View v = LayoutInflater.from(mContext)
            .inflate(R.layout.recycler_view_adapter, parent, false);

    ViewHolder vh = new ViewHolder(v);
    return vh;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {

    holder.titleText.setText(mDataset.get(position).getName());
    holder.subtitleText.setText(mDataset.get(position).getpNumber());
    holder.recyclerImage.setImageResource(mDataset.get(position).getImageSource());

}

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

public static class ViewHolder extends RecyclerView.ViewHolder {

    public ImageView recyclerImage;
    public TextView titleText;
    public TextView subtitleText;

    public ViewHolder(View v) {
        super(v);
        recyclerImage = v.findViewById(R.id.recycler_image);
        titleText = v.findViewById(R.id.recycler_title);
        subtitleText = v.findViewById(R.id.recycler_subtitle);
    }
}

的DataEntry:

public class DataEntry {

private String []names;
private String []numbers;
private TypedArray photos;
private List<Contacts> indivisuals = new ArrayList<>();

public DataEntry(String[] names, String[] numbers, TypedArray photos) {
    this.names = names;
    this.numbers = numbers;
    this.photos = photos;
}

public List<Contacts> individualContacts(){

    for(int i = 0 ; i < names.length ; i++){

        int imageSource = photos.getResourceId(i, -1);
        indivisuals.add(new Contacts(names[i], numbers[i], imageSource));

    }

    return indivisuals;
}}

0 个答案:

没有答案