简体   繁体   English

在活动布局中多次合并/包含xml布局文件的最佳解决方案

[英]Best solution to merge/include an xml layout file multiple times in an activity layout

I'm building a flashcards app as a college project, and wanted a horizontally scrolling display of the cards. 我正在将抽认卡应用程序构建为大学项目,并希望水平滚动显示这些卡片。 I've built an xml file for the flashcard design itself: 我已经为抽认卡设计本身构建了一个xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:background="@drawable/whitenote" android:padding="3dp">

    <ImageButton
        android:id="@+id/imageButtonPins"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" android:background="@color/transparent" android:src="@drawable/pinselector"/>

    <TextView
        android:id="@+id/textViewWord"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageButton1"
        android:layout_centerHorizontal="true"
        android:text="Word" android:textColor="@color/black" android:paddingTop="30dp" android:textSize="20dp"/>

    <TextView
        android:id="@+id/textViewMeaning"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textViewWord"
        android:layout_centerHorizontal="true"
        android:text="Meaning" android:textColor="@color/black"/>

</RelativeLayout>

I've also created the Class file. 我还创建了Class文件。

In the activity layout, I have a few more elements and as such a linearlayout at the root level is necessary. 在活动布局中,我还有更多元素,因此在根级别进行线性布局是必要的。 I've been able to display a single flashcard for test purposes using and using layout inflater. 我已经能够使用布局充气机显示单个抽认卡用于测试目的。

Question
In both the ways, in and layout inflater I've been unable to get the ImageButton working. 在这两种方式中,都无法使ImageButton正常工作。 My question is how do I get the button to work. 我的问题是如何使按钮起作用。

Update: Managed to get the ImageButton working using . 更新:设法使ImageButton使用。 Realised that I have to handle the onclick event in the activity, and not the Custom Adapter class. 意识到我必须处理活动中的onclick事件,而不是Custom Adapter类。 This should allow me to obtain the words too, as long as I can keep track of the "current" flashcard on display 只要我可以跟踪显示的“当前”抽认卡,这也应该使我也能读懂这些单词

Also, whats the best way to handle the scrolling for a flashcard app? 此外,什么是处理抽认卡应用程序滚动的最佳方法? My current plans so far is to use a HorizontalScrollView and customise it a bit, because I need (a) a swipe should make the flashcard move only to the next one (b) I need to focus on the "current" flashcard since I need some data from its children views (ie, the word). 到目前为止,我目前的计划是使用Horizo​​ntalScrollView并对其进行一些自定义,因为我需要(a)滑动即可使抽认卡仅移至下一个(b)我需要专注于“当前”抽认卡,因为我需要来自其子视图的一些数据(即单词)。

对于问题的第二部分,请看一下ViewPager

Are you considering Fragments? 您在考虑片段吗?

You can get some help with the ViewPager here.This is supported in Android 3.0 or above or Android 1.6 with the compatibility package. 您可以在此处获得有关ViewPager的帮助。Android3.0或更高版本或具有兼容性包的Android 1.6均支持此功能。

http://geekyouup.blogspot.com/2011/07/viewpager-example-from-paug.html http://geekyouup.blogspot.com/2011/07/viewpager-example-from-paug.html

If you do not wish to use the fragments, you can simply use the Gallery. 如果您不希望使用这些片段,则可以仅使用图库。 This way, you can achieve the horizontal scrolling. 这样,您可以实现水平滚动。 (like in the Amazon app) without complex ViewPager. (例如在Amazon应用中),而无需复杂的ViewPager。

A HorizontalScrollView or a Gallery are probably the most direct way of implementing this. HorizontalScrollViewGallery可能是实现此目的的最直接方法。 I don't use Gallery -- but it is good to at least know it exists. 我不使用Gallery ,但是至少知道它的存在是一件好事。

If you want a much more robust implementation, I agree with dcanh121 and think you should check out a Fragment based ViewPager . 如果你想要一个健壮的实现,我同意dcanh121,并认为你应该看看一个Fragment基于ViewPager This will allow more options than just a View , but might be overkill depending on the goal. 这将允许更多选择,而不仅仅是View,但根据目标可能会过分杀伤。 A fragment is basically the bizarre offspring of an Activity and a View , but don't quote me on that. fragment 基本上ActivityView的奇异后代,但请不要在此引用我。

Also, 也,

Inflating layouts is costly, so try to only inflate the XML into a View once, and reuse that View object. 扩大布局的成本很高,因此请尝试仅将XML一次扩充到一个View ,然后重用该View对象。 Try not to re-inflate the XML every time a new flashcard is drawn. 每次绘制新的抽认卡时,请尽量不要重新添加XML。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM