在Android中将List滑到底部时ListView上升

时间:2016-03-14 06:37:33

标签: android listview android-adapter

您好我是android的新手并且正在进行列表视图演示,我最近发现了一个奇怪的列表视图行为,当我向下滚动它强有力地上升时,有人能告诉我这个行为背后的原因应该是什么我在这里发布我的代码。

活性

echo "<td>".$records['Content_short']."  <a href='article.php?id=' " . $records['ID']. ">Read More...</a></td>";

适配器

package one.tusk.stush.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.company.stush.R;

import java.util.List;

import one.tusk.stush.SearchPostActivity;
import one.tusk.stush.adapters.TimelineAdapter;
import one.tusk.stush.connect.Post;
import one.tusk.stush.loaders.SearchPostsLoader;
import uk.co.senab.actionbarpulltorefresh.library.ActionBarPullToRefresh;
import uk.co.senab.actionbarpulltorefresh.library.PullToRefreshLayout;
import uk.co.senab.actionbarpulltorefresh.library.listeners.OnRefreshListener;

public class SearchPostsFragment extends BaseFragment implements LoaderManager.LoaderCallbacks<List<Post>>, OnRefreshListener, OnScrollListener {

    public static android.widget.PopupMenu popupMenu;
    private int index = -1;
    private int top = 0;
    TimelineAdapter mTimelineAdapter;
    SearchPostsLoader mTimelineLoader;
    PullToRefreshLayout mPullToRefreshLayout;
    ListView mListViewTimeline;
    ProgressBar mProgressBarLoading;
    TextView mTextViewNoItems;
    ImageView im;
    Menu mm;
    int total;

    int lastPosition;
    int lastTop;
    String search;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        search = getArguments().getString("search");

        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_timeline_other, container, false);
        setHasOptionsMenu(false);
        return rootView;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setHasOptionsMenu(false);
    }

    public void onResume () {

        super.onResume();
        getActivity().invalidateOptionsMenu();

        mListViewTimeline.setSelectionFromTop(
                lastPosition,
                lastTop
        );


//      if(index!=-1){
//          mListViewTimeline.setSelectionFromTop(index, top);
//        }
        setHasOptionsMenu(false);



    }

    @Override
    public void onStop() {
        super.onStop();


        lastPosition = mListViewTimeline.getFirstVisiblePosition();
        View v = mListViewTimeline.getChildAt(0);
        lastTop = (v == null) ? 0 : v.getTop();

//       lastPosition = mListViewTimeline.getSelectedItemPosition();
//       int lastPositionInGroup = lastPosition - mListViewTimeline.getFirstVisiblePosition();
//       lastTop = mListViewTimeline.getChildAt( lastPositionInGroup ).getTop();
    }


    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        getActivity().getMenuInflater().inflate(R.menu.search_post, menu);
        //MenuItem refreshItem = menu.findItem(R.id.action_edit);
        //refreshItem.setVisible(true);

        //inflater.inflate(R.menu.fragment_profile_menu, menu);
    }



    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        if (id == R.id.search22) {
            //   Log.d("MyApp", "action bar clicked");


            Intent intent = new Intent(getActivity(), SearchPostActivity.class);
            intent.putExtra("View_mode", true);
            startActivity(intent);

        }

        return super.onOptionsItemSelected(item);
    }


    @Override
    public void onActivityCreated (Bundle savedInstanceState) {

        super.onActivityCreated(savedInstanceState);
        total = 0;
        mPullToRefreshLayout = (PullToRefreshLayout) getActivity().findViewById(R.id.ptr_layout);
        ActionBarPullToRefresh.from(getActivity()).allChildrenArePullable().listener(this).setup(mPullToRefreshLayout);

        mProgressBarLoading = (ProgressBar) getActivity().findViewById(R.id.progressBarLoading);
        mTextViewNoItems = (TextView) getActivity().findViewById(R.id.textViewNoItems);

        mTimelineAdapter = new TimelineAdapter(getActivity());



        mListViewTimeline = (ListView) getActivity().findViewById(R.id.listViewTimeline);
        mListViewTimeline.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
        mListViewTimeline.setEmptyView(mProgressBarLoading);
        mListViewTimeline.setAdapter(mTimelineAdapter);

        mListViewTimeline.setOnScrollListener(this);

        //  mListViewTimeline.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
        mListViewTimeline.setVerticalScrollBarEnabled(false);
        getLoaderManager().initLoader(0, null, this);

    }

    @Override
    public Loader<List<Post>> onCreateLoader(int id, Bundle args) {

        mTimelineLoader = new SearchPostsLoader(getActivity(), mListViewTimeline, search);
        setHasOptionsMenu(false);
        return mTimelineLoader;
    }

    @Override
    public void onLoadFinished(Loader<List<Post>> arg0, List<Post> data) {

        mTimelineAdapter.setData(data);
        mTimelineAdapter.notifyDataSetChanged();


        mPullToRefreshLayout.setRefreshComplete();
        mTextViewNoItems.setVisibility(View.VISIBLE);
        mProgressBarLoading.setVisibility(View.INVISIBLE);
        mListViewTimeline.setEmptyView(mTextViewNoItems);

    }


    @Override
    public void onLoaderReset(Loader<List<Post>> arg0) {

        mTimelineAdapter.setData(null);
        setHasOptionsMenu(false);
    }

    @Override
    public void onRefreshStarted(View view) {

        getLoaderManager().destroyLoader(0);
        getLoaderManager().initLoader(0, null, this);
        mTextViewNoItems.setVisibility(View.INVISIBLE);
        mProgressBarLoading.setVisibility(View.VISIBLE);
        mListViewTimeline.setEmptyView(mProgressBarLoading);


    }

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

        if (firstVisibleItem + visibleItemCount >= totalItemCount && visibleItemCount != 0) {
            if (mTimelineLoader.loadMore() && !mTimelineLoader.loading()) {
                total = totalItemCount - 1;
                lastPosition = view.getFirstVisiblePosition();
                View v =  view.getChildAt(0);
                lastTop = (v == null) ? 0 : v.getTop();
                mTimelineLoader.onContentChanged();

            }


        }


    }

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

    }

    public void searchPost(String query)
    {



        search  = query;
        lastPosition = 0;
        lastTop = 0;
        mTimelineAdapter.clear();
        // mTimelineLoader.loadInBackground();
        Log.d("12","UPDATE");
        getLoaderManager().restartLoader(0, null, this);

    }
}

1 个答案:

答案 0 :(得分:1)

你可以试试这个,

更改您的代码

            if(results.size() > 0){
                SearchAdapter searchAdapter = new SearchAdapter(SearchActivity.this);
                searchAdapter.setData(results);
                mListViewSearchUsers.setAdapter(searchAdapter);
                textNotFound.setVisibility(View.GONE);
            }else {
                textNotFound.setVisibility(View.VISIBLE);
                textNotFound.setText("No users found");
            }

            if(results.size() > 0){
                SearchAdapter searchAdapter = new SearchAdapter(SearchActivity.this);
                   if (searchAdapter== null) { 
                        searchAdapter.setData(results);
                        mListViewSearchUsers.setAdapter(searchAdapter);
                   } else { 
                 searchAdapter.setData(results); 
                 mListViewSearchUsers.notifyDatasetChange();
               }
                textNotFound.setVisibility(View.GONE);
            }else {
                textNotFound.setVisibility(View.VISIBLE);
                textNotFound.setText("No users found");
            }

更改适配器中的一些代码(注释一些代码)

 public void setData(List<User> data) {
   // clear();
    if (data != null) {
        addAll(data);
       // notifyDataSetChanged();
    }
    }

更新:(通过此行传达解决问题)

mListViewTimeline.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);

我希望这会对你有所帮助。