Elie Daher
Elie Daher

Reputation: 390

How to create an Horizontal List View inside of a Vertical List View?

I have succeed to add a List View inside of a vertical List View. My Problem is with the horizontal list view. Can somebody explain to me how I should create an horizontal ListView? Should i switch to a Recycler View to create an horizontal List ? Or I stick with a ListView?

Thank you for your help !

Upvotes: 2

Views: 841

Answers (2)

Nowshad
Nowshad

Reputation: 294

you need to just inflate layout inside getView if you had still issues then look at this Answer.

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

Upvotes: 0

Mahendra Chhimwal
Mahendra Chhimwal

Reputation: 1818

Use a RecyclerView with Horizontal LinearLayout as a item view of vertical RecyclerView with Vertical LinearLayoutManager. Yeah if you are creating your views right now from the scratch, always use Recycler View instead of ListView onwards.

Upvotes: 2

Related Questions