Android中的动态大小水平滚动视图

时间:2013-07-18 12:52:52

标签: java android layout horizontalscrollview layout-inflater

我一直在查看教程here,了解如何开始使用horizo​​ntalscrollviews,因此创建了一个模拟程序来练习UI元素。我正在尝试学习如何使用动态数量的“条目”填充水平滚动视图,这些条目可以滚动。我设法让它最初显示一个“条目”,在这种情况下是一个imageview,当你点击按钮时,它会添加一个新条目。这很好用。最后,我想要夸大什么是有效的自定义布局,一个简单的例子就是一个Imageview,在它下面有一行文本,这正是我目前正在尝试添加的内容,正如您从(非工作)代码添加中看到的那样在insertElement方法中。

现在我想拥有可绘制的资源图像,下面有一个textview,说明它是哪个元素但是目前还没有工作,我不知道要添加什么来使其正常工作。

//目前,imageView正在显示并正确添加,但我无法在图像下方看到任何文本。 (FIXED)

我的问题, 首先,我是以最好的方式解决这个问题吗?意思是,是否可能,如果是这样,是否更容易定义一个自定义布局,其中imageview在顶部,textview下面可以根据需要膨胀到scrollview,然后内容设置方式与我当前的方式类似上午。我该怎么做?

其次,我最终将尝试使用“通用图像加载器”将图像延迟加载到这些可滚动的图像视图中而不是资源。任何人都可以建议一种方法将这个库集成到我现在用来处理延迟加载的代码格式吗?我知道你需要传递UIL加载的url和imageview来加载它 - 我如何得到对这个新的imageview的引用我正在以可以将它传递给UIL的方式进行膨胀。 / p>

我的片段布局xml:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/horizontalscroller1">

    <HorizontalScrollView
            android:id="@+id/hsvIcons"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        <LinearLayout
                android:id="@+id/mygallery"
                android:layout_width="match_parent"
                android:layout_height="200dip"
                android:orientation="horizontal"
                />
    </HorizontalScrollView>
    <Button
        android:id="@+id/bAddItem"
        android:gravity="center"
        android:text="Add Item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/hsvIcons"
        android:layout_centerHorizontal="true"/>

</RelativeLayout>

我的主要java相关代码:

public Fragment getItem(int position) {
            Fragment fragment;
            if(position < 3){
                // getItem is called to instantiate the fragment for the given page.
                // Return a DummySectionFragment (defined as a static inner class
                // below) with the page number as its lone argument.
                fragment = new DummySectionFragment();
                Bundle args = new Bundle();
                args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
                fragment.setArguments(args);
            }else if(position == 3){
                fragment = new MyFragment();
                Bundle args = new Bundle();
                ArrayList<String> strings = new ArrayList<String>();
                strings.add("one");
                strings.add("two");
                strings.add("three");
                args.putStringArrayList(MyFragment.ARG_STRING_LIST, strings);
                fragment.setArguments(args);
            }else if (position == 4){
                fragment = new MyScrollView();
            }else{
                fragment = new DummySectionFragment();
                Bundle args = new Bundle();
                args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
                fragment.setArguments(args);
            }
            return fragment;
        }

public class MyScrollView extends Fragment {

        int itemCount;
        Button addItem;
        LinearLayout myScrollable;

        public MyScrollView() {
            itemCount = 1;
        }
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.my_horiz_scroller, container, false);
            addItem = (Button) rootView.findViewById(R.id.bAddItem);
            myScrollable = (LinearLayout) rootView.findViewById(R.id.mygallery);
            addItem.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View view) {
                    myScrollable.addView(insertElement(R.drawable.web, itemCount++));
                }
            });
            myScrollable.addView(insertElement(R.drawable.chrome, itemCount++));
            return rootView;
        }
    }

    View insertElement(int resID, int itemCount){
        LinearLayout layout = new LinearLayout(getApplicationContext());
        layout.setLayoutParams(new LinearLayout.LayoutParams(250, 250));
        layout.setGravity(Gravity.CENTER);
        layout.setOrientation(LinearLayout.VERTICAL);

        ImageView imageView = new ImageView(getApplicationContext());
        imageView.setLayoutParams(new LinearLayout.LayoutParams(220, 220));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setImageResource(resID);

        TextView textView = new TextView(getApplicationContext());
        textView.setText("Item " + itemCount);

        layout.addView(imageView);
        layout.addView(textView);
        return layout;
    }

感谢您的帮助,我从来没有冒险进入Android的漂亮用户界面。以前一直是功能性(可怕的)工具!

编辑:我现在看到为什么我的textview不可见,我忘记了这条线,

layout.addView(textView)
在insertElement方法中

。我现在修改了给定的java代码以反映这一点。

关于直接膨胀布局xml而不是以编程方式直接添加新元素的其他问题仍然存在,以及集成UIL。

0 个答案:

没有答案