2

I'm working to make a cool layout like this:

For this I'm using AndroidParallax l

For this I'm using Android Parallax library from Github. This library is creating all the views (as shown in picture) from xml itself. But I want to create my own recyclerview, create adpaters, model classes and show them with cardview.

I tried using cardview and recyclerview.

Problem:

When I put RecyclerView's height as match_parent (android:layout_height="match_parent"), it gives UI like this:

enter image description here

Only first cardview is display with half part only. Other cardviews are overlapped somehow.

But when I give its height with fixed height like (1000dp, 2000dp), it shows the UI as expected (as shown in first figure). I think it is not a good solution to give its height fixed since data items may differ.

I don't understand what is wrong with my code. Please do suggest some solutions on this.

Following is my different views with my code.

activity_main.xml

<com.github.florent37.parallax.ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:scaleType="centerCrop"
            android:src="@drawable/background"
            android:tag="parallax=0.3" />

        <TextView
            style="@style/MyTitle"
            android:layout_width="match_parent"
            android:layout_height="160dp"
            android:gravity="center"
            android:tag="parallax=0.5"
            android:text="My awesome title" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="150dp"
            android:orientation="vertical">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/my_recycler_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scrollbars="vertical" />
        </LinearLayout>
    </FrameLayout>
</com.github.florent37.parallax.ScrollView>

card_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        card_view:cardCornerRadius="2dp"
        card_view:contentPadding="10dp">

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:text="Hello"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/textView"
                android:layout_marginTop="10dp"
                android:text="world" />
        </RelativeLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>

My Adapter class is like this:

public class MyRecyclerViewAdapter extends RecyclerView
        .Adapter<MyRecyclerViewAdapter
        .DataObjectHolder> {

    private ArrayList<ContactsModel> mDataset;
    private static MyClickListener myClickListener;

    public static class DataObjectHolder extends RecyclerView.ViewHolder
            implements View
            .OnClickListener {
        TextView label;
        TextView dateTime;

        public DataObjectHolder(View itemView) {
            super(itemView);
            label = (TextView) itemView.findViewById(R.id.textView);
            dateTime = (TextView) itemView.findViewById(R.id.textView2);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            //myClickListener.onItemClick(getAdapterPosition(), v);
        }
    }

    public void setOnItemClickListener(MyClickListener myClickListener) {
        this.myClickListener = myClickListener;
    }

    public MyRecyclerViewAdapter(ArrayList<ContactsModel> myDataset) {
        mDataset = myDataset;
    }

    @Override
    public DataObjectHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.card_view, parent, false);

        DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
        return dataObjectHolder;
    }

    @Override
    public void onBindViewHolder(DataObjectHolder holder, int position) {
        holder.label.setText(mDataset.get(position).getmText1());
        holder.dateTime.setText(mDataset.get(position).getmText2());
    }

    public void addItem(ContactsModel dataObj, int index) {
        mDataset.add(index, dataObj);
        notifyItemInserted(index);
    }

    public void deleteItem(int index) {
        mDataset.remove(index);
        notifyItemRemoved(index);
    }

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

    public interface MyClickListener {
        public void onItemClick(int position, View v);
    }
}

In MainActivity, I have written code like this:

public class MainActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mAdapter = new MyRecyclerViewAdapter(getDataSet());
        mRecyclerView.setAdapter(mAdapter);



        Fabric.with(this, new Crashlytics());
    }
    private ArrayList<ContactsModel> getDataSet() {
        ArrayList results = new ArrayList<ContactsModel>();
        for (int index = 0; index < 20; index++) {
            ContactsModel obj = new ContactsModel("Some Primary Text " + index,
                    "Secondary " + index);
            results.add(index, obj);
        }
        return results;
    }
}

Model class is like:

public class ContactsModel {
    private String mText1;
    private String mText2;

    ContactsModel (String text1, String text2){
        mText1 = text1;
        mText2 = text2;
    }

    public String getmText1() {
        return mText1;
    }

    public void setmText1(String mText1) {
        this.mText1 = mText1;
    }

    public String getmText2() {
        return mText2;
    }

    public void setmText2(String mText2) {
        this.mText2 = mText2;
    }
}

Apologies for the very long bulk of code.

Thanks!

Kishor Bikram Oli
  • 1,748
  • 1
  • 22
  • 40
  • Is there a reason you aren't using the parallax support built into [CollapsingToolbarLayout](https://developer.android.com/reference/android/support/design/widget/CollapsingToolbarLayout.html) and the built in ability to [overlap your AppBarLayout](https://stackoverflow.com/questions/31039074/overlap-scrolling-view-with-appbarlayout)? – ianhanniballake Jun 01 '17 at 18:02
  • I have had no idea on CollapsingToolbarLayout so when surfing on internet, I found this library bit cool, so I went with it. – Kishor Bikram Oli Jun 01 '17 at 18:09
  • @ianhanniballake Is collapsing toolbar similar like this? I have had spent lot of time on this library, so is it good idea switching to CollapsingToolbar keeping this Parallax library aside. Please let me know. Thanks. – Kishor Bikram Oli Jun 01 '17 at 18:13

3 Answers3

13

You have a RecyclerView (which is scrollable) within a Scrollable Layout.

Replace your ScrollView tag with "android.support.v4.widget.NestedScrollView". This will allow the RecyclerView to properly expand.

<android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

...

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical" />

</android.support.v4.widget.NestedScrollView>
Josh Beckwith
  • 1,432
  • 3
  • 20
  • 38
5

After Replaceing your ScrollView with NestedScrollView you can just add this line into the NestedScrollView

android:fillViewport="true"
Alale sa
  • 147
  • 2
  • 9
1

I was having a same problem in my NestedScrollView.

Just added one line inside NestedScrollView and my problem solved.

android:fillViewport="true"