滚动位置未保存在Grdiview中

时间:2016-11-05 06:28:05

标签: android scrollview android-view scroll-position

有人可以帮助我吗?我在GridView的滚动中遇到问题我做的是我在API中显示GridView中的前10个产品..现在我需要的是 - 当用户滚动并获取列表的最后位置时然后API再次点击页面值2 ..并且滚动显示从用户所在的当前位置..现在显示数据并且适配器更改其值..但是当新API命中时滚动显示在顶部...可以有谁请告诉我该怎么办?真的很赞赏请帮帮..

我的所作所为 -

        grid.setOnScrollListener(new AbsListView.OnScrollListener() {
        int last_item;

        @Override
        public void onScrollStateChanged(AbsListView view, int   scrollState) {

        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                             int visibleItemCount, int totalItemCount)      {


            int lastitem = firstVisibleItem + visibleItemCount;
            if(lastitem == totalItemCount){

                       HitApi();//This is the method where API hits with the new page value


            }


        }
    });

xml文件

       <?xml version="1.0" encoding="utf-8"?>
       <LinearLayout
       android:orientation="vertical"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"

       xmlns:android="http://schemas.android.com/apk/res/android">


        <GridView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/products_GridView"
        android:numColumns="auto_fit"
        android:listSelector="@android:color/transparent"
        android:verticalSpacing="1dp"
        android:horizontalSpacing="1dp"
        >
       </GridView>


        </LinearLayout>

3 个答案:

答案 0 :(得分:1)

好的,假设 ngOnInit() { this.getData(); } getData() { // this is async call and its unknown that how long this process would take to fetch data. this.DataService.getData(localStorage.getItem('token')) .subscribe( DataSet =>{ this.DataSet = DataSet; console.log(this.DataSet) //<<<<here it will print data to the console. }, error => console.error('Error: ' + <any>error), () => console.log('Completed!') ) } 使用新项更新hitApi(),那么该方法可能会在当前列表中调用ListView,这将导致问题,然后它将添加新东西;你必须要做的就是保持最后的位置是在.clear()方法之前添加它:

hitApi()

然后在调用int i = yourList.getFirstVisiblePosition(); View v = yourList.getChildAt(0); int t = 0; if(v!=null)v.getTop(); 方法后,设置旧位置:

hitApi()

答案 1 :(得分:0)

试试这个......

    /***
     * LodeMore Variables
     */
    private int lastVisibleItem, totalItemCount;
    private int visibleThreshold = 1;
    private int index = 1;
    private boolean loading;

 private void initLodeMoreAdapter()
    {

        if (recyclerView.getLayoutManager() instanceof GridLayoutManager)
        {

            final GridLayoutManager linearLayoutManager = (GridLayoutManager) rvResponse.getLayoutManager();
            rvResponse.addOnScrollListener(new RecyclerView.OnScrollListener()
            {

                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy)
                {
                    super.onScrolled(recyclerView, dx, dy);


                    totalItemCount = linearLayoutManager.getItemCount();
                    lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
                    if (!loading &&totalItemCount <= (lastVisibleItem + visibleThreshold))
                    {
                        // End has been reached
                        // Do something
            loading = true;
                        getResponseListing();
                    }
                }
            });
        }
    }

 private void getResponseListing()
    {
        if (utility.isNetworkAvailable())
        {
        API Call...

        adapter.notifyDataSetChanged();

        loading = false;
        }
    }

答案 2 :(得分:-1)

在适配器中尝试这样的操作以获得最后的可见位置

public class TestEventHandlers
{
    public void OpenMarket(Page page)
    {
        ChangeMarketState(page, "Open", "market@mail.com");
    }

    public void CloseMarket(Page page)
    {
        ChangeMarketState(page, "Close", "market@mail.com");
    }

    private static void SendEmailNotification(string subject,string body,string emailAddress)
    {
        var smtpClient = new SmtpClient();

        var message = new MailMessage();

        message.Subject = subject;
        message.Body = body;
        message.To.Add(new MailAddress(emailAddress));

        smtpClient.Send(message);
    }

    public void ChangeMarketState(Page page, string changeStateTo, string sendMailTo)
    {
        var Id = page.Request["MarketId"];
        if(Id != null)
        {
            var repository = new EntityRepository();
            IEntity market = repository.GetById(Id);

            if (market.state == changeStateTo)
            {
                if(changeStateTo == "Close")
                    throw new Exception("The market is already close!");
                else
                    throw new Exception("The market is not open!");
            }
            else
            {
                string currentMarketState = string.empty;
                string mailHeader = string.empty;
                if(changeStateTo == "Close")
                {
                   market.Close();
                   currentMarketState = market.ToString() + " has been closed.";
                   mailHeader = "market closed";
                }
                else
                {
                   market.Open();
                   currentMarketState = market.ToString() + " was open.";
                   mailHeader = "market open";
                }

                repository.SaveChangesTo(market);

                SendEmailNotification(mailHeader, currentMarketState, sendMailTo);
            }
        }
        else
        {
            throw new Exception("entityId can not be null");  
        }
    }
}

只是一个想法。不确定它是否有效。只是一个解决方法