Back to project page twitterListView.
The source code is released under:
This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a co...
If you think the Android project twitterListView listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * Copyright 2012 Google Inc.//from w w w . j a va 2 s. c om * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.amalbit; import java.util.ArrayList; import android.annotation.SuppressLint; import android.content.Context; import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; import android.support.v4.app.ListFragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.ViewTreeObserver; import android.view.animation.TranslateAnimation; import android.widget.AbsListView; import android.widget.AbsListView.OnScrollListener; import android.widget.BaseAdapter; import android.widget.LinearLayout; import android.widget.TextView; import com.amalbit.twitterlistview.R; import com.amalbit.view.PullToRefreshListView; import com.amalbit.view.PullToRefreshListView.OnRefreshListener; public class BottomFragment extends ListFragment { private PullToRefreshListView mListView; private LinearLayout mQuickReturnView; private int mQuickReturnHeight; private static final int STATE_ONSCREEN = 0; private static final int STATE_OFFSCREEN = 1; private static final int STATE_RETURNING = 2; private int mState = STATE_ONSCREEN; private int mScrollY; private int mMinRawY = 0; private TranslateAnimation anim; ArrayList<String> alTweets; ListAdapter adapter; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.footer_fragment, null); mQuickReturnView = (LinearLayout) view.findViewById(R.id.footer); return view; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); mListView = (PullToRefreshListView) getListView(); alTweets = new ArrayList<String>(); for(int i = 0; i < 50; i++) alTweets.add("Tweet Number "+i); adapter = new ListAdapter(); mListView.setAdapter(adapter); ((PullToRefreshListView) getListView()).setOnRefreshListener(new OnRefreshListener() { @Override public void onRefresh() { // Do work to refresh the list here. new GetDataTask().execute(); } }); mListView.getViewTreeObserver().addOnGlobalLayoutListener( new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { mQuickReturnHeight = mQuickReturnView.getHeight(); mListView.computeScrollY(); } }); mListView.setOnScrollListener(new OnScrollListener() { @SuppressLint("NewApi") @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { mScrollY = 0; int translationY = 0; if (mListView.scrollYIsComputed()) { mScrollY = mListView.getComputedScrollY(); } int rawY = mScrollY; switch (mState) { case STATE_OFFSCREEN: if (rawY >= mMinRawY) { mMinRawY = rawY; } else { mState = STATE_RETURNING; } translationY = rawY; break; case STATE_ONSCREEN: if (rawY > mQuickReturnHeight) { mState = STATE_OFFSCREEN; mMinRawY = rawY; } translationY = rawY; break; case STATE_RETURNING: translationY = (rawY - mMinRawY) + mQuickReturnHeight; System.out.println(translationY); if (translationY < 0) { translationY = 0; mMinRawY = rawY + mQuickReturnHeight; } if (rawY == 0) { mState = STATE_ONSCREEN; translationY = 0; } if (translationY > mQuickReturnHeight) { mState = STATE_OFFSCREEN; mMinRawY = rawY; } break; } /** this can be used if the build is below honeycomb **/ if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB) { anim = new TranslateAnimation(0, 0, translationY, translationY); anim.setFillAfter(true); anim.setDuration(0); mQuickReturnView.startAnimation(anim); } else { mQuickReturnView.setTranslationY(translationY); } } @Override public void onScrollStateChanged(AbsListView view, int scrollState) { } }); } private class GetDataTask extends AsyncTask<Void, Void, String[]> { @Override protected String[] doInBackground(Void... params) { try { Thread.sleep(2000); } catch (InterruptedException e) { ; } return null; } @Override protected void onPostExecute(String[] result) { alTweets.add(0, "New Tweet Added"); if(alTweets.size() > 50) alTweets.remove(alTweets.size() - 1); // Call onRefreshComplete when the list has been refreshed. mListView.onRefreshComplete(); super.onPostExecute(result); } } public class ListAdapter extends BaseAdapter{ @Override public int getCount() { return alTweets.size(); } @Override public Object getItem(int position) { return alTweets.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View row = convertView; LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); row = inflater.inflate(R.layout.tweet_cell, parent, false); ((TextView)row.findViewById(R.id.tv_tweet)).setText(alTweets.get(position)); return row; } } }