本文介绍了在回收视图上显示随机页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在我们的 recycleview 应用程序中显示随机页面吗?我想在我的 detailactivity.javaclass 上放置一个 floatbutton,当我点击它时,它必须显示一个包含我的字符串数据和图像文件的页面.

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);

如何在最后一页显示.

推荐答案

你可以在你的活动/片段中使用这样的东西

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();
}

您可以通过为您的 Flower 类实现 DiffCallback 来改进元素的更新方式(link)

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

这篇关于在回收视图上显示随机页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 22:18