Android Open Source - saostar Accounts Fragment






From Project

Back to project page saostar.

License

The source code is released under:

Apache License

If you think the Android project saostar 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 net.azyobuzi.azyotter.saostar.activities;
/*w  w  w. j av a  2 s  . c o m*/
import jp.ne.hatena.d.shogo0809.widget.SortableListView;
import jp.ne.hatena.d.shogo0809.widget.SortableListView.SimpleDragListener;
import net.azyobuzi.azyotter.saostar.R;
import net.azyobuzi.azyotter.saostar.configuration.Account;
import net.azyobuzi.azyotter.saostar.configuration.Accounts;
import net.azyobuzi.azyotter.saostar.system.Action;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class AccountsFragment extends ListFragment {
  @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.accounts_list, null);
  }

  private int mDraggingPosition = -1;
  private int selectedIndex = -1;
  private final AccountAdapter adapter = new AccountAdapter();

  private boolean dualPane;

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

        setListAdapter(adapter);

        SortableListView lv = (SortableListView)getListView();
        lv.setDragListener(new DragListener());
        lv.setSortable(true);
        lv.setOnItemClickListener(new OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        showDetails(arg2);
      }
        });

        View detailsFrame = getActivity().findViewById(R.id.layout_account_page_details);
        dualPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE;

        Accounts.accountsChangedHandler.add(accountsChangedHandler);

      long id = getActivity().getIntent().getLongExtra(AccountPreferenceFragment.ACCOUNT_ID, -1);

        if (dualPane) {
          getListView().setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
          showDetails(id != -1 ? Accounts.indexOf(id) : 0);
        } else if (id != -1) {
          startActivity(new Intent(getActivity(), AccountPreferenceActivity.class)
        .putExtra(AccountPreferenceFragment.ACCOUNT_ID, id)
        .putExtra(MainActivity.CALLED_FROM_AZYOTTER, true));
        }
  }

  @Override
  public void onDestroy() {
    Accounts.accountsChangedHandler.remove(accountsChangedHandler);

    super.onDestroy();
  }

  private final Action accountsChangedHandler = new Action() {
    @Override
    public void invoke() {
      adapter.notifyDataSetChanged();

      if (dualPane) {
        showDetails(selectedIndex);
      }
    }
  };

  public void showDetails(int index) {
    if (Accounts.getAccountsCount() <= index) {
      showDetails(Accounts.getAccountsCount() - 1);
      return;
    }

    selectedIndex = index;

    if (dualPane) {
      if (selectedIndex < 0) {
        getFragmentManager().beginTransaction()
          .replace(R.id.layout_account_page_details, new Fragment(), null)
          .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
          .commit();
        return;
      }

      getListView().setItemChecked(index, true);

      AccountPreferenceFragment fragment = new AccountPreferenceFragment();
      Bundle arg = new Bundle();
      arg.putLong(AccountPreferenceFragment.ACCOUNT_ID, adapter.getAccountItem(index).getId());
      fragment.setArguments(arg);

      getFragmentManager().beginTransaction()
        .replace(R.id.layout_account_page_details, fragment)
        .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
        .commit();
    } else {
      startActivity(new Intent(getActivity(), AccountPreferenceActivity.class)
        .putExtra(AccountPreferenceFragment.ACCOUNT_ID, adapter.getAccountItem(index).getId())
        .putExtra(MainActivity.CALLED_FROM_AZYOTTER, true));
    }
  }

  private class AccountAdapter extends BaseAdapter {
    @Override
    public int getCount() {
      return Accounts.getAccountsCount();
    }

    public Account getAccountItem(int index) {
      return Accounts.getAllAccounts().elementAtOrDefault(index, null);
    }

    @Override
    public Object getItem(int arg0) {
      return getAccountItem(arg0);
    }

    @Override
    public long getItemId(int arg0) {
      return arg0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
      TextView re = arg1 != null
        ? (TextView)arg1
        : (TextView)getActivity().getLayoutInflater().inflate(android.R.layout.simple_list_item_activated_1, null);
      Account a = getAccountItem(arg0);
      re.setText(a.getScreenName());
      re.setVisibility(arg0 == mDraggingPosition ? View.INVISIBLE : View.VISIBLE);
      return re;
    }

  }

  private class DragListener extends SimpleDragListener {
    @Override
        public int onStartDrag(int position) {
            mDraggingPosition = position;
            getListView().invalidateViews();
            return position;
        }

        @Override
        public int onDuringDrag(int positionFrom, int positionTo) {
            if (positionFrom < 0 || positionTo < 0
                    || positionFrom == positionTo) {
                return positionFrom;
            }
            Accounts.move(positionFrom, positionTo);
            mDraggingPosition = positionTo;
            if (dualPane && positionFrom == selectedIndex)
              getListView().setItemChecked(selectedIndex = positionTo, true);
            getListView().invalidateViews();
            return positionTo;
        }

        @Override
        public boolean onStopDrag(int positionFrom, int positionTo) {
            mDraggingPosition = -1;
            getListView().invalidateViews();
            return super.onStopDrag(positionFrom, positionTo);
        }
  }
}




Java Source Code List

jp.ne.hatena.d.shogo0809.widget.SortableListView.java
net.azyobuzi.azyotter.saostar.ActivityUtil.java
net.azyobuzi.azyotter.saostar.ContextAccess.java
net.azyobuzi.azyotter.saostar.NotificationCenter.java
net.azyobuzi.azyotter.saostar.SaostarApplication.java
net.azyobuzi.azyotter.saostar.StringUtil.java
net.azyobuzi.azyotter.saostar.Twitter4JFactories.java
net.azyobuzi.azyotter.saostar.TwitterUriGenerator.java
net.azyobuzi.azyotter.saostar.activities.AccountPreferenceActivity.java
net.azyobuzi.azyotter.saostar.activities.AccountPreferenceFragment.java
net.azyobuzi.azyotter.saostar.activities.AccountsActivity.java
net.azyobuzi.azyotter.saostar.activities.AccountsFragment.java
net.azyobuzi.azyotter.saostar.activities.ExpandLinkActivity.java
net.azyobuzi.azyotter.saostar.activities.LoginActivity.java
net.azyobuzi.azyotter.saostar.activities.MainActivity.java
net.azyobuzi.azyotter.saostar.activities.RetryActivity.java
net.azyobuzi.azyotter.saostar.activities.SettingActivity.java
net.azyobuzi.azyotter.saostar.activities.SettingFragment.java
net.azyobuzi.azyotter.saostar.activities.TabFilterSettingFragment.java
net.azyobuzi.azyotter.saostar.activities.TabGeneralSettingFragment.java
net.azyobuzi.azyotter.saostar.activities.TabPreferenceActivity.java
net.azyobuzi.azyotter.saostar.activities.TabsActivity.java
net.azyobuzi.azyotter.saostar.activities.TabsFragment.java
net.azyobuzi.azyotter.saostar.activities.TimelineTabFragment.java
net.azyobuzi.azyotter.saostar.activities.TweetDetailActivity.java
net.azyobuzi.azyotter.saostar.activities.TwitterUriHookActivity.java
net.azyobuzi.azyotter.saostar.activities.UpdateStatusActivity.java
net.azyobuzi.azyotter.saostar.configuration.Account.java
net.azyobuzi.azyotter.saostar.configuration.Accounts.java
net.azyobuzi.azyotter.saostar.configuration.Command.java
net.azyobuzi.azyotter.saostar.configuration.Setting.java
net.azyobuzi.azyotter.saostar.configuration.Tab.java
net.azyobuzi.azyotter.saostar.configuration.Tabs.java
net.azyobuzi.azyotter.saostar.d_aqa.Constant.java
net.azyobuzi.azyotter.saostar.d_aqa.FunctionFactory.java
net.azyobuzi.azyotter.saostar.d_aqa.Function.java
net.azyobuzi.azyotter.saostar.d_aqa.Invokable.java
net.azyobuzi.azyotter.saostar.d_aqa.OperatorFactory.java
net.azyobuzi.azyotter.saostar.d_aqa.Operator.java
net.azyobuzi.azyotter.saostar.d_aqa.PropertyFactory.java
net.azyobuzi.azyotter.saostar.d_aqa.Property.java
net.azyobuzi.azyotter.saostar.d_aqa.Reader.java
net.azyobuzi.azyotter.saostar.d_aqa.operators.EqualityOperator.java
net.azyobuzi.azyotter.saostar.d_aqa.operators.GreaterThanOperator.java
net.azyobuzi.azyotter.saostar.d_aqa.operators.GreaterThanOrEqualOperator.java
net.azyobuzi.azyotter.saostar.d_aqa.operators.InequalityOperator.java
net.azyobuzi.azyotter.saostar.d_aqa.operators.LessThanOperator.java
net.azyobuzi.azyotter.saostar.d_aqa.operators.LessThanOrEqualOperator.java
net.azyobuzi.azyotter.saostar.d_aqa.properties.CreatedAtProperty.java
net.azyobuzi.azyotter.saostar.d_aqa.properties.FromCreatedAtProperty.java
net.azyobuzi.azyotter.saostar.d_aqa.properties.FromIdProperty.java
net.azyobuzi.azyotter.saostar.d_aqa.properties.FromNameProperty.java
net.azyobuzi.azyotter.saostar.d_aqa.properties.FromProtectedProperty.java
net.azyobuzi.azyotter.saostar.d_aqa.properties.FromScreenNameProperty.java
net.azyobuzi.azyotter.saostar.d_aqa.properties.FromVerifiedProperty.java
net.azyobuzi.azyotter.saostar.d_aqa.properties.IdProperty.java
net.azyobuzi.azyotter.saostar.d_aqa.properties.InReplyToProperty.java
net.azyobuzi.azyotter.saostar.d_aqa.properties.IsHomeTweetProperty.java
net.azyobuzi.azyotter.saostar.d_aqa.properties.OriginalTextProperty.java
net.azyobuzi.azyotter.saostar.d_aqa.properties.RetweetedCreatedAtProperty.java
net.azyobuzi.azyotter.saostar.d_aqa.properties.RetweetedIdProperty.java
net.azyobuzi.azyotter.saostar.d_aqa.properties.RetweetedProperty.java
net.azyobuzi.azyotter.saostar.d_aqa.properties.RetweetedSourceProperty.java
net.azyobuzi.azyotter.saostar.d_aqa.properties.RetweetedUserCreatedAtProperty.java
net.azyobuzi.azyotter.saostar.d_aqa.properties.RetweetedUserIdProperty.java
net.azyobuzi.azyotter.saostar.d_aqa.properties.RetweetedUserNameProperty.java
net.azyobuzi.azyotter.saostar.d_aqa.properties.RetweetedUserScreenNameProperty.java
net.azyobuzi.azyotter.saostar.d_aqa.properties.RetweetedUserVerifiedProperty.java
net.azyobuzi.azyotter.saostar.d_aqa.properties.SourceProperty.java
net.azyobuzi.azyotter.saostar.d_aqa.properties.TextProperty.java
net.azyobuzi.azyotter.saostar.linq.Enumerable.java
net.azyobuzi.azyotter.saostar.linq.Enumerator.java
net.azyobuzi.azyotter.saostar.services.FavoriteService.java
net.azyobuzi.azyotter.saostar.services.RetweetService.java
net.azyobuzi.azyotter.saostar.services.TimelineReceiveService.java
net.azyobuzi.azyotter.saostar.services.UpdateStatusService.java
net.azyobuzi.azyotter.saostar.system.Action1.java
net.azyobuzi.azyotter.saostar.system.Action2.java
net.azyobuzi.azyotter.saostar.system.Action3.java
net.azyobuzi.azyotter.saostar.system.Action.java
net.azyobuzi.azyotter.saostar.system.Func1.java
net.azyobuzi.azyotter.saostar.system.Func2.java
net.azyobuzi.azyotter.saostar.system.Func.java
net.azyobuzi.azyotter.saostar.timeline_data.TimelineItemCollection.java
net.azyobuzi.azyotter.saostar.timeline_data.TimelineItemId.java
net.azyobuzi.azyotter.saostar.timeline_data.TimelineItem.java
net.azyobuzi.azyotter.saostar.timeline_data.TweetEntities.java
net.azyobuzi.azyotter.saostar.timeline_data.UserCollection.java
net.azyobuzi.azyotter.saostar.timeline_data.UserInfo.java
net.azyobuzi.azyotter.saostar.widget.AccountSelector.java
net.azyobuzi.azyotter.saostar.widget.CustomizedUrlImageView.java