Horizontal ScrollView in List View Item Android

隐身守侯 提交于 2019-11-28 11:32:19

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

The problem is with your adapter as:

  1. getCount() from your adapter must return the total number from list. Returning 20 is not valid in your context - and you seem to have 20 items in your list. You should return dataSet.size();
  2. getItem() should return the item from the model data structure, in this case:

Below

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return dataSet.get(position);
}

Also, your getView method must return the view that displays the model data at given at position parameter. Returning a ViewGroup with non-meaningfull dummy data is what you currently have. You should get the ArrayList<DwivediJi> from parameter position (through dataSet.get(position)) and construct/inflate a View that displays properly this data structure item.

Change inner layout width to match_parent. This should help you

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <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" />
</LinearLayout>

Seems you are adding your inner layout in this LinearLayout

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

Did you try changing its width to match_parent too??

Santosh Kathait

Use scrollview in xml layout and then dynamically create HorizontalScrollView inside for loop:

example :

for(int i.......) //number of HorizontalScrollView needed
{
    HorizontalScrollView mainlinear = new HorizontalScrollView(
                getApplicationContext()); 
    for(int i.......) //number of items wants to add in  HorizontalScrollView 

    {
        // adding any widgets as per your requirements
    }
    mainlinear.addView(Widgetname);
    scroll.addView(mainlinear);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!