0

I am creating a new project where I am using a listview inside a scroll view.But I got an issue where when I open this page by default it goes to the bottom of the page where I set my listview. But my requirement is that When I open the page it should show the top of the page..pls help me..

my xml

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

tools:context="com.example.test.Product_description"
>
<!--android:background="@drawable/bg_products"-->

<!-- TODO: Update blank fragment layout -->

<ScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="100dp"
    android:layout_marginTop="10dp"
    android:fillViewport="false" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:background="#ccffffff"
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#ffffff"
            android:orientation="horizontal" >

            <ImageView
                android:id="@+id/iv_proDesc_mainImage"
                android:layout_width="fill_parent"
                android:layout_height="250dp"
                 />
           <!-- android:background="@drawable/to_be_deleted_1"-->

            <TextView
                android:id="@+id/tv_proDesc_price"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"

                android:gravity="center"
                android:text="$ 9.95"
                android:textColor="#ffffff"
                android:textSize="28dp" />
            <!--android:background="@drawable/bg_price"-->
        </RelativeLayout>

        <TextView
            android:id="@+id/tv_proDesc_productName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="20dp"
            android:layout_marginTop="20dp"
            android:text="Premium Cherry Soup"
            android:textColor="#d58001"
            android:textSize="26sp" />

        <TextView
            android:id="@+id/tv_proDesc_productDesc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&apos;s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book." />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            android:layout_marginTop="20dp"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/btn_proDesc_wishlist"
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_gravity="center"
                android:layout_weight="2"

                android:paddingLeft="10dp"
                android:text="Wishlist"
                android:textColor="#ffffff" />
           <!-- android:background="@drawable/btn_wishlist"
            android:drawableLeft="@drawable/heart"-->

            <Button
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_weight="1"
                android:background="#00ffffff" />

            <Button
                android:id="@+id/btn_proDesc_addtocart"
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_gravity="left|center_vertical"
                android:layout_weight="2"

                android:drawablePadding="5dp"
                android:includeFontPadding="true"
                android:paddingLeft="10dp"
                android:text="Add to Cart"
                android:textColor="#ffffff" />
            <!--android:background="@drawable/btn_addtocart"
            android:drawableLeft="@drawable/cart"-->
        </LinearLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:autoText="false"
            android:text="Reviews"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#000000"
            android:textIsSelectable="true"
            android:translationZ="10dp" />

        <RatingBar
            android:id="@+id/rB_proDesc_rating"

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_margin="10dp"
            android:isIndicator="true"
            android:numStars="6"
            android:rating="4"
            android:stepSize="1" />
        <!--style="@style/foodRatingBar"-->

        <LinearLayout
            android:id="@+id/lL_proDesc_userReviews"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:orientation="vertical" >
        </LinearLayout>

        <ListView
            android:id="@+id/listcomment"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:transcriptMode="alwaysScroll"
            >

            </ListView>


    </LinearLayout>
</ScrollView>

Sarath vs
  • 1
  • 1

4 Answers4

1

If you want to use ListView inside the scrollview. That means you have to give height of listview after set adapter

For Exe

Listview.setAdapter(adapter);
setListViewHeightBasedOnChildren(listview, adapter);

public void setListViewHeightBasedOnChildren(ListView listView,StaffManagerJobDetailListAdapter staffmgrJobDetailsAdapter) {
    if(staffmgrJobDetailsAdapter==null){
        return;
    }       
    int totalHeight = 0;
    int items = staffmgrJobDetailsAdapter.getCount();
    if(items==0){
        totalHeight = 0;
    }else{
        totalHeight = (int) (getResources().getDimension(R.dimen.margin_50dp)*(items-candidateJobProfileDetails.size()));

    }
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight+(listView.getDividerHeight() * (items - 1));
    listView.setLayoutParams(params);
    listView.setFocusable(true);

}
samsad
  • 1,241
  • 1
  • 10
  • 15
0

Use only ListView instead. You can add top part of your xml to the header of ListView.

First, create header.xml with top part of your xml without ScrollView and ListView.

Second, in my.xml remain only ListView.

Then in your activity's onCreate() write down follow:

setContentView(R.layout.my);
ListView listView = (ListView) findViewById(R.id.listcomment);
LayoutInflater inflater = getLayoutInflater();
View header = inflater.inflate(R.layout.header, listView, false);
listView.addHeader(header);
CommentAdapter adapter = new CommentAdapter();
listView.setAdapter(adapter);
Sergei Vasilenko
  • 2,313
  • 2
  • 27
  • 38
0

If you want that when you open the page, the ScrollView page should show top of page, then you should programmatically scroll to top of page when page resumes.

ScrollView mainScrollView = (ScrollView) findViewById(R.id.scrollView);
mainScrollView.fullScroll(ScrollView.FOCUS_UP);
Kushal
  • 8,100
  • 9
  • 63
  • 82
0

Try to use ExpandableHeightListView instead of ListView.

Kishan Vaghela
  • 7,678
  • 5
  • 42
  • 67