0

I just started to learn Android and was having a blockage in this tutorial, spent quite a bit of time to figure this out but couldn't make it. How do I solve this?

I was following this tutorial: http://frogermcs.github.io/Instagram-with-Material-Design-concept-is-getting-real/

I wanted to display item in the RecycleView but nothing shown and whenever I add integer in my itemCount I get error which stated below.

Here's the error:

android.view.InflateException: Binary XML file line #22: Error inflating class learningAndroid.view.SquaredFrameLayout

and is pointed to this line.

at learningAndroid.Adapter.FeedAdapter.onCreateViewHolder(FeedAdapter.java:34)

<?xml version="1.0" encoding="utf-8"?><!-- item_feed.xml -->
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="8dp"
card_view:cardCornerRadius="4dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <!-- Add user profile image -->
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/wink" />

    <learningAndroid.view.SquaredImageView
        android:id="@+id/vImageRoot"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/ivFeedCenter"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop" />
    </learningAndroid.view.SquaredImageView>

    <ImageView
        android:id="@+id/ivFeedBottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>
</android.support.v7.widget.CardView>

Here's my code.

package learningAndroid.Adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;

import butterknife.ButterKnife;
import butterknife.InjectView;
import learningAndroid.R;
import learningAndroid.ui.Utils;
import learningAndroid.view.SquaredImageView;

/**
 * Created by Win 7 on 7/8/2015.
 */

public class FeedAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int ANIMATED_ITEMS_COUNT = 2;

    private Context context;
    private int lastAnimatedPosition = -1;
    private int itemsCount;

    public FeedAdapter(Context context) {
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_feed, parent, false);
        return new CellFeedViewHolder(view);
    }

    private void runEnterAnimation(View view, int position) {
        if (position >= ANIMATED_ITEMS_COUNT - 1) {
            return;
        }

        if (position > lastAnimatedPosition) {
            lastAnimatedPosition = position;
            view.setTranslationY(Utils.getScreenHeight(context));
            view.animate()
                    .translationY(0)
                    .setInterpolator(new DecelerateInterpolator(3.f))
                    .setDuration(700)
                    .start();
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        runEnterAnimation(viewHolder.itemView, position);
        CellFeedViewHolder holder = (CellFeedViewHolder) viewHolder;
        if (position % 2 == 0) {
            holder.ivFeedCenter.setImageResource(R.drawable.wink);
            holder.ivFeedBottom.setImageResource(R.drawable.wink);
        } else {
            holder.ivFeedCenter.setImageResource(R.drawable.wink);
            holder.ivFeedBottom.setImageResource(R.drawable.wink);
        }
    }

    @Override
    public int getItemCount() {
        return itemsCount;
    }

    public static class CellFeedViewHolder extends RecyclerView.ViewHolder {
        @InjectView(R.id.ivFeedCenter)
        SquaredImageView ivFeedCenter;
        @InjectView(R.id.ivFeedBottom)
        ImageView ivFeedBottom;

        public CellFeedViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }

    public void updateItems() {
        itemsCount = 10;
        notifyDataSetChanged();
    }
}

1 Answer 1

1

I think that your problem is with:

<learningAndroid.view.SquaredImageView
    android:id="@+id/vImageRoot"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

<ImageView
    android:id="@+id/ivFeedCenter"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="centerCrop" />
</learningAndroid.view.SquaredImageView>

If you are using similar code to this and this, your ImageView cannot be inside SquaredImageView, because SquaredImageView is not a layout, it extends from `ImageView.

You should change it to SquaredFrameLayout.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.