Android Open Source - twitt4droid Lists Fragment






From Project

Back to project page twitt4droid.

License

The source code is released under:

Apache License

If you think the Android project twitt4droid 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

/*
 * Copyright 2014 Daniel Pedraza-Arcega//from   www  .  j  a v  a  2  s.  c  o  m
 *
 * 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.twitt4droid.app.fragment;

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.twitt4droid.Resources;
import com.twitt4droid.Twitt4droid;
import com.twitt4droid.app.R;
import com.twitt4droid.fragment.ListTimelineFragment;

import twitter4j.AsyncTwitter;
import twitter4j.ResponseList;
import twitter4j.TwitterAdapter;
import twitter4j.TwitterException;
import twitter4j.TwitterMethod;
import twitter4j.UserList;

import java.util.ArrayList;
import java.util.List;

public class ListsFragment extends Fragment {

    private ViewPager viewPager;
    private PagerTabStrip pagerStrip;
    private SwipeTimelineFragmentPagerAdapter adapter;
    private AsyncTwitter twitter;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
        setUpTwitter();
    }

    private void setUpTwitter() {
        twitter = Twitt4droid.getAsyncTwitter(getActivity());
        twitter.addListener(new TwitterAdapter() {

            @Override
            public void gotUserLists(final ResponseList<UserList> userLists) {
                if (getActivity() != null) {
                    getActivity().runOnUiThread(new Runnable() {
                        
                        @Override
                        public void run() {
                            if (userLists != null && !userLists.isEmpty()) {
                                for (UserList userList : userLists) {
                                    ListTimelineFragment fragment = ListTimelineFragment.newInstance(userList, isDarkThemeSelected());
                                    adapter.addFragment(fragment);
                                }
                            }
                        }
                    });
                }
            }
            
            @Override
            public void onException(TwitterException te, TwitterMethod method) {
                if (getActivity() != null) {
                    getActivity().runOnUiThread(new Runnable() {
                        
                        @Override
                        public void run() {
                            Toast.makeText(getActivity().getApplicationContext(),
                                    R.string.twitt4droid_error_message,
                                    Toast.LENGTH_SHORT)
                                    .show();
                        }
                    });
                }
            }
        });
    }

    private boolean isDarkThemeSelected() {
        String theme = PreferenceManager.getDefaultSharedPreferences(getActivity())
            .getString(getString(R.string.change_theme_key), getString(R.string.change_theme_default_value));
        return theme.equals(getString(R.string.dark_theme_entry));
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View layout = inflater.inflate(R.layout.fragment_lists, container, false);
        setUpLayout(layout);
        return layout;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (Resources.isConnectedToInternet(getActivity())) twitter.getUserLists(Twitt4droid.getCurrentUser(getActivity()).getScreenName());
        else {
            Toast.makeText(getActivity().getApplicationContext(),
                    R.string.twitt4droid_is_offline_messege,
                    Toast.LENGTH_LONG)
                    .show();
        }
    }

    private void setUpLayout(View layout) {
        viewPager = (ViewPager) layout.findViewById(R.id.view_pager);
        pagerStrip = (PagerTabStrip) layout.findViewById(R.id.pager_strip);
        adapter = new SwipeTimelineFragmentPagerAdapter();
        viewPager.setAdapter(adapter);
        pagerStrip.setDrawFullUnderline(false);
        pagerStrip.setTabIndicatorColor(getResources().getColor(R.color.twitt4droid_primary_color));
    }

    private class SwipeTimelineFragmentPagerAdapter extends FragmentStatePagerAdapter {

        private final List<ListTimelineFragment> fragments;

        private SwipeTimelineFragmentPagerAdapter() {
            super(getChildFragmentManager());
            fragments = new ArrayList<>();
        }

        private void addFragment(ListTimelineFragment fragment) {
            fragments.add(fragment);
            notifyDataSetChanged();
        }

        @Override
        public Fragment getItem(int position) {
            if (!fragments.isEmpty() && position >= 0) return fragments.get(position);
            else return null;
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
        
        @Override
        public CharSequence getPageTitle(int position) {
            if (!fragments.isEmpty() && position >= 0) return fragments.get(position).getListTitle();
            else return null;
        }
    }
}




Java Source Code List

com.twitt4droid.Resources.java
com.twitt4droid.Twitt4droid.java
com.twitt4droid.activity.UserProfileActivity.java
com.twitt4droid.activity.WebLoginActivity.java
com.twitt4droid.app.activity.MainActivity.java
com.twitt4droid.app.activity.SettingsActivity.java
com.twitt4droid.app.activity.SignInActivity.java
com.twitt4droid.app.fragment.ListsFragment.java
com.twitt4droid.app.widget.DrawerItemAdapter.java
com.twitt4droid.app.widget.DrawerItem.java
com.twitt4droid.app.widget.ScrimInsetsFrameLayout.java
com.twitt4droid.data.dao.GenericDAO.java
com.twitt4droid.data.dao.ListTimelineDAO.java
com.twitt4droid.data.dao.TimelineDAO.java
com.twitt4droid.data.dao.UserDAO.java
com.twitt4droid.data.dao.UserTimelineDAO.java
com.twitt4droid.data.dao.impl.DAOFactory.java
com.twitt4droid.data.dao.impl.sqlite.ListSQLiteDAO.java
com.twitt4droid.data.dao.impl.sqlite.SQLiteTemplate.java
com.twitt4droid.data.dao.impl.sqlite.SQLiteUtils.java
com.twitt4droid.data.dao.impl.sqlite.StatusCursorImpl.java
com.twitt4droid.data.dao.impl.sqlite.TimelineSQLiteDAO.java
com.twitt4droid.data.dao.impl.sqlite.UserCursorImpl.java
com.twitt4droid.data.dao.impl.sqlite.UserSQLiteDAO.java
com.twitt4droid.data.dao.impl.sqlite.UserTimelineSQLiteDAO.java
com.twitt4droid.data.source.SQLFileParser.java
com.twitt4droid.data.source.Twitt4droidDatabaseHelper.java
com.twitt4droid.fragment.FixedQueryTimelineFragment.java
com.twitt4droid.fragment.HomeTimelineFragment.java
com.twitt4droid.fragment.ListTimelineFragment.java
com.twitt4droid.fragment.MentionsTimelineFragment.java
com.twitt4droid.fragment.QueryableTimelineFragment.java
com.twitt4droid.fragment.TimelineFragment.java
com.twitt4droid.fragment.UserTimelineFragment.java
com.twitt4droid.util.Files.java
com.twitt4droid.util.Images.java
com.twitt4droid.util.Objects.java
com.twitt4droid.util.Strings.java
com.twitt4droid.widget.LogInOutButton.java
com.twitt4droid.widget.TweetAdapter.java
com.twitt4droid.widget.TweetDialog.java