Android ListView to horizontal tabs

I have a ListView

<ListView
    android:id="@+id/group"
    android:layout_width="264dp"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"/>

ListView gvGroup = ((ListView)findViewById(R.id.group));
    gvGroup.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
            details=fManager.getPageItems(fMenu,(int) id);
            ((BaseAdapter)gvDetail.getAdapter()).notifyDataSetChanged();
        }
    });
    gvGroup.setAdapter(new GroupListAdapter());
    gvGroup.postDelayed(new Runnable() {
        public void run() {
            gvGroup.setItemChecked(0, true);
        }
    }, 50);

and the adapter is

private final class GroupListAdapter extends BaseAdapter{
    public int getCount() {return groups!=null?groups.size():0;}
    public Object getItem(int position) {return groups.get(position);}
    public long getItemId(int position) {return groups.get(position).Id;}
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView tv=(TextView) convertView;
        if(tv==null){
            float size_font_dp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 28, getResources().getDisplayMetrics());
            float width_dp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 72, getResources().getDisplayMetrics());
            tv = new TextView(MyClass.this);
            tv.setBackgroundResource(R.drawable.list_selector);
            tv.setTextSize(size_font_dp);tv.setTypeface(Typeface.DEFAULT_BOLD);
            tv.setSingleLine();tv.setGravity(Gravity.CENTER_VERTICAL);
            AbsListView.LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,(int)(width_dp));              
            tv.setLayoutParams(lp);
        }
        tv.setText(((FPage)getItem(position)).Name);
        return tv;
    }
}

My problem is that I need to transform this ListView in a horizontal one, that can be scrolled. I've tried to change my ListView to a android.support.v4.view.ViewPager but the same adapter is not working. Is there a way to change the ListView that act as a TabControl to a horizontal one?

I'm new in Android programming so please be gentle :)

1 answer

  • answered 2017-10-11 10:10 Sneha Sarkar

    You can get your desired result using RecyclerView. You can find an example of RecyclerView from this link: http://www.androhub.com/android-staggered-and-horizontal-recyclerview/

    To create a horizontal list with RecyclerView, you might do something like this:

     final LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
    
        final RecyclerView myList = (RecyclerView) findViewById(R.id.recycler_view);
        myList.setLayoutManager(layoutManager);