1

I have been looking at the tutorial here for how to get started with horizontalscrollviews so have created a mock program to practice UI elements. I am trying to learn how to populate a horizontal scroll view with a dynamic number of "entries" which can be scrolled across. I have managed to get it showing initially one "entry", which in this case is an imageview, and when you click the button, it adds a new one. This works fine. Ultimately, i'd like to inflate what is effectively a custom layout, a simple example would be an Imageview, with a line of text beneath it which is what I am currently trying to add as you can see from the (nonworking) code addition in the insertElement method.

For now I'd like to have the drawable resource image, with a textview underneath saying which element it is but this isn't currently working and I'm not sure what to add to get this working correctly.

//Currently, the imageView is showing and being added correctly, but I can't see any text //underneath the images. (FIXED)

My questions, Firstly, am I going about this in the best way? Meaning, is it possible, and if so, is it easier to define a custom layout with the imageview on top and the textview beneath which can be inflated into the scrollview as needed, and then the contents set in a similar way to how I currently am. And how would I do this?

Secondly, I am eventually going to be trying to use the "Universal Image Loader" to lazy load images instead of resources into these scrollable imageviews. Could anyone suggest a way of integrating this library to the code format I'm using for now to work with the lazy loading? I know you need to pass UIL the url to load and the imageview to load it to - how do I for example get the reference to this new imageview I'm inflating in such a way as it is possible to pass it to UIL.

My fragment layout 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>

My main java relevant code:

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

Thanks for your help, I've never ventured into the world of nice UIs in android. Has always been functional (hideous) tools before!

EDIT: I see now why my textview wasn't visible, I had forgotten the line,

layout.addView(textView)

in the insertElement method. I have now modified the given java code to reflect this.

The other questions regarding directly inflating a layout xml instead of programatically adding new elements directly is still standing, as well as integrating UIL.

4

0 回答 0