Search in sources :

Example 1 with Article

use of com.forcelain.android.awesomerecyclerview.model.Article in project AwesomeRecyclerView by forceLain.

the class ArticleAdapter method onBindViewHolder.

@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
    ArticleViewHolder articleViewHolder = (ArticleViewHolder) viewHolder;
    Article article = articles.get(position);
    articleViewHolder.textContent.setText(article.text);
    articleViewHolder.textTitle.setText(article.title);
    if (article.image.startsWith(SCHEME_ASSETS)) {
        String fileName = article.image.replace(SCHEME_ASSETS, "");
        AssetManager assetManager = viewHolder.itemView.getContext().getAssets();
        InputStream is = null;
        try {
            is = assetManager.open(fileName);
            Bitmap bitmap = BitmapFactory.decodeStream(is);
            articleViewHolder.imageView.setImageBitmap(bitmap);
        } catch (IOException e) {
            Log.e(TAG, Log.getStackTraceString(e));
        } finally {
            IOUtils.closeQuietly(is);
        }
    }
    viewHolder.itemView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (itemClickListener != null) {
                itemClickListener.onItemClicked(position);
            }
        }
    });
}
Also used : Bitmap(android.graphics.Bitmap) AssetManager(android.content.res.AssetManager) InputStream(java.io.InputStream) Article(com.forcelain.android.awesomerecyclerview.model.Article) IOException(java.io.IOException) ImageView(android.widget.ImageView) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) View(android.view.View)

Example 2 with Article

use of com.forcelain.android.awesomerecyclerview.model.Article in project AwesomeRecyclerView by forceLain.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    layoutManager = new AwesomeLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);
    ArticleAdapter adapter = new ArticleAdapter();
    recyclerView.setAdapter(adapter);
    dataProvider = new FakeDataProvider(this);
    List<Article> articles = dataProvider.getArticles();
    adapter.setArticles(articles);
    recyclerView.setChildDrawingOrderCallback(new RecyclerView.ChildDrawingOrderCallback() {

        @Override
        public int onGetChildDrawingOrder(int childCount, int i) {
            return childCount - i - 1;
        }
    });
    adapter.setItemClickListener(new ArticleAdapter.OnItemClickListener() {

        @Override
        public void onItemClicked(int pos) {
            layoutManager.openItem(pos);
        }
    });
    findViewById(R.id.test_button).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            recyclerView.smoothScrollToPosition(4);
        }
    });
}
Also used : FakeDataProvider(com.forcelain.android.awesomerecyclerview.data.FakeDataProvider) Article(com.forcelain.android.awesomerecyclerview.model.Article) RecyclerView(android.support.v7.widget.RecyclerView) AwesomeLayoutManager(com.forcelain.android.awesomerecyclerview.view.AwesomeLayoutManager) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View) ArticleAdapter(com.forcelain.android.awesomerecyclerview.view.ArticleAdapter)

Example 3 with Article

use of com.forcelain.android.awesomerecyclerview.model.Article in project AwesomeRecyclerView by forceLain.

the class FakeDataProvider method getArticles.

@Override
public List<Article> getArticles() {
    Gson gson = new Gson();
    Article[] articles = gson.fromJson(new InputStreamReader(context.getResources().openRawResource(R.raw.data)), Article[].class);
    return new ArrayList<>(Arrays.asList(articles));
}
Also used : InputStreamReader(java.io.InputStreamReader) Article(com.forcelain.android.awesomerecyclerview.model.Article) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson)

Aggregations

Article (com.forcelain.android.awesomerecyclerview.model.Article)3 RecyclerView (android.support.v7.widget.RecyclerView)2 View (android.view.View)2 AssetManager (android.content.res.AssetManager)1 Bitmap (android.graphics.Bitmap)1 ImageView (android.widget.ImageView)1 TextView (android.widget.TextView)1 FakeDataProvider (com.forcelain.android.awesomerecyclerview.data.FakeDataProvider)1 ArticleAdapter (com.forcelain.android.awesomerecyclerview.view.ArticleAdapter)1 AwesomeLayoutManager (com.forcelain.android.awesomerecyclerview.view.AwesomeLayoutManager)1 Gson (com.google.gson.Gson)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 ArrayList (java.util.ArrayList)1