5

I want to achieve Fig-1 but I am stuck with Fig-2 and not able to see full gridview as a header of Listview.

enter image description here

As you can see the gridview is not showing fully and hiding behind Listview in Fig-2

Gridview xml :

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

<GridView
    android:id="@+id/gridview"
    android:numColumns="2"
    android:stretchMode="columnWidth"
    android:cacheColorHint="#ffffffff"
    android:gravity="center"
    android:verticalSpacing="5dp"
    android:horizontalSpacing="5dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</RelativeLayout>

Listview xml :

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

<ListView
    android:id="@+id/listview"
    android:scrollbars="vertical"
    android:background="#fff"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

</ListView>

</RelativeLayout>

Fragment code :

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


    view=inflater.inflate(R.layout.fragmentpage_layout, null);

    listView=(ListView)view.findViewById(R.id.listview);

    header=inflater.inflate(R.layout.gridview_layout,null);

    gridView=(GridView)header.findViewById(R.id.gridview);
    listView.addHeaderView(header);
    gridViewAdapter=new CustomGridViewAdapter(getActivity(),images,toptexts, bottomtexts);
    listViewAdapter=new CustomListViewAdapter(getActivity(),images,toptexts,bottomtexts);

    gridView.setAdapter(gridViewAdapter);

    listView.setAdapter(listViewAdapter);


   return view;
}

Thanks In Advance.

Ashish Shukla
  • 1,027
  • 16
  • 36
  • 2
    Provide the xml so we can help. – ivan Jan 25 '16 at 05:50
  • its because of you have mentioned height just minimize height and check – Pavya Jan 25 '16 at 05:50
  • I have added xml files – Ashish Shukla Jan 25 '16 at 06:03
  • can you show your code where you are inflating header view and adding it to list vew. – Developer Jan 25 '16 at 06:14
  • I have edited my question. – Ashish Shukla Jan 25 '16 at 06:26
  • Try with this: ViewGroup header = (ViewGroup) inflater.inflate(R.layout.gridview, listView, false); listView.addHeaderView(header, null, false); – Developer Jan 25 '16 at 06:36
  • @Developer I have tried that too, it does not works. – Ashish Shukla Jan 25 '16 at 06:42
  • http://stackoverflow.com/questions/17629542/how-to-inflate-another-layout-inside-getview-of-gridview-adapter-in-android – Amit Vaghela Jan 25 '16 at 06:53
  • Is there any problem if you use that in a single layout that is gridview and listiview ? @AshishShukla – Amit Vaghela Feb 02 '16 at 09:37
  • try adding the header view after setting the adapter to gridview and before listview and also change gridview relativelayout height to wrap_content – JAAD Feb 02 '16 at 09:47
  • @AshishShukla For your design either you can use layout_weight property in vertical linear layout or you can give dynamic height to list view so it will not overlap your grid content. i haven't check this code but you can try http://stackoverflow.com/a/19117325/2128166 – Wasim K. Memon Feb 02 '16 at 10:16
  • @androidnoobdev the code is for listview, what about gridview ? – Ashish Shukla Feb 02 '16 at 10:19
  • @AshishShukla check out this code. hope it will help. it is for layout and apply dynamic height also if this won't work. http://pastebin.com/sPZ4Ercn – Wasim K. Memon Feb 02 '16 at 10:38
  • @androidnoobdev not this code, i want the code for setGridViewHeightBasedOnChildren() for gridview – Ashish Shukla Feb 02 '16 at 11:47
  • @AshishShukla why you want to set grid view height also ? it will take space as much it require and then you give dynamic height to listview so it will fit into screen. i hope it will work. and we are not here to provide you full code man do some googling... – Wasim K. Memon Feb 02 '16 at 13:46
  • @androidnoobdev i just want the replacement function for getDividerHeight() for GridView. – Ashish Shukla Feb 03 '16 at 06:18
  • @AshishShukla Maybe my question is stupid but there's something I don't understand very well. Do you want to handle scrolling of GridView and ListView as if it is a single component (so a single scrolling) or you just need to have GridView fixed and ListView scrolling? – andrea.petreri Feb 03 '16 at 06:25
  • You are correct on your first question i.e single scrolling – Ashish Shukla Feb 03 '16 at 06:31
  • @AshishShukla ok, is it mandatory for you to use GridView and ListView or is it ok to migrate to RecyclerView? With RecyclerView I think it's much easier to get outcome you need. – andrea.petreri Feb 03 '16 at 06:47
  • I was using Recyclerview but bcoz of the unanswered Marshalling exceptions i switched to Listview – Ashish Shukla Feb 03 '16 at 08:49
  • @AshishShukla uhm ok... I have a solution ready for you with RecyclerView. Of course I can share with you but I want to be sure this could be a valid solution for you. I think in any case that using GridView together with ListView could be difficult due to the fact that they both implement scrolling, so merging them could lead to weird results. – andrea.petreri Feb 03 '16 at 08:57
  • but i have seen two listview together working properly in single layout using single scrolling – Ashish Shukla Feb 03 '16 at 09:09
  • @AshishShukla well, ok... I just mean that this is not the approach I would follow for implementing this scenario. RecyclerView offers proper features for handling situations like yours. But that's because I was asking if you expect an answer regardless of used components or if you want this to be implemented with GridView and ListView. BTW can I ask you the link to the question about marshalling? – andrea.petreri Feb 03 '16 at 09:49
  • there are marshalling questions for other viewgroups but not for Recyclerview thats why i switched to Listview – Ashish Shukla Feb 03 '16 at 10:05
  • @AshishShukla uhm ok... I'm asking because I've implemented your scenario with RecyclerView but I'm not doing any marshalling. So I'm wondering if I'm missing something. Well... if you want to go deeper into RecyclerView solution we can even continue in chat of course. If you want to keep GridView and ListView I need more time for understanding how to effectively combining them, in particular for avoiding eventual performance issues. – andrea.petreri Feb 03 '16 at 10:10
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/102455/discussion-between-ashish-shukla-and-thetonrifles). – Ashish Shukla Feb 03 '16 at 10:13
  • 1
    @AshishShukla get after calling setAdater for gridview calculate height of gridview and set as height in layout param – Jaiprakash Soni Feb 04 '16 at 05:26
  • @Jaiprakash Soni what to replace for getDividerHeight() in Listview for Gridview while calculating divider height for gridview – Ashish Shukla Feb 04 '16 at 05:39
  • @AshishShukla did you checked code I shared with you in chat? – andrea.petreri Feb 05 '16 at 06:53
  • 1
    Actually you don't need to calculate the size manually you can do it by override the onmeasure method of grid view . Please check the link for more information . http://stackoverflow.com/a/22727503/3257178 – Arun Antoney Feb 07 '16 at 05:09
  • @Ashish Shukla Have yoy find solution for this I am having same issue please help – Sagar Oct 06 '17 at 12:03

4 Answers4

2

Hey Guys I got my Answer using Gridview as a Header of Listview.

I just Calculated the Divider Height for Gridview using below method and it worked perfectly as I wanted.

   public static void setDynamicHeightGridView(GridView mListView,String oddeven) {
        ListAdapter mListAdapter = mListView.getAdapter();
        if (mListAdapter == null) {
            return;
        }
        int height = 0;
        int desiredWidth = View.MeasureSpec.makeMeasureSpec(mListView.getWidth(), View.MeasureSpec.UNSPECIFIED);


        for(int i = 0; i < mListAdapter.getCount(); i++){
            View listItem = mListAdapter.getView(i, null, mListView);
            listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
            height += listItem.getMeasuredHeight();
            itemHeight=listItem.getMeasuredHeight()/3;
        }

        ViewGroup.LayoutParams params = mListView.getLayoutParams();

        if(oddeven.equals("odd")){
            if(mListAdapter.getCount()>=5){
                int count=((mListAdapter.getCount()-5)/2) + 1;
                params.height = ((height - (height / 3)) - (itemHeight * count)) + 20 + (count * 5);

            }else{
                params.height = height - (height / 3) + 20;
            }

        }else if(oddeven.equals("even")) {
            params.height = height/2 + 20;
        }


        mListView.setLayoutParams(params);
        mListView.requestLayout();
    }

Edited :

Finally the Exact Answer :

For Even number of views set :

params.height = height/2 + 20;

For Odd number of Views set :

params.height = ((height - (height / 3)) - (itemHeight * count)) + 20 + (count * 5);

where :

  • I have used 5 as the number for comparision bcoz after this value of Adapters Count the variation in the space between gridview and listview is increasing with fixed values.

  • For the space before 5 is handled in the else part.

  • itemHeight is the incremental value with which the space is increasing

  • 20 is the margin space between gridview and listview

  • (count x 5) is the value for managing the margin as the elements increases.

bcoz it was giving me double the space for Gridview height above Listview for Even number of views

and Gridview + half it's height space for Odd number of views

Hope It Helps :)

Ashish Shukla
  • 1,027
  • 16
  • 36
  • You will have to set height just like you did as a gridview is scrollable and even the listview is scrollable. So in future whenever we try to put a scrollable view inside a listview or any other scrollable view, we need to set the height of the inner view irrespective of the orientation. – Vaibhav Sharma Feb 07 '16 at 13:57
0

Try this way , Taking your Grid and List in single layout and using android:weightSum to differentiate so that it will be applicable in all devices ,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="3dp"
    android:weightSum="2">

    <GridView
        android:id="@+id/gridview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:cacheColorHint="#ffffffff"
        android:gravity="center"
        android:numColumns="2"
        android:stretchMode="columnWidth" />

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#fff"
        android:scrollbars="vertical" />

</LinearLayout>

Fragment code :

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    view=inflater.inflate(R.layout.fragmentpage_layout, null);

    listView=(ListView)view.findViewById(R.id.listview);
    gridView=(GridView)header.findViewById(R.id.gridview);

    //your code

    gridView.setAdapter(gridViewAdapter);
    listView.setAdapter(listViewAdapter);


   return view;

}

OR

If you want the whole gridview to be displayed fully first and below it should be the listview and when you scroll the gridview should go upwards and the listview should follow it behind as in Fig-1

Checkout this Awesome-MaterialDesign library with simple explaination.As shown in below figure

enter image description here

Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
0

When you want to inflate your GridLayout also reference your listView With something like this :

header = LayoutInflater.from(getActivity()).inflate(R.layout.your_header, mListView, false);

And one more things : pass your relativeLayout instead of your GrideLayout as header.

Amir
  • 16,067
  • 10
  • 80
  • 119
0

try adding android:layout_marginTop="some_value" in the RelativeLayout of the ListView

inkedTechie
  • 684
  • 5
  • 13