Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Horizontal ScrollView in List View Item Android

I have a list view with 20 rows and I want to setup a horizontal scrollview for every row item in list view, as each row contains more than one item.

Here is my code :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <HorizontalScrollView
        android:id="@+id/hor_scroll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none" >

        <LinearLayout
            android:id="@+id/mainLinear"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
        </LinearLayout>
    </HorizontalScrollView>

</RelativeLayout>

Inner Row Layout which is to be replicated in a row any number of times

<ImageView
    android:id="@+id/icon"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_marginRight="6.0dip"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:gravity="center_vertical"
    android:text="Example Value"
    android:textAppearance="?android:textAppearanceMedium" />

BaseAdapter

public class HorizontalListViewAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<ArrayList<DwivediJi>> dataSet;


    public HorizontalListViewAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return 20;
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.horizontal_list_item, null,false);

        LinearLayout mainLinnerLayout=(LinearLayout)convertView.findViewById(R.id.mainLinear);

         for (int i = 0; i <5; i++) {
             View additionView = inflater.inflate(R.layout.inner_layout_file, null,false);
             LinearLayout innerLinnerLayout=(LinearLayout)additionView.findViewById(R.id.inner_layout);
             mainLinnerLayout.addView(innerLinnerLayout);
        } 
        return convertView;
    }

    class ViewHolder {
        TextView tv_titleExample;
        HorizontalScrollView hzView;
        LinearLayout linear_layout,main_linear_layout;
    }
}

My Problem

Look at the screenshot attached. My problem is that more than one view is showing at one time in each row.

I want that only one view should show to the user at one time and for the rest all the user has to do is swipe left to right or right to left.

enter image description here

like image 608
Gaurav Arora Avatar asked Nov 14 '13 07:11

Gaurav Arora


People also ask

Can ListView be horizontal?

Even Google confirms that the only way for horizontal listview is Gallery.

How do you make a ListView scroll horizontal?

To scroll a Flutter ListView widget horizontally, set scrollDirection property of the ListView widget to Axis. horizontal. This arranges the items side by side horzontally.

What is the difference between ScrollView and horizontal ScrollView in Android?

ScrollView and HorizontalScrollView has same attributes, the only difference is scrollView scroll the child items in vertical direction while horizontal scroll view scroll the child items in horizontal direction.


1 Answers

Note: Not an ideal solution, but should provide what you want.. Another Note: This may make your listview slightly janky, depending on the layout-complexity

@Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.horizontal_list_item, null,false);

        LinearLayout mainLinnerLayout=(LinearLayout)convertView.findViewById(R.id.mainLinear);

         for (int i = 0; i <5; i++) {
             View additionView = inflater.inflate(R.layout.inner_layout_file, null,false);
             LinearLayout innerLinnerLayout=(LinearLayout)additionView.findViewById(R.id.inner_layout);

             // If the width varies for each innerLinnerLayout, then remove the if block & always calculate padding value
             // padding is an integer initialized to -1 in the constructor
             if (padding == -1) {
                 int width = context.getResources().getDisplayMetrics().widthPixels;
                 innerLinnerLayout.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
                 padding = width - additionView.getMeasuredWidth();
             }
             // I've set padding to right only, but you could center it by giving left and right padding of value=(padding/2)
             innerLinnerLayout.setPadding(0, 0, padding, 0);
             mainLinnerLayout.addView(innerLinnerLayout);
        } 
        return convertView;
    }
like image 139
Kiran Parmar Avatar answered Oct 29 '22 04:10

Kiran Parmar