Android Open Source - Test-SimpleTwitterClient Timeline Fragment






From Project

Back to project page Test-SimpleTwitterClient.

License

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 Test-SimpleTwitterClient listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.example.Twitter_Android.Fragments;
/* w w w. ja  v a  2 s. c  o m*/
import android.app.ListFragment;
import android.app.LoaderManager;
import android.content.Loader;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import com.example.Twitter_Android.Logic.DataCache;
import com.example.Twitter_Android.Fragments.Adapters.TimelineAdapter;

import java.util.List;

public abstract class TimelineFragment<T> extends ListFragment implements LoaderManager.LoaderCallbacks<List<T>>,
    AdapterView.OnItemLongClickListener {

  private TimelineAdapter<T> currentAdapter;
  private final DataCache cache;
  private int currentScrollPosition;
  private int selectedListItemPosition = 0;

  /**
   * ??????????? ??????, ????? ??????? ????? ?????????? ???????? ?????, ? ??.
   */
  private static final int NEWEST_TWEET_LOAD_PERIOD = 70_000;
  /**
   * ????? ?????????? ???????? ????? ??????. ??????????, ????? ??????? ???????? ????? ?????? ??? ? 3 ??????.
   */
  private long prevLoadTime;
  /**
   * ?? ??????? ?????? ?? ????? ???????? ????????? ????? ??????.
   */
  static final int TWEETS_BEFORE_END = 20;
  public static final String TAG = "TAG_TIMELINE_FRAGMENT";
  private static final String SCROLL_POSITION = "SCROLL_POSITION";

  volatile boolean isLoading = false;   //???? ?? ???????? ????? ???????
  volatile boolean needNewestTweet = true;  //????? ?? ??????? ????? (????????) ???????
  volatile boolean needOldTweet = true; //????? ?? ??????? ?????? ???????

  TimelineFragment() {
    prevLoadTime = System.currentTimeMillis();
    cache = DataCache.getInstance();
  }

  //------------------------------------------------------------------------------------------------------------------
  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    final ListView listView = getListView();
    if (listView != null) {
      listView.setOnItemLongClickListener(this);
      /*
        ????????? ?? ???? - ?????????? ?????? ????? (??? ???????? - ?????, ???? ??????? ?????).
       */
      listView.setOnScrollListener(new AbsListView.OnScrollListener() {
        private int item = 0;

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

        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
          item += firstVisibleItem;
          /*
            ????? ?? ????? ?????? ???????? "TWEETS_BEFORE_END" ??????, ???????? ?????????? ????? ??????.
            ??? ???? ????????? ???????? (??-?? ????, ??? ???????? ?????????? ?? ???????????),
            ????? ????? ?????? ?? ???????? ???????????? ?? ?????????? ??????? ???????? ?????? ??????.
            ??? ????, ????? ????? ????????, ?????? ?????????? isLoading, ??????? ???????? ???????????
            ???????? ????????.
            ????? ?????????? ???????? ??????????? ????? ?????? ?????????? ?????????? isLoading ? false.
           */
          if (!isLoading) {
            if (currentAdapter != null) {
              if (needOldTweet && ((firstVisibleItem + visibleItemCount) > (currentAdapter.getCount() - TWEETS_BEFORE_END))) {
                isLoading = true;
                loadOldItems();
              } else if (item > 0 && firstVisibleItem == 0) {
              /*
                ????? ?????? ?????????, ???? ??? ?????????? ?????? ??????? ? ListView, ? ??????
                ?????????? ??? ??? ???, ?????? ?? ?????????? ????? ?? ????? ? ????? ?? ??????????
                ???????? ?????.
               */
                if (needNewestTweet && timeToLoad()) {
                  item = 0;
                  isLoading = true;
                  loadNewestItems();
                  prevLoadTime = System.currentTimeMillis();
                }
              }
            }
          }
        }
      });

      listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
          selectedListItemPosition = position;
        }
      });
    }
  }

  //------------------------------------------------------------------------------------------------------------------
  private boolean timeToLoad() {
    return (System.currentTimeMillis() - prevLoadTime) >= NEWEST_TWEET_LOAD_PERIOD;
  }

  /*
    ?.?. ??? ???????????? ???????? ? ?????????? ????????? ?? TimelineAdapter,
    ?? ?????? ???? ?? ??????.
   */
  @SuppressWarnings("unchecked")
  @Override
  public void setListAdapter(ListAdapter adapter) {
    super.setListAdapter(adapter);
    currentAdapter = (TimelineAdapter<T>) adapter;
  }
  //------------------------------------------------------------------------------------------------------------------

  /**
   * ???????? ??????? (Tweet ??? Person) ?? ???????? ???????.
   *
   * @param position ??????? ?????????? ???????? ? ListView.
   * @return ???????? ???????.
   */
  T getItem(int position) {
    return (currentAdapter != null) ? (currentAdapter.getItem(position)) : null;
  }
  //------------------------------------------------------------------------------------------------------------------

  /**
   * ???????? ???????? max_id ??? ????????? ??????.
   *
   * @return max_id
   */
  long getMaxId() {
    return (currentAdapter != null) ? currentAdapter.getMaxID() : 0;
  }
  //------------------------------------------------------------------------------------------------------------------

  /**
   * ???????? ???????? since_id ??? ???????? ????? ??????.
   *
   * @return since_id
   */
  long getSinceID() {
    return (currentAdapter != null) ? currentAdapter.getSinceID() : 0;
  }
  //------------------------------------------------------------------------------------------------------------------

  /**
   * ???????? ?????? ??????.
   * ??????????, ????? ???????????? ???????????? ??? ??????????? ?????? ?? ?????.
   */
  protected abstract void loadOldItems();

  /**
   * ????????? ???????? ??????.
   * ??????????, ????? ???????????? ??????? ?????? ? ??????, ?? ???? ?????? ???? ? NEWEST_TWEET_LOAD_PERIOD ??.
   */
  void loadNewestItems(){

  }
  //------------------------------------------------------------------------------------------------------------------
  @Override
  public abstract Loader<List<T>> onCreateLoader(int id, Bundle args);

  @Override
  public abstract void onLoadFinished(Loader<List<T>> loader, List<T> data);

  @Override
  public abstract void onLoaderReset(Loader<List<T>> loader);

  //------------------------------------------------------------------------------------------------------------------
  @Override
  public abstract boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id);

  @Override
  public void onViewStateRestored(Bundle savedInstanceState) {
    super.onViewStateRestored(savedInstanceState);
    if (savedInstanceState != null) {
      currentScrollPosition = savedInstanceState.getInt(SCROLL_POSITION, 0);
    }
    if (currentAdapter != null && getListView() != null) {
      getListView().setSelection(currentScrollPosition);
    }
  }

  @Override
  public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    if (getListView() != null) {
      currentScrollPosition = getListView().getFirstVisiblePosition();
      outState.putInt(SCROLL_POSITION, currentScrollPosition);
    }
  }

  @Override
  public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if (currentAdapter != null && getListView() != null) {
      setListAdapter(currentAdapter);
      getListView().setSelection(currentScrollPosition);
    }
  }

  @Override
  public void onStop() {
    super.onStop();
    if (getListView() != null) {
      currentScrollPosition = getListView().getFirstVisiblePosition();
    }

    if (currentAdapter != null) {
      cache.saveAdapter(currentAdapter.getTag(), currentAdapter);
    }
  }

  T getSelectedItem() {
    return (currentAdapter != null) ? currentAdapter.getItem(selectedListItemPosition) : null;
  }

  int getSelectedListItemPosition() {
    return selectedListItemPosition;
  }
}




Java Source Code List

com.example.Twitter_Android.AppActivity.ConcreteUserTimelineActivity.java
com.example.Twitter_Android.AppActivity.MainActivity.java
com.example.Twitter_Android.AppActivity.MessagesActivity.java
com.example.Twitter_Android.AppActivity.SearchableActivity.java
com.example.Twitter_Android.AppActivity.SettingsActivity.java
com.example.Twitter_Android.AsynkTasks.ImageDownloader.java
com.example.Twitter_Android.AsynkTasks.TaskDeleteTweet.java
com.example.Twitter_Android.AsynkTasks.TaskFollow.java
com.example.Twitter_Android.AsynkTasks.TaskPostTweet.java
com.example.Twitter_Android.AsynkTasks.TaskReply.java
com.example.Twitter_Android.AsynkTasks.TaskRetweet.java
com.example.Twitter_Android.AsynkTasks.TaskSendMessage.java
com.example.Twitter_Android.AsynkTasks.TaskUnfollow.java
com.example.Twitter_Android.Fragments.ConcreteUserTimelineFragment.java
com.example.Twitter_Android.Fragments.ConnectedUserTimelineFragment.java
com.example.Twitter_Android.Fragments.FollowersFragment.java
com.example.Twitter_Android.Fragments.FollowingsFragment.java
com.example.Twitter_Android.Fragments.HomeTimelineFragment.java
com.example.Twitter_Android.Fragments.TimelineFragment.java
com.example.Twitter_Android.Fragments.UserInfoFragment.java
com.example.Twitter_Android.Fragments.Adapters.ConcreteUserTimelineAdapter.java
com.example.Twitter_Android.Fragments.Adapters.FollowingsListAdapter.java
com.example.Twitter_Android.Fragments.Adapters.MessagesAdapter.java
com.example.Twitter_Android.Fragments.Adapters.TimelineAdapter.java
com.example.Twitter_Android.Fragments.Adapters.TweetAdapter.java
com.example.Twitter_Android.Fragments.Dialogs.DeleteTweetDialog.java
com.example.Twitter_Android.Fragments.Dialogs.DirectMessageDialog.java
com.example.Twitter_Android.Fragments.Dialogs.ErrorDialog.java
com.example.Twitter_Android.Fragments.Dialogs.PostTweetDialog.java
com.example.Twitter_Android.Fragments.Dialogs.ReplyDialog.java
com.example.Twitter_Android.Fragments.Dialogs.RetweetDialog.java
com.example.Twitter_Android.Fragments.Dialogs.ShowImageDialog.java
com.example.Twitter_Android.Fragments.Dialogs.UserInfoDialog.java
com.example.Twitter_Android.Fragments.Preferences.SettingsFragment.java
com.example.Twitter_Android.Loaders.AsyncUserInfoLoader.java
com.example.Twitter_Android.Loaders.FollowersLoader.java
com.example.Twitter_Android.Loaders.FollowingsLoader.java
com.example.Twitter_Android.Loaders.HomeTimelineLoader.java
com.example.Twitter_Android.Loaders.MessageLoader.java
com.example.Twitter_Android.Loaders.Task_LoadSearchedTweets.java
com.example.Twitter_Android.Loaders.Task_LoadSearchedUsers.java
com.example.Twitter_Android.Loaders.TweetLoader.java
com.example.Twitter_Android.Loaders.UserTimelineLoader.java
com.example.Twitter_Android.Logic.AppDatabase.java
com.example.Twitter_Android.Logic.Constants.java
com.example.Twitter_Android.Logic.DataCache.java
com.example.Twitter_Android.Logic.FileWorker.java
com.example.Twitter_Android.Logic.Person.java
com.example.Twitter_Android.Logic.Tweet.java
com.example.Twitter_Android.Net.Connector.java
com.example.Twitter_Android.Net.Parser.java
com.mass.cmassive.CMassive.java