Horizontal ListView as Google Directories

How to make a horizontal list view similar to that presented in Google directories?

The large main area is a view pane, but the bottom line is a horizontal scroll with a list of items that you can click. I guess this is a list, if it were, how will it be done?

I used the open source horizontal list view, which is referenced in other issues, but it doesn't work as smoothly as in this Google app.

+3
source share
2 answers

This is definitely a gallery!

You can see here that this is probably the Gallery that comes with the SDK → Watch the Youtube video to see how smoothly it works;)

I made myself a short guide with Android.com for future reference. I hope you can use it too:

1) Open the res / layout / main.xml file and paste the following:

<?xml version="1.0" encoding="utf-8"?> <Gallery xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gallery" android:layout_width="fill_parent" android:layout_height="wrap_content" /> 

2) Code to insert your onCreate() method:

 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Gallery gallery = (Gallery) findViewById(R.id.gallery); gallery.setAdapter(new ImageAdapter(this)); gallery.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView parent, View v, int position, long id) { Toast.makeText(HelloGallery.this, "" + position, Toast.LENGTH_SHORT).show(); } }); } 

3) Create a new XML file in the res / values ​​/ directory called attrs.xml. Paste the following:

 <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="HelloGallery"> <attr name="android:galleryItemBackground" /> </declare-styleable> </resources> 

4) Go back to your .java file and after the onCreate(Bundle) method, define a custom ImageAdapter class:

 public class ImageAdapter extends BaseAdapter { int mGalleryItemBackground; private Context mContext; private Integer[] mImageIds = { R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7 }; public ImageAdapter(Context c) { mContext = c; TypedArray attr = mContext.obtainStyledAttributes(R.styleable.HelloGallery); mGalleryItemBackground = attr.getResourceId( R.styleable.HelloGallery_android_galleryItemBackground, 0); attr.recycle(); } public int getCount() { return mImageIds.length; } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView = new ImageView(mContext); imageView.setImageResource(mImageIds[position]); imageView.setLayoutParams(new Gallery.LayoutParams(150, 100)); imageView.setScaleType(ImageView.ScaleType.FIT_XY); imageView.setBackgroundResource(mGalleryItemBackground); return imageView; } } 

Well ... the code is very simple, but you can refer to the original and longer document here .

+6
source

I'm not sure, but I think this is a Gallery ( http://developer.android.com/reference/android/widget/Gallery.html ) that sends callbacks to the ViewPager.

Here you can find an example code: http://www.androidpeople.com/android-gallery-example

Instead of a toast, you need to call the viewpager callback and set the page you want.

I think he wants, you want! :-)

+1
source

All Articles