com.vinson.jingwen.NewsListFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.vinson.jingwen.NewsListFragment.java

Source

/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * 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.vinson.jingwen;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.vinson.jingwen.api.BaseApi;
import com.vinson.jingwen.api.NewsRequest;
import com.vinson.jingwen.model.Channels;
import com.vinson.jingwen.model.News;
import com.vinson.jingwen.utils.Utils;

import java.util.ArrayList;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class NewsListFragment extends Fragment {

    private ArrayList<News.New> values;
    RecyclerView rv;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        Bundle bundle = getArguments();
        Channels.Channel channel = (Channels.Channel) bundle.getSerializable("channel");

        rv = (RecyclerView) inflater.inflate(R.layout.fragment_cheese_list, container, false);
        getNewsInfo(channel.channelId, channel.name);

        return rv;
    }

    private void getNewsInfo(String channelId, String channelName) {
        NewsRequest.getNews(new BaseApi().getAppKey(), channelId, channelName).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<News>() {
                    @Override
                    public void onCompleted() {
                        Log.d("news", "onCompleted");
                        setupRecyclerView(rv, values);
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.d("news", e.toString());
                    }

                    @Override
                    public void onNext(News news) {
                        Log.d("news", news.toString());
                        values = news.resBody.container.pageBean.contentlist;
                    }
                });
    }

    private void setupRecyclerView(RecyclerView recyclerView, ArrayList<News.New> news) {
        recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
        recyclerView.setAdapter(new SimpleStringRecyclerViewAdapter(getActivity(), news));
    }

    public static class SimpleStringRecyclerViewAdapter
            extends RecyclerView.Adapter<SimpleStringRecyclerViewAdapter.ViewHolder> {

        private final TypedValue mTypedValue = new TypedValue();
        private int mBackground;
        private ArrayList<News.New> news;

        public static class ViewHolder extends RecyclerView.ViewHolder {
            public String mBoundString;

            public final View mView;
            public final ImageView mImageView;
            public final TextView mTextView;

            public ViewHolder(View view) {
                super(view);
                mView = view;
                mImageView = (ImageView) view.findViewById(R.id.avatar);
                mTextView = (TextView) view.findViewById(android.R.id.text1);
            }

            @Override
            public String toString() {
                return super.toString() + " '" + mTextView.getText();
            }
        }

        public News.New getValueAt(int position) {
            return news.get(position);
        }

        public SimpleStringRecyclerViewAdapter(Context context, ArrayList<News.New> news) {
            context.getTheme().resolveAttribute(R.attr.selectableItemBackground, mTypedValue, true);
            mBackground = mTypedValue.resourceId;
            this.news = news;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
            view.setBackgroundResource(mBackground);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(final ViewHolder holder, int position) {
            final News.New newsItem = news.get(position);

            final StringBuilder content = new StringBuilder();
            for (Object o : newsItem.contents) {
                content.append(o.toString());
            }

            holder.mBoundString = newsItem.title;
            holder.mTextView.setText(newsItem.title);

            holder.mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Context context = v.getContext();
                    Intent intent = new Intent(context, NewsDetailActivity.class);
                    intent.putExtra(NewsDetailActivity.EXTRA_NEWS, newsItem);

                    context.startActivity(intent);
                }
            });

            Glide.with(holder.mImageView.getContext())
                    .load(newsItem.havePic ? newsItem.imageurls.get(0).url : Utils.getRandomCheeseDrawable())
                    .fitCenter().into(holder.mImageView);
        }

        @Override
        public int getItemCount() {
            return news.size();
        }
    }
}