Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
228 views
in Technique[技术] by (71.8m points)

android - display random page on recycleview

Can I display random page when our recycleview apps? I want to put a floatbutton on my detailactivity.javaclass and when I click it must display a page with my string data and image files.

mFlowerData = new FlowerData("Rose", getString(R.string.description_flower_rose),
            R.drawable.rose);
mFlowerList.add(mFlowerData);
mFlowerData = new FlowerData("Carnation", getString(R.string.description_flower_carnation),
            R.drawable.carnation);

How can I display it on the last page.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use something like this in your activity/fragment

private List<Simple> elements;

private String names[] = {
        "Monk's Oartormow",
        "Eldoth Nut",
        "Eblurst",
        "Poison Okule",
        "Black Berry",
        "Snake Eye",
        "Witch's Root",
        "White Burm",
        "Yellow Pig Claw",
        "Uchea Mint"
};

private String contents[] = {
        "What if the adaptable injury ate the click?",
        "The united chip stuffs into the babyish sad.",
        "Did the interesting progress really argue the visual?",
        "What if the chief sign ate the schedule?",
        "Is the brush respect better than the raise?",
        "It was then the inexperienced topic met the open period."
};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_flower);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    elements = new ArrayList<>();

    adapter = new SimpleAdapter();

    RecyclerView recyclerView = findViewById(R.id.recycler_view);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setAdapter(adapter);

    FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Simple s = generateRandomElement();
            elements.add(0,s);
            adapter.updateElements(elements);
        }
    });
}

private Simple generateRandomElement() {
    int posNames = (int) (Math.random() * names.length);
    int posContent = (int) (Math.random() * contents.length);
    String name = names[posNames];
    String content = contents[posContent];

    return new Simple(name, content);
}

and for updating the elements in the adapter:

private List<Simple> elements;

public SimpleAdapter() {
    this.elements = new ArrayList<>();
}

@Override
public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.row_simple_element, parent, false);
    return new SimpleViewHolder(view);
}

@Override
public void onBindViewHolder(SimpleViewHolder holder, int position) {
    Simple element = elements.get(position);
    holder.title.setText(element.getTitle());
    holder.content.setText(element.getContent());
}

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

public void updateElements(List<Simple> elements) {
    this.elements.clear();
    this.elements.addAll(elements);
    notifyDataSetChanged();
}

you can improve how the elements are updated implementing a DiffCallback for your Flower class (link)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...