gpt4 book ai didi

android - 如何分别在70张图片的不同位置分配按钮?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:11:39 25 4
gpt4 key购买 nike

我的问题:

I have 70 images, and on each image I want to put transparent button in such a way that when user taps on it, it plays a short audio regarding the spot on image. Images are displaying in a ViewPager.

我的解决方案:

现在我的想法是我可以创建 70 个 fragment ,每个 fragment 包含各自的图像作为背景,我可以轻松地在每个点上分配按钮,并为将播放各自音频的按钮分配 Action 。

但是

这看起来不是在单个应用中包含 70 个 fragment 的好方法。

那么我该如何实现这一点,我可以使用什么更好的方法呢?

最佳答案

我们可以只用一个 fragment 并创建一个 ArrayList of Map(Button, RelatedAudioFile) 数据结构来保存按钮和相关的音频文件。 ArrayList 索引表示页码。

例子:现在假设我们有 3 个 viewPages。由于大多数 PagerAdapter 索引从“0”开始,假设 Page-0 包含 3 个按钮,Page-1 包含 1 个按钮,Page-2 包含 2 个按钮。

伪代码:

class PagerHolder extends Fragment {

//buttons list - here, arrayList index represents page number
//and at each index we have map of buttons (buttons in each of these pages) and the related audio files
private ArrayList<Map<Button, RelatedAudioFile>> pageButtonsList = new ArrayList<>();
//pager view
private ViewPager viewPager;
//pager adapter
private PagerAdapter pagerAdapter;
//current page number -- page in which we are in right now
private int currentpageNumber = 0;

//buttonListener -- one button listener for all the buttons in all the pages.
private View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View buttonView) {
//here you can play the audio.
//buttonView -- is the same button-object that was pressed.
((RelatedAudioFile)pageButtonsList.get(currentpageNumber).get(buttonView)).play();

}
};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pagerAdapter = new PagerAdapter(getChildFragmentManager());

//add buttons to the list
//page 0 buttons
HashMap<Button, RelatedAudioFile> page0Buttons = new HashMap<>();
page0Buttons.put(new Button(context), new RelatedAudioFile());
page0Buttons.put(new Button(context), new RelatedAudioFile());
page0Buttons.put(new Button(context), new RelatedAudioFile());
pageButtonsList.add(page0Buttons)

//Adding page 1 buttons:
HashMap<Button, RelatedAudioFile> page1Buttons = new HashMap<>();
page1Buttons.put(new Button(context), new RelatedAudioFile());
pageButtonsList.add(page1Buttons);

//Adding page 2 buttons:
HashMap<Button, RelatedAudioFile> page2Buttons = new HashMap<>();
page2Buttons.put(new Button(context), new RelatedAudioFile());
page2Buttons.put(new Button(context), new RelatedAudioFile());
pageButtonsList.add(page2Buttons);

for(Map<Button, RelatedAudioFile> pageButtons :pageButtonsList) {
for(Button button : pageButtons.keySet()) {
button.setOnClickListener(listener);
}
}

}



@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_editor_pager, container, false);

viewPager = (ViewPager) v.findViewById(R.id.view_pager);
viewPager.setAdapter(pagerAdapter);

return v;
}



private class PagerAdapter extends FragmentPagerAdapter {

private ButtonFragment buttonFragment;

private PagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int pageNumber) {
currentpageNumber = pageNumber;

//pass the buttons to the fragment so that it can add these buttons to the screen
buttonFragment = ButtonFragment.newInstance(pageButtonsList.get(pageNumber).keySet());

//to add buttons on screen programatically at certain position you can refer here:
// http://stackoverflow.com/questions/3441475/android-change-absolute-position-of-a-view-programmatically
}

//number of pages
@Override
public int getCount() {
return 70;
}
}

关于android - 如何分别在70张图片的不同位置分配按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36952199/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com