Android Open Source - Multi-Mania-app News Fragment






From Project

Back to project page Multi-Mania-app.

License

The source code is released under:

MIT License

If you think the Android project Multi-Mania-app 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 be.ana.nmct.multimania.ui;
// w w w  .ja  v  a  2  s  . c  o m
import android.app.Fragment;
import android.app.LoaderManager;
import android.content.Context;
import android.content.CursorLoader;
import android.content.Intent;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.ImageView;

import com.etsy.android.grid.StaggeredGridView;
import com.etsy.android.grid.util.DynamicHeightTextView;
import com.koushikdutta.ion.Ion;

import be.ana.nmct.multimania.R;
import be.ana.nmct.multimania.data.MultimaniaContract;

/**
 * The NewsFragment shows all NewsItems
 */
public class NewsFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor>, AdapterView.OnItemClickListener {

    private static final String TAG = NewsFragment.class.getSimpleName();
    private NewsCursorAdapter mAdapter;
    private Cursor mData;
    private StaggeredGridView mGridView;

    //default ctor
    public NewsFragment(){}

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
        mAdapter=new NewsCursorAdapter(getActivity(),null,0);
        //setListAdapter(mAdapter);
    }

    @Override
    public void onStart() {
        super.onStart();
        getLoaderManager().initLoader(MainActivity.LOADER_NEWS_ID, null, this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_news,container,false);
        mGridView =(StaggeredGridView) v.findViewById(R.id.news_grid);
        mGridView.setOnItemClickListener(this);
        mGridView.setAdapter(mAdapter);
        mAdapter.swapCursor(mData);
        return v;
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        return new CursorLoader(getActivity(),MultimaniaContract.NewsItemEntry.CONTENT_URI,null,null,null,null);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        mData=data;
        mAdapter.swapCursor(mData);
        loader.abandon();
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        mData=null;
        mAdapter.swapCursor(null);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Cursor cursor = mAdapter.getCursor();
        cursor.moveToPosition(position);
        Uri uri = MultimaniaContract.NewsItemEntry.buildItemUri(
                cursor.getLong(
                        cursor.getColumnIndex(
                                MultimaniaContract.NewsItemEntry._ID
                        )
                )
        );

        Intent intent = new Intent(getActivity(),NewsItemActivity.class);
        intent.setData(uri);
        startActivity(intent);
    }

    private class NewsCursorAdapter extends CursorAdapter{
        private int mTitleCol;
        private int mImgCol;
        private int mShortDescriptionCol;
        private LayoutInflater mInflater;
        private Animation animFadeIn;



        public NewsCursorAdapter(Context context, Cursor cursor, int flags) {
            super(context, cursor, flags);
            // mInflater = LayoutInflater.from(context);
            mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            animFadeIn = AnimationUtils.loadAnimation(context.getApplicationContext(),
                    R.anim.fade_in);

        }

        @Override
        public Cursor swapCursor(Cursor newCursor) {
            if(newCursor!=null){
                mTitleCol = newCursor.getColumnIndexOrThrow(MultimaniaContract.NewsItemEntry.TITLE);
                mImgCol = newCursor.getColumnIndexOrThrow(MultimaniaContract.NewsItemEntry.IMAGE);
                mShortDescriptionCol = newCursor.getColumnIndexOrThrow(MultimaniaContract.NewsItemEntry.SHORT_DESCRIPTION);
            }
            return super.swapCursor(newCursor);
        }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            //mInflater = LayoutInflater.from(context);

            View v  = mInflater.inflate(R.layout.row_news, parent, false);

            ViewHolder holder = new ViewHolder();
            holder.txtTitle = (DynamicHeightTextView) v.findViewById(R.id.txtNewsTitle);
            holder.txtShortDescription = (DynamicHeightTextView) v.findViewById(R.id.txtNewsText);
            holder.imgNews = (ImageView)v.findViewById(R.id.imgNews);
            v.setTag(holder);
            return v;

        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            if(cursor==null||cursor.isClosed())return;
            ViewHolder holder = (ViewHolder)view.getTag();

            String title = cursor.getString(mTitleCol);
            String shortDescription = cursor.getString(mShortDescriptionCol);
            String img = cursor.getString(mImgCol);

            holder.txtTitle.setText(title);
            holder.txtShortDescription.setText(shortDescription);
            Ion.with(holder.imgNews)
                    .smartSize(true)
                    .animateIn(animFadeIn)
                    .load(img);
        }


    }

    static class ViewHolder{
        DynamicHeightTextView txtTitle;
        DynamicHeightTextView txtShortDescription;
        ImageView imgNews;
    }
}




Java Source Code List

be.ana.nmct.multimania.ApiTest.java
be.ana.nmct.multimania.ApplicationTest.java
be.ana.nmct.multimania.CalendarTest.java
be.ana.nmct.multimania.DbTest.java
be.ana.nmct.multimania.Import.java
be.ana.nmct.multimania.NotificationImport.java
be.ana.nmct.multimania.ProviderTest.java
be.ana.nmct.multimania.SyncTest.java
be.ana.nmct.multimania.data.ApiActions.java
be.ana.nmct.multimania.data.DbHelper.java
be.ana.nmct.multimania.data.GsonLoader.java
be.ana.nmct.multimania.data.MultimaniaContract.java
be.ana.nmct.multimania.data.MultimaniaProvider.java
be.ana.nmct.multimania.data.NewsItemLoader.java
be.ana.nmct.multimania.data.RoomLoader.java
be.ana.nmct.multimania.data.TagLoader.java
be.ana.nmct.multimania.data.TalkLoader.java
be.ana.nmct.multimania.model.IData.java
be.ana.nmct.multimania.model.NewsItem.java
be.ana.nmct.multimania.model.Room.java
be.ana.nmct.multimania.model.Speaker.java
be.ana.nmct.multimania.model.Tag.java
be.ana.nmct.multimania.model.TalkSpeaker.java
be.ana.nmct.multimania.model.TalkTag.java
be.ana.nmct.multimania.model.Talk.java
be.ana.nmct.multimania.model.User.java
be.ana.nmct.multimania.service.AuthenticatorService.java
be.ana.nmct.multimania.service.Authenticator.java
be.ana.nmct.multimania.service.BootListener.java
be.ana.nmct.multimania.service.NotificationReceiver.java
be.ana.nmct.multimania.service.NotificationSender.java
be.ana.nmct.multimania.service.SyncAdapter.java
be.ana.nmct.multimania.service.SyncService.java
be.ana.nmct.multimania.ui.AboutFragment.java
be.ana.nmct.multimania.ui.LoadActivity.java
be.ana.nmct.multimania.ui.MainActivity.java
be.ana.nmct.multimania.ui.MapFragment.java
be.ana.nmct.multimania.ui.MyScheduleFragment.java
be.ana.nmct.multimania.ui.MySchedulesFragment.java
be.ana.nmct.multimania.ui.NavigationDrawerFragment.java
be.ana.nmct.multimania.ui.NewsFragment.java
be.ana.nmct.multimania.ui.NewsItemActivity.java
be.ana.nmct.multimania.ui.NewsItemFragment.java
be.ana.nmct.multimania.ui.ScheduleFragment.java
be.ana.nmct.multimania.ui.SchedulesFragment.java
be.ana.nmct.multimania.ui.SettingsFragment.java
be.ana.nmct.multimania.ui.SuggestionFragment.java
be.ana.nmct.multimania.ui.TalkActivity.java
be.ana.nmct.multimania.ui.TalkFragment.java
be.ana.nmct.multimania.utils.GoogleCalUtil.java
be.ana.nmct.multimania.utils.SettingsHelper.java
be.ana.nmct.multimania.utils.SettingsUtil.java
be.ana.nmct.multimania.utils.SyncUtils.java
be.ana.nmct.multimania.utils.Utility.java
be.ana.nmct.multimania.vm.NavigationItem.java
be.ana.nmct.multimania.vm.ScheduleTalkVm.java
be.ana.nmct.multimania.widget.StaggeredGridView.java
com.bulletnoid.android.widget.StaggeredGridView.BulletStaggeredGridView.java
com.bulletnoid.android.widget.StaggeredGridView.HeaderFooterListAdapter.java
com.bulletnoid.android.widget.StaggeredGridView.ScrollerCompatIcs.java
com.bulletnoid.android.widget.StaggeredGridView.ScrollerCompat.java