Android Open Source - Multi-Mania-app News Item 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;
/* ww w.  j  a va 2s  .c om*/
import android.app.Activity;
import android.app.Fragment;
import android.app.LoaderManager;
import android.content.CursorLoader;
import android.content.Intent;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.koushikdutta.ion.Ion;
import com.manuelpeinado.fadingactionbar.FadingActionBarHelper;
import com.manuelpeinado.fadingactionbar.FadingActionBarHelperBase;

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

/**
 * NewsItemFragment shows the data for one NewsItem
 * The NewsItemFragment lives inside the NewsItemActivity
 * @see be.ana.nmct.multimania.ui.NewsItemActivity
 */
public class NewsItemFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor>{

    public static final String URI_KEY = "uri_key";
    private Uri mUri=null;
    private ImageView mNewsItemImg;
    private TextView mNewsItemTitle;
    private WebView mNewsItemInfo;
    private FadingActionBarHelperBase mFadingHelper;
    private TitleLoadListener mTitleLoadListener;
    private Cursor mData;

    public NewsItemFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
        if (getArguments() != null) {
            mUri = getArguments().getParcelable(URI_KEY);
        }
        getLoaderManager().initLoader(0, null, this);
        setHasOptionsMenu(true);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = mFadingHelper.createView(inflater);
        mNewsItemImg = (ImageView) view.findViewById(R.id.img);
        mNewsItemTitle = (TextView) view.findViewById(R.id.title);
        mNewsItemInfo = (WebView) view.findViewById(R.id.txtNewsItemInfo);

        mNewsItemInfo.setBackgroundColor(0x00000000);
        if (Build.VERSION.SDK_INT >= 11) mNewsItemInfo.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);

        BindData(mData);
        return view;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.news_item, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()){
            case R.id.action_share: {
                Intent sendIntent = new Intent();
                sendIntent.setAction(Intent.ACTION_SEND);
                sendIntent.putExtra(Intent.EXTRA_TEXT, mNewsItemTitle.getText().toString() + ". " + getString(R.string.readmoreatmm)+ ". " + getString(R.string.hashtag_mm));
                sendIntent.setType("text/plain");
                startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.share_with)));
            }
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mFadingHelper = new FadingActionBarHelper()
                .parallax(false)
                .actionBarBackground(R.drawable.ab_background)
                .headerLayout(R.layout.image_title)
                .contentLayout(R.layout.fragment_news_item);
        if(activity.getActionBar()!=null){
            mFadingHelper.initActionBar(activity);
        }
    }

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

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

    private void BindData(Cursor cursor) {
        if(cursor==null)return;
        if(cursor.moveToFirst()) {
            int titleCol = cursor.getColumnIndexOrThrow(MultimaniaContract.NewsItemEntry.TITLE);
            int imgCol = cursor.getColumnIndexOrThrow(MultimaniaContract.NewsItemEntry.IMAGE);
            int shortDescriptionCol = cursor.getColumnIndexOrThrow(MultimaniaContract.NewsItemEntry.SHORT_DESCRIPTION);
            int longDescriptionCol = cursor.getColumnIndexOrThrow(MultimaniaContract.NewsItemEntry.LONG_DESCRIPTION);

            Animation animFadein = AnimationUtils.loadAnimation(getActivity().getApplicationContext(),
                    R.anim.fade_in);

            String title = cursor.getString(titleCol);
            if (mTitleLoadListener != null) {
                mTitleLoadListener.onTitleloaded(title);
            }
            String shortDescription = cursor.getString(shortDescriptionCol);
            String longDescription = cursor.getString(longDescriptionCol);
            if (TextUtils.isEmpty(longDescription)) {
                longDescription = shortDescription;
            }
            String img = cursor.getString(imgCol);

            mNewsItemTitle.setText(title);

            String mime = "text/html";
            String encoding = "utf-8";
            String html = Utility.getHtml(longDescription);
            mNewsItemInfo.getSettings().setJavaScriptEnabled(true);
            mNewsItemInfo.loadDataWithBaseURL("file:///android_asset/", html, mime, encoding, null);
            Animation anim =Utility.getAlphaAnimation(0, 1, 400, 300);
            mNewsItemInfo.setAnimation(anim);
            anim.start();

            // holder.imgNews.setImageURI(Uri.parse(img));
            Ion.with(mNewsItemImg)
                    .smartSize(true)
                    .animateIn(animFadein)
                    .load(img);
        }
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {

    }

    public void setTitleLoadListener(TitleLoadListener listener){
        mTitleLoadListener =listener;
    }

    public interface TitleLoadListener {
        public void onTitleloaded(String title);
    }
}




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