I am currently using Gallery to display a list of items. Everything work fine except the center locking feature which I don’t like. I found this HorizontalListView implementation from here http://www.dev-smart.com/archives/34 and want using it to replace the Android Gallery. I downloaded the HorizontalListView.java, put it in my project and change Gallery to HorizontalListView, but when I run the app, nothing show up. The items of the Gallery (HorizontalListView now) is set by using ArrayAdapter. Did I set up anything wrong? Here’s the code:
Layout HorizontalListView:
<com.myapp.HorizontalListView
android:id="@+id/related"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ebebeb" >
</com.myapp.HorizontalListView>
HorizontalListView item:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff"
android:orientation="vertical" >
<ImageView
android:id="@+id/gameicon"
android:layout_width="120dip"
android:layout_height="120dip"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:src="@drawable/iconbig" />
<TextView
android:id="@+id/gamename"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:gravity="center_horizontal|center_vertical"
android:text="Title"
android:textSize="15sp"
android:textColor="#000000"
android:textStyle="bold"/>
</LinearLayout>
Adapter for items:
public class RelatedListAdapter extends ArrayAdapter<GameInfo> {
private ArrayList<GameInfo> items;
public static HashMap<String, String> relatedImageMap;
public RelatedListAdapter(Context context, int textViewResourceId,
ArrayList<GameInfo> items) {
super(context, textViewResourceId, items);
this.items = items;
if (relatedImageMap == null)
relatedImageMap = new HashMap<String, String>();
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater mInflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = mInflater.inflate(R.layout.related_items, null);
}
GameInfo game = items.get(position);
if (game != null) {
TextView name = (TextView) v.findViewById(R.id.gamename);
ImageView icon = (ImageView) v.findViewById(R.id.gameicon);
if (name != null) {
name.setBackgroundColor(Color.parseColor("#ebebeb"));
name.setText(NewDetailActivity.TruncateString(game.getName(), 8) );
}
if (icon != null) {
icon.setBackgroundColor(Color.parseColor("#ebebeb"));
if (EfficientAdapter.imageMap.containsKey(game.getId())) {
String filePath = EfficientAdapter.imageMap.get(game
.getId());
Bitmap bm = BitmapFactory.decodeFile(filePath);
icon.setImageBitmap(bm);
} else if (relatedImageMap.containsKey(game.getId())) {
String filePath = relatedImageMap.get(game.getId());
Bitmap bm = BitmapFactory.decodeFile(filePath);
icon.setImageBitmap(bm);
} else {
String storagePath = Environment
.getExternalStorageDirectory().toString()
+ getContext().getResources().getString(
R.string.imgCacheFolder);
String image = game.getImgUrl().replaceAll(
"[^a-zA-Z 0-9]+", "");
String filePath = storagePath + image;
EfficientAdapter.LoadImageFromWebOperations(game.getImgUrl(),
filePath, null);
if (relatedImageMap != null) {
synchronized (relatedImageMap) {
relatedImageMap.put(game.getId(), filePath);
}
}
Bitmap bm = BitmapFactory.decodeFile(filePath);
icon.setImageBitmap(bm);
}
}
}
return v;
}
}
And in my Activity:
HorizontalListView related = (HorizontalListView) findViewById(R.id.related);
data = new ArrayList<GameInfo>();
adap = new RelatedListAdapter(this, R.layout.related_items, data);
related.setAdapter(adap);
If I change HorizontalListView to Gallery, everything work fine and the list is shown. But using HorizontalListView, nothing show up. Can anyone help me out with this?
EDIT: I found out something different now. The HorizontalListView did show up, but not the first time I open it. For example I have 3 tab and HorizontalListView in tab 3. When I start activity, it show tab 1. Then I click tab 3, nothing show up. I changed to tab 1 or tab 2, then return to tab 3, the list is showing now. Strange huh? Does anybody know why?
I also used the HorizontalListView in the before. But it is too hard to adapt to suite my particular use case.
Then I found this class HorizontalScrollView. It is defined in android SDK – much more easy to use and versatile.