This is a ListView but it is Horizontal scrolling preferred. It containing image,button,and textView.
Get Reference from DEV-SMART, Origional post http://www.dev-smart.com/archives/34 here.
HorizontalListView.java
package com.sandy.demo;
import java.util.LinkedList;
import java.util.Queue;
import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.Scroller;
public class HorizontalListView extends AdapterView<ListAdapter> {
public boolean mAlwaysOverrideTouch = true;
protected ListAdapter mAdapter;
private int mLeftViewIndex = -1;
private int mRightViewIndex = 0;
protected int mCurrentX;
protected int mNextX;
private int mMaxX = Integer.MAX_VALUE;
private int mDisplayOffset = 0;
protected Scroller mScroller;
private GestureDetector mGesture;
private Queue<View> mRemovedViewQueue = new LinkedList<View>();
private OnItemSelectedListener mOnItemSelected;
private OnItemClickListener mOnItemClicked;
private OnItemLongClickListener mOnItemLongClicked;
private boolean mDataChanged = false;
public HorizontalListView(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
private synchronized void initView() {
mLeftViewIndex = -1;
mRightViewIndex = 0;
mDisplayOffset = 0;
mCurrentX = 0;
mNextX = 0;
mMaxX = Integer.MAX_VALUE;
mScroller = new Scroller(getContext());
mGesture = new GestureDetector(getContext(), mOnGesture);
}
@Override
public void setOnItemSelectedListener(AdapterView.OnItemSelectedListener listener) {
mOnItemSelected = listener;
}
@Override
public void setOnItemClickListener(AdapterView.OnItemClickListener listener){
mOnItemClicked = listener;
}
@Override
public void setOnItemLongClickListener(AdapterView.OnItemLongClickListener listener) {
mOnItemLongClicked = listener;
}
private DataSetObserver mDataObserver = new DataSetObserver() {
@Override
public void onChanged() {
synchronized(HorizontalListView.this){
mDataChanged = true;
}
invalidate();
requestLayout();
}
@Override
public void onInvalidated() {
reset();
invalidate();
requestLayout();
}
};
@Override
public ListAdapter getAdapter() {
return mAdapter;
}
@Override
public View getSelectedView() {
//TODO: implement
return null;
}
@Override
public void setAdapter(ListAdapter adapter) {
if(mAdapter != null) {
mAdapter.unregisterDataSetObserver(mDataObserver);
}
mAdapter = adapter;
mAdapter.registerDataSetObserver(mDataObserver);
reset();
}
private synchronized void reset(){
initView();
removeAllViewsInLayout();
requestLayout();
}
@Override
public void setSelection(int position) {
//TODO: implement
}
private void addAndMeasureChild(final View child, int viewPos) {
LayoutParams params = child.getLayoutParams();
if(params == null) {
params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
}
addViewInLayout(child, viewPos, params, true);
child.measure(MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.AT_MOST),
MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.AT_MOST));
}
@Override
protected synchronized void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
if(mAdapter == null){
return;
}
if(mDataChanged){
int oldCurrentX = mCurrentX;
initView();
removeAllViewsInLayout();
mNextX = oldCurrentX;
mDataChanged = false;
}
if(mScroller.computeScrollOffset()){
int scrollx = mScroller.getCurrX();
mNextX = scrollx;
}
if(mNextX <= 0){
mNextX = 0;
mScroller.forceFinished(true);
}
if(mNextX >= mMaxX) {
mNextX = mMaxX;
mScroller.forceFinished(true);
}
int dx = mCurrentX - mNextX;
removeNonVisibleItems(dx);
fillList(dx);
positionItems(dx);
mCurrentX = mNextX;
if(!mScroller.isFinished()){
post(new Runnable(){
@Override
public void run() {
requestLayout();
}
});
}
}
private void fillList(final int dx) {
int edge = 0;
View child = getChildAt(getChildCount()-1);
if(child != null) {
edge = child.getRight();
}
fillListRight(edge, dx);
edge = 0;
child = getChildAt(0);
if(child != null) {
edge = child.getLeft();
}
fillListLeft(edge, dx);
}
private void fillListRight(int rightEdge, final int dx) {
while(rightEdge + dx < getWidth() && mRightViewIndex < mAdapter.getCount()) {
View child = mAdapter.getView(mRightViewIndex, mRemovedViewQueue.poll(), this);
addAndMeasureChild(child, -1);
rightEdge += child.getMeasuredWidth();
if(mRightViewIndex == mAdapter.getCount()-1) {
mMaxX = mCurrentX + rightEdge - getWidth();
}
if (mMaxX < 0) {
mMaxX = 0;
}
mRightViewIndex++;
}
}
private void fillListLeft(int leftEdge, final int dx) {
while(leftEdge + dx > 0 && mLeftViewIndex >= 0) {
View child = mAdapter.getView(mLeftViewIndex, mRemovedViewQueue.poll(), this);
addAndMeasureChild(child, 0);
leftEdge -= child.getMeasuredWidth();
mLeftViewIndex--;
mDisplayOffset -= child.getMeasuredWidth();
}
}
private void removeNonVisibleItems(final int dx) {
View child = getChildAt(0);
while(child != null && child.getRight() + dx <= 0) {
mDisplayOffset += child.getMeasuredWidth();
mRemovedViewQueue.offer(child);
removeViewInLayout(child);
mLeftViewIndex++;
child = getChildAt(0);
}
child = getChildAt(getChildCount()-1);
while(child != null && child.getLeft() + dx >= getWidth()) {
mRemovedViewQueue.offer(child);
removeViewInLayout(child);
mRightViewIndex--;
child = getChildAt(getChildCount()-1);
}
}
private void positionItems(final int dx) {
if(getChildCount() > 0){
mDisplayOffset += dx;
int left = mDisplayOffset;
for(int i=0;i<getChildCount();i++){
View child = getChildAt(i);
int childWidth = child.getMeasuredWidth();
child.layout(left, 0, left + childWidth, child.getMeasuredHeight());
left += childWidth + child.getPaddingRight();
}
}
}
public synchronized void scrollTo(int x) {
mScroller.startScroll(mNextX, 0, x - mNextX, 0);
requestLayout();
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
boolean handled = super.dispatchTouchEvent(ev);
handled |= mGesture.onTouchEvent(ev);
return handled;
}
protected boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
synchronized(HorizontalListView.this){
mScroller.fling(mNextX, 0, (int)-velocityX, 0, 0, mMaxX, 0, 0);
}
requestLayout();
return true;
}
protected boolean onDown(MotionEvent e) {
mScroller.forceFinished(true);
return true;
}
private OnGestureListener mOnGesture = new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onDown(MotionEvent e) {
return HorizontalListView.this.onDown(e);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
return HorizontalListView.this.onFling(e1, e2, velocityX, velocityY);
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
synchronized(HorizontalListView.this){
mNextX += (int)distanceX;
}
requestLayout();
return true;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
for(int i=0;i<getChildCount();i++){
View child = getChildAt(i);
if (isEventWithinView(e, child)) {
if(mOnItemClicked != null){
mOnItemClicked.onItemClick(HorizontalListView.this, child, mLeftViewIndex + 1 + i, mAdapter.getItemId( mLeftViewIndex + 1 + i ));
}
if(mOnItemSelected != null){
mOnItemSelected.onItemSelected(HorizontalListView.this, child, mLeftViewIndex + 1 + i, mAdapter.getItemId( mLeftViewIndex + 1 + i ));
}
break;
}
}
return true;
}
@Override
public void onLongPress(MotionEvent e) {
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View child = getChildAt(i);
if (isEventWithinView(e, child)) {
if (mOnItemLongClicked != null) {
mOnItemLongClicked.onItemLongClick(HorizontalListView.this, child, mLeftViewIndex + 1 + i, mAdapter.getItemId(mLeftViewIndex + 1 + i));
}
break;
}
}
}
private boolean isEventWithinView(MotionEvent e, View child) {
Rect viewRect = new Rect();
int[] childPosition = new int[2];
child.getLocationOnScreen(childPosition);
int left = childPosition[0];
int right = left + child.getWidth();
int top = childPosition[1];
int bottom = top + child.getHeight();
viewRect.set(left, top, right, bottom);
return viewRect.contains((int) e.getRawX(), (int) e.getRawY());
}
};
}
HorizontalListViewDemo.java
package com.sandy.demo;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
public class HorizontalListViewDemo extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listviewdemo);
HorizontalListView listview = (HorizontalListView) findViewById(R.id.listview);
listview.setAdapter(mAdapter);
}
private static String[] dataObjects = new String[]{ "Text #1",
"Text #2",
"Text #3","Text #4","Text #5","Text #6","Text #7","Text #8","Text #9","Text #10" };
private BaseAdapter mAdapter = new BaseAdapter() {
private OnClickListener mOnButtonClicked = new OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(HorizontalListViewDemo.this);
builder.setMessage("hello from " + v);
builder.setPositiveButton("Cool", null);
builder.show();
}
};
@Override
public int getCount() {
return dataObjects.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View retval = LayoutInflater.from(parent.getContext()).inflate(R.layout.viewitem, null);
TextView title = (TextView) retval.findViewById(R.id.title);
Button button = (Button) retval.findViewById(R.id.clickbutton);
button.setOnClickListener(mOnButtonClicked);
title.setText(dataObjects[position]);
return retval;
}
};
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
listviewdemo.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff"
>
<com.sandy.demo.HorizontalListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ddd"
/>
</LinearLayout>
viewitem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#fff"
>
<ImageView
android:id="@+id/image"
android:layout_width="150dip"
android:layout_height="150dip"
android:scaleType="centerCrop"
android:src="@drawable/icon"
/>
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:gravity="center_horizontal"
/>
<Button
android:id="@+id/clickbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:text="Click Me"
/>
</LinearLayout>
Click here to Download.
Get Reference from DEV-SMART, Origional post http://www.dev-smart.com/archives/34 here.
HorizontalListView.java
package com.sandy.demo;
import java.util.LinkedList;
import java.util.Queue;
import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.Scroller;
public class HorizontalListView extends AdapterView<ListAdapter> {
public boolean mAlwaysOverrideTouch = true;
protected ListAdapter mAdapter;
private int mLeftViewIndex = -1;
private int mRightViewIndex = 0;
protected int mCurrentX;
protected int mNextX;
private int mMaxX = Integer.MAX_VALUE;
private int mDisplayOffset = 0;
protected Scroller mScroller;
private GestureDetector mGesture;
private Queue<View> mRemovedViewQueue = new LinkedList<View>();
private OnItemSelectedListener mOnItemSelected;
private OnItemClickListener mOnItemClicked;
private OnItemLongClickListener mOnItemLongClicked;
private boolean mDataChanged = false;
public HorizontalListView(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
private synchronized void initView() {
mLeftViewIndex = -1;
mRightViewIndex = 0;
mDisplayOffset = 0;
mCurrentX = 0;
mNextX = 0;
mMaxX = Integer.MAX_VALUE;
mScroller = new Scroller(getContext());
mGesture = new GestureDetector(getContext(), mOnGesture);
}
@Override
public void setOnItemSelectedListener(AdapterView.OnItemSelectedListener listener) {
mOnItemSelected = listener;
}
@Override
public void setOnItemClickListener(AdapterView.OnItemClickListener listener){
mOnItemClicked = listener;
}
@Override
public void setOnItemLongClickListener(AdapterView.OnItemLongClickListener listener) {
mOnItemLongClicked = listener;
}
private DataSetObserver mDataObserver = new DataSetObserver() {
@Override
public void onChanged() {
synchronized(HorizontalListView.this){
mDataChanged = true;
}
invalidate();
requestLayout();
}
@Override
public void onInvalidated() {
reset();
invalidate();
requestLayout();
}
};
@Override
public ListAdapter getAdapter() {
return mAdapter;
}
@Override
public View getSelectedView() {
//TODO: implement
return null;
}
@Override
public void setAdapter(ListAdapter adapter) {
if(mAdapter != null) {
mAdapter.unregisterDataSetObserver(mDataObserver);
}
mAdapter = adapter;
mAdapter.registerDataSetObserver(mDataObserver);
reset();
}
private synchronized void reset(){
initView();
removeAllViewsInLayout();
requestLayout();
}
@Override
public void setSelection(int position) {
//TODO: implement
}
private void addAndMeasureChild(final View child, int viewPos) {
LayoutParams params = child.getLayoutParams();
if(params == null) {
params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
}
addViewInLayout(child, viewPos, params, true);
child.measure(MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.AT_MOST),
MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.AT_MOST));
}
@Override
protected synchronized void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
if(mAdapter == null){
return;
}
if(mDataChanged){
int oldCurrentX = mCurrentX;
initView();
removeAllViewsInLayout();
mNextX = oldCurrentX;
mDataChanged = false;
}
if(mScroller.computeScrollOffset()){
int scrollx = mScroller.getCurrX();
mNextX = scrollx;
}
if(mNextX <= 0){
mNextX = 0;
mScroller.forceFinished(true);
}
if(mNextX >= mMaxX) {
mNextX = mMaxX;
mScroller.forceFinished(true);
}
int dx = mCurrentX - mNextX;
removeNonVisibleItems(dx);
fillList(dx);
positionItems(dx);
mCurrentX = mNextX;
if(!mScroller.isFinished()){
post(new Runnable(){
@Override
public void run() {
requestLayout();
}
});
}
}
private void fillList(final int dx) {
int edge = 0;
View child = getChildAt(getChildCount()-1);
if(child != null) {
edge = child.getRight();
}
fillListRight(edge, dx);
edge = 0;
child = getChildAt(0);
if(child != null) {
edge = child.getLeft();
}
fillListLeft(edge, dx);
}
private void fillListRight(int rightEdge, final int dx) {
while(rightEdge + dx < getWidth() && mRightViewIndex < mAdapter.getCount()) {
View child = mAdapter.getView(mRightViewIndex, mRemovedViewQueue.poll(), this);
addAndMeasureChild(child, -1);
rightEdge += child.getMeasuredWidth();
if(mRightViewIndex == mAdapter.getCount()-1) {
mMaxX = mCurrentX + rightEdge - getWidth();
}
if (mMaxX < 0) {
mMaxX = 0;
}
mRightViewIndex++;
}
}
private void fillListLeft(int leftEdge, final int dx) {
while(leftEdge + dx > 0 && mLeftViewIndex >= 0) {
View child = mAdapter.getView(mLeftViewIndex, mRemovedViewQueue.poll(), this);
addAndMeasureChild(child, 0);
leftEdge -= child.getMeasuredWidth();
mLeftViewIndex--;
mDisplayOffset -= child.getMeasuredWidth();
}
}
private void removeNonVisibleItems(final int dx) {
View child = getChildAt(0);
while(child != null && child.getRight() + dx <= 0) {
mDisplayOffset += child.getMeasuredWidth();
mRemovedViewQueue.offer(child);
removeViewInLayout(child);
mLeftViewIndex++;
child = getChildAt(0);
}
child = getChildAt(getChildCount()-1);
while(child != null && child.getLeft() + dx >= getWidth()) {
mRemovedViewQueue.offer(child);
removeViewInLayout(child);
mRightViewIndex--;
child = getChildAt(getChildCount()-1);
}
}
private void positionItems(final int dx) {
if(getChildCount() > 0){
mDisplayOffset += dx;
int left = mDisplayOffset;
for(int i=0;i<getChildCount();i++){
View child = getChildAt(i);
int childWidth = child.getMeasuredWidth();
child.layout(left, 0, left + childWidth, child.getMeasuredHeight());
left += childWidth + child.getPaddingRight();
}
}
}
public synchronized void scrollTo(int x) {
mScroller.startScroll(mNextX, 0, x - mNextX, 0);
requestLayout();
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
boolean handled = super.dispatchTouchEvent(ev);
handled |= mGesture.onTouchEvent(ev);
return handled;
}
protected boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
synchronized(HorizontalListView.this){
mScroller.fling(mNextX, 0, (int)-velocityX, 0, 0, mMaxX, 0, 0);
}
requestLayout();
return true;
}
protected boolean onDown(MotionEvent e) {
mScroller.forceFinished(true);
return true;
}
private OnGestureListener mOnGesture = new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onDown(MotionEvent e) {
return HorizontalListView.this.onDown(e);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
return HorizontalListView.this.onFling(e1, e2, velocityX, velocityY);
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
synchronized(HorizontalListView.this){
mNextX += (int)distanceX;
}
requestLayout();
return true;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
for(int i=0;i<getChildCount();i++){
View child = getChildAt(i);
if (isEventWithinView(e, child)) {
if(mOnItemClicked != null){
mOnItemClicked.onItemClick(HorizontalListView.this, child, mLeftViewIndex + 1 + i, mAdapter.getItemId( mLeftViewIndex + 1 + i ));
}
if(mOnItemSelected != null){
mOnItemSelected.onItemSelected(HorizontalListView.this, child, mLeftViewIndex + 1 + i, mAdapter.getItemId( mLeftViewIndex + 1 + i ));
}
break;
}
}
return true;
}
@Override
public void onLongPress(MotionEvent e) {
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View child = getChildAt(i);
if (isEventWithinView(e, child)) {
if (mOnItemLongClicked != null) {
mOnItemLongClicked.onItemLongClick(HorizontalListView.this, child, mLeftViewIndex + 1 + i, mAdapter.getItemId(mLeftViewIndex + 1 + i));
}
break;
}
}
}
private boolean isEventWithinView(MotionEvent e, View child) {
Rect viewRect = new Rect();
int[] childPosition = new int[2];
child.getLocationOnScreen(childPosition);
int left = childPosition[0];
int right = left + child.getWidth();
int top = childPosition[1];
int bottom = top + child.getHeight();
viewRect.set(left, top, right, bottom);
return viewRect.contains((int) e.getRawX(), (int) e.getRawY());
}
};
}
HorizontalListViewDemo.java
package com.sandy.demo;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
public class HorizontalListViewDemo extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listviewdemo);
HorizontalListView listview = (HorizontalListView) findViewById(R.id.listview);
listview.setAdapter(mAdapter);
}
private static String[] dataObjects = new String[]{ "Text #1",
"Text #2",
"Text #3","Text #4","Text #5","Text #6","Text #7","Text #8","Text #9","Text #10" };
private BaseAdapter mAdapter = new BaseAdapter() {
private OnClickListener mOnButtonClicked = new OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(HorizontalListViewDemo.this);
builder.setMessage("hello from " + v);
builder.setPositiveButton("Cool", null);
builder.show();
}
};
@Override
public int getCount() {
return dataObjects.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View retval = LayoutInflater.from(parent.getContext()).inflate(R.layout.viewitem, null);
TextView title = (TextView) retval.findViewById(R.id.title);
Button button = (Button) retval.findViewById(R.id.clickbutton);
button.setOnClickListener(mOnButtonClicked);
title.setText(dataObjects[position]);
return retval;
}
};
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
listviewdemo.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff"
>
<com.sandy.demo.HorizontalListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ddd"
/>
</LinearLayout>
viewitem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#fff"
>
<ImageView
android:id="@+id/image"
android:layout_width="150dip"
android:layout_height="150dip"
android:scaleType="centerCrop"
android:src="@drawable/icon"
/>
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:gravity="center_horizontal"
/>
<Button
android:id="@+id/clickbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:text="Click Me"
/>
</LinearLayout>
Click here to Download.
Thanks a lot. Lots of examples are there..but this is the most simple one and bug-free.
ReplyDeleteWelcome @Syamantak Basu. I put it all here because of everyone who required, they get easily demo and solve his/her problems.
ReplyDeletethank you so much
DeleteHow to set load more for horizontal listview?
DeleteThnxx... it's working very well :-)
DeleteHow to give space between listview items.plz tell me
ReplyDeleteyou need to create viewitem.xml like your requirements.
Deletemeans you just put margin in that .xml file and get space between two listdata.
This is a plagiary!! You are a FRAUD and plagiarist because you copy it completely textually from https://github.com/dinocore1 and didn't give any credit. At least you should be creative and change the methods name.
ReplyDeleteMaybe so. I saw the GitHub version first but because I couldn't load it into my Android Studio, I found this simple listing easier to handle. It's good either way. Thanks sandip.
DeleteHi, I would like to suggest you to put image like that we can have an view of how it is displaying
ReplyDeleteThanks for Suggestion, I do it ASAP...
DeleteHow i do set load more item with horizontal gridview?
Deletesandip, i'm getting error...
ReplyDelete12-18 05:45:44.007: E/AndroidRuntime(1324): FATAL EXCEPTION: main
12-18 05:45:44.007: E/AndroidRuntime(1324): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.hlv/com.example.hlv.HorizontalListView}: java.lang.InstantiationException: can't instantiate class com.example.hlv.HorizontalListView; no empty constructor
12-18 05:45:44.007: E/AndroidRuntime(1324): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
12-18 05:45:44.007: E/AndroidRuntime(1324): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
12-18 05:45:44.007: E/AndroidRuntime(1324): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-18 05:45:44.007: E/AndroidRuntime(1324): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
12-18 05:45:44.007: E/AndroidRuntime(1324): at android.os.Handler.dispatchMessage(Handler.java:99)
12-18 05:45:44.007: E/AndroidRuntime(1324): at android.os.Looper.loop(Looper.java:137)
12-18 05:45:44.007: E/AndroidRuntime(1324): at android.app.ActivityThread.main(ActivityThread.java:5103)
12-18 05:45:44.007: E/AndroidRuntime(1324): at java.lang.reflect.Method.invokeNative(Native Method)
12-18 05:45:44.007: E/AndroidRuntime(1324): at java.lang.reflect.Method.invoke(Method.java:525)
12-18 05:45:44.007: E/AndroidRuntime(1324): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-18 05:45:44.007: E/AndroidRuntime(1324): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-18 05:45:44.007: E/AndroidRuntime(1324): at dalvik.system.NativeStart.main(Native Method)
12-18 05:45:44.007: E/AndroidRuntime(1324): Caused by: java.lang.InstantiationException: can't instantiate class com.example.hlv.HorizontalListView; no empty constructor
12-18 05:45:44.007: E/AndroidRuntime(1324): at java.lang.Class.newInstanceImpl(Native Method)
12-18 05:45:44.007: E/AndroidRuntime(1324): at java.lang.Class.newInstance(Class.java:1130)
12-18 05:45:44.007: E/AndroidRuntime(1324): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
12-18 05:45:44.007: E/AndroidRuntime(1324): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
12-18 05:45:44.007: E/AndroidRuntime(1324): ... 11 more
you need to create constructor of HorizontalListView class.
Deletebecause in your error code tell me that.
12-18 05:45:44.007: E/AndroidRuntime(1324): Caused by: java.lang.InstantiationException: can't instantiate class com.example.hlv.HorizontalListView; no empty constructor
so, please create one empty constructor first.
Hi ,
ReplyDeletethis horizontal list view scrolling is not as smooth as it happens in Gallery or list view. please tell me how we can scroll smoothly .
when i put in inside scroll view it didn't appear why ?!!
ReplyDeleteBecause Scroll View is a one Control and It also contain the scroll property like this one. so, no need to use Scroll View as parent of this view.
DeleteDude, you forgot to mention that the original implementation is from dev-smart.
ReplyDeleteLink is here: http://www.dev-smart.com/archives/34
Yes, I think you has to link to original post and author.
Deletehow can i load more item in listview when i scroll to right of listview , you listview does't have onscroll listener
ReplyDeleteThis comment has been removed by the author.
DeleteI already put in HorizontalListView class.
Delete@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
synchronized(HorizontalListView.this){
mNextX += (int)distanceX;
}
requestLayout();
return true;
}
i want to add footer at the end of horizontallistview...pls tell how can i do it?
Deletehow do use onScroll in activity?
DeleteHi,How can I implement auto scroll in the above code
ReplyDeleteHo to set the gravity of this Horizontal listview as center of it's parent.
ReplyDeleteHi Sandip,
ReplyDeleteNice tutorial thanks
You are Welcome @Reena Nachare
DeleteHorizontalListView layout_height="wrap_content" does not work..
ReplyDeleteThanks alot for lovely tutorial , how to set list in bottom instead of top , i set the layout gravity in bottom but does not work??
ReplyDeleteplease help me ?
YOu need to use RelativeLayout instead of LinearLayout in "listviewdemo.xml"
Deletefile. and then put "android:layout_alignParentBottom="true" in "com.sandy.demo.HorizontalListView" control.
like below.
listviewdemo.xml
I don't want to change the position of images when it scrolls then tell me the portion of code to remove on it.
DeleteThanks for tutorial. but i have some problems. i have so much item row in list adapter , when i'm trying this horizontal LV , item rows (2nd,3rd, etc) always added in first row. Any help ? Thank you very much....
ReplyDeleteSource cant be downloaded..!!!
ReplyDelete@jigar jimsky Same problem......
ReplyDelete12-05 03:38:04.203: E/AndroidRuntime(20165): FATAL EXCEPTION: main
ReplyDelete12-05 03:38:04.203: E/AndroidRuntime(20165): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.horizontaldemo/com.example.horizontaldemo.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.horizontaldemo.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.horizontaldemo-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.horizontaldemo-1, /system/lib]]
12-05 03:38:04.203: E/AndroidRuntime(20165): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
12-05 03:38:04.203: E/AndroidRuntime(20165): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
12-05 03:38:04.203: E/AndroidRuntime(20165): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-05 03:38:04.203: E/AndroidRuntime(20165): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
12-05 03:38:04.203: E/AndroidRuntime(20165): at android.os.Handler.dispatchMessage(Handler.java:99)
12-05 03:38:04.203: E/AndroidRuntime(20165): at android.os.Looper.loop(Looper.java:137)
12-05 03:38:04.203: E/AndroidRuntime(20165): at android.app.ActivityThread.main(ActivityThread.java:5103)
12-05 03:38:04.203: E/AndroidRuntime(20165): at java.lang.reflect.Method.invokeNative(Native Method)
12-05 03:38:04.203: E/AndroidRuntime(20165): at java.lang.reflect.Method.invoke(Method.java:525)
12-05 03:38:04.203: E/AndroidRuntime(20165): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-05 03:38:04.203: E/AndroidRuntime(20165): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-05 03:38:04.203: E/AndroidRuntime(20165): at dalvik.system.NativeStart.main(Native Method)
12-05 03:38:04.203: E/AndroidRuntime(20165): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.horizontaldemo.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.horizontaldemo-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.horizontaldemo-1, /system/lib]]
12-05 03:38:04.203: E/AndroidRuntime(20165): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
12-05 03:38:04.203: E/AndroidRuntime(20165): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
12-05 03:38:04.203: E/AndroidRuntime(20165): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
12-05 03:38:04.203: E/AndroidRuntime(20165): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
12-05 03:38:04.203: E/AndroidRuntime(20165): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
12-05 03:38:04.203: E/AndroidRuntime(20165): ... 11 more
Nice Example Good Work...
ReplyDeletehey! how does this work in RTL?
ReplyDeleteHow to limit gesture for list only..not for complete page
ReplyDeleteIt was really nice and cool tut. thanks and keep it up!! Thumbs Up!!
ReplyDeletehow to put side arrows in horizontal list view???
ReplyDeleteHi,
ReplyDeleteCan you please let me know how to implement the setSelection Method?, if you have code for that can you please share that as well
Hi,
ReplyDeletecan you please implement the smoothScrollto and setSelection Methods.
Hi, I appreciate your work it helped me a lot
ReplyDeleteHow can get item value of the dataobjects when the button is clicked
ReplyDeletewhen i am scrolling listview the item get selected even if i do not want..i have used my own baseadapter and didnot used onclicklistener that is there in your base adapter
ReplyDeleteHorizontialListView how to work in android studio
ReplyDeleteplease compile lib please
I have to use a horizontal listview containing Images of a song playing below that horizontal listview in a Vertical listview conatining songs.. both are scrolling smoothly, How can I do that ?
ReplyDeletei am not able to download example..
ReplyDeletehow to scroll listview automaticaly continue
ReplyDeleteHorizontally list images and bind text using recyclerView. Check this tutorial... http://whats-online.info/science-and-tutorials/87/Android-tutorial-Horizontal-RecyclerView-with-images-and-text-example/
ReplyDeleteHello sandip. Thanks for this. It may well have had it origins elsewhere but your listing is the one that got me productively started on a problem I have had for some while. That was for a double row horizontal list view to place in the vertical centre of a protract screen on a Android. I managed to add the second row with little or no trouble and have added item locaters that place the desired item (eg the selected item) in the centre of the screen. Adjusting the size of my list presented to the horizontal list view proved to be trickier. In the end, I had to create a completely new Horizontal List View and make a few kludges in the onLayout(...) of the ViewGroup that holds the Horizontal List View. But, did I miss an easier way to resize my list and synchronize the Horizontal List View?
ReplyDeleteOh no no no no. My last didn't work. I just ran out memory. So my question is how do I reset my HorizontalListView to a supplied array of different length and varied data in. The array is just and ArrayList with different selections from the same basic source. This is built into BaseAsapter mAdapater via its overrides getView and getCount. When I change the array contents, I issue a HorizontalListView instance.refresh(mApapter) call. I added refresh to the HorizontalListView class as
ReplyDeletepublic synchronized void refresh(BaseAdapter baseAdapter) {
mScroller.forceFinished(true);
mDataChanged = true;
baseAdapter.notifyDataSetInvalidated();
}
This successfully makes the HorizontalListView display the new array correctly BUT the scroller and fling don't work. It appears its control variables are totally out of sync!! How do I remedy this.
Best content & valuable as well. Thanks for sharing this content.
ReplyDeleteApproved Auditor in DAFZA
Approved Auditor in RAKEZ
Approved Auditor in JAFZA
i heard about this blog & get actually whatever i was finding. Nice post love to read this blog
Approved Auditor in DMCC
Good luck & keep writing such awesome content.
ReplyDeleteVirgin Linseed Oil BP
flaxseed oil
Drift-Master
ReplyDeleteI feel astoundingly reeled to have https://playboyclothing.net/ seen your post. I thought about the most surprising and dazzling one. I'm incredibly happy to visit your post.
ReplyDeleteNice blog Juice Wrld Merch and absolutely outstanding. You can do something much better but i still say this perfect. Keep trying for the best.
ReplyDeleteMost of us know how to manage our finances using a smartphone app, however, some of us may find it challenging to use an extension. This misconception that using the extension is difficult will be dispelled in this section of the post as we go over how to download and install the MetaMask browser extension.
ReplyDeleteIf you are signing up for Informed delivery, a free and optional notification feature is provided by the Zelle login account. It allows you to digitally preview your letter and manage incoming packages all on the dashboard, here are the steps that you need to follow steps for zelle login
"Wow! This blog is a mind-expanding kanyewestmerch adventure! 🚀 Fascinating insights presented with crystal-clear clarity.🌟 I'm hooked and can't wait for the next knowledge boost! 💡📚"
ReplyDeleteTrès content de mon achat Corteiz. Le pull est agréable, bien taillé, et super stylé. Idéal pour l’hiver, il tient bien chaud. Un classique moderne.
ReplyDelete