Skip to main content

GrowingListView - growing list view inside the scroll view



GrowingList View

Introduction:

The growing list view is a kind of list view that resembles the recycler view with the same style of the recycler view adapter. This library grows in the height as the height of the items inside dynamically.

Problem Statement:

The recycler view or the list view doesn't grow in size dynamically while being inside the scroll view. Also setting the height dynamically to the items height is programmatically achievable but it behaves differently on different devices.

The growing list view is built to resolve this issue. Include the view provided by the library and include the adapter class in the project and set it to the list and see the magic that the list will be growing in size dynamically with the height of the height.

How to include in your project:

  1. Just download the .aar file from the following link : https://github.com/PranavKAndro/GrowingListView.
  2. Include the .aar file into the project by new-->module-->import .aar module in the android studio.
  3. Sync the project with the gradle.

How to use this view:

In the layout xml include the growing list view as follows,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"`
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="pranav.com.growinglistimpl.MainActivity">

<pranav.com.growinglistview.ui.GrowingListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/growingsample">

</pranav.com.growinglistview.ui.GrowingListView>
</RelativeLayout>
Then create Adapter class extending the GrowingListAdapter,
public class adapter extends GrowingListAdapter {

ArrayList<String> strings ;
public adapter(Context context,ArrayList<String> items) {
    super(context);
    strings=items;
}

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

@Override
public GrowingListViewHolder OnCreateViewHolder(Context context) {
    View v = LayoutInflater.from(context).inflate(R.layout.itemview,null,false);
    adapter.viewHolder holder = new viewHolder(v);
    return holder;
}

@Override
public void OnBindViewHolder(int i, GrowingListViewHolder growingListViewHolder) {

    adapter.viewHolder holder = (adapter.viewHolder)growingListViewHolder;
    holder.sanple.setText(strings.get(i));

}

// create a view holder class 
class viewHolder extends GrowingListViewHolder
{
    TextView sanple;
    public viewHolder(View itemView) {
        super(itemView);
        sanple = (TextView) itemView.findViewById(R.id.textView);
    }
}
}
In the mainActivity set the adapter to the growing list view as follows,
public class MainActivity extends AppCompatActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        GrowingListView growingListView = (GrowingListView) findViewById(R.id.growingsample);
        ArrayList<String> strings = new ArrayList<>();
        strings.add("i am 0");
        strings.add("i am 1");
        strings.add("i am 2");
        strings.add("i am 3");
        strings.add("i am 4");
        strings.add("i am 5");
        strings.add("i am 6");
        adapter adapterq = new adapter(this,strings);
        growingListView.setAdapter(adapterq);
        strings.add("i am 7");
        adapterq.notifyDataSetChanged();
        strings.add(0,"I am changed at postion 0");
        adapterq.notifyDataItemChanged(0);
        growingListView.setOnItemClickListener(this);
    }

    @Override
    public void onClick(View view, int i) 
    {
        Toast.makeText(MainActivity.this, "Hello i am clicked"+i, Toast.LENGTH_SHORT).show();
    }
}

NotifyDataSetChanged() -- call this function once if you change the data in the list to reflect to the UI.
notifyDataItemChanged(int position) -- call this function to change the item at the particular position.
To use the item click listener. Follow the below procedure,
growingListView.setOnClickListener(new OnGrowingListItemClickListener()
{
   @Override
   public void onClick(View view, int i) {
       Toast.makeText(MainActivity.this, "Hello i am clicked"+i, Toast.LENGTH_SHORT).show();
   }
 });
Enjoy using the GrowinListView !!! 👍

Comments

Post a Comment

Popular posts from this blog

How to access the each view of item by position in Recycler View in android ?

How to access the each view of item by position in Recycler View in android ? There are some methods to access the view of each item view to change or update the view during the run time. To access the view of the item view , consider the Recycler view as below, RecyclerView mainRecyclerView = (RecyclerView)view.findViewById(R.id.main_recycler_view); RecyclerAdapter adapter = new RecyclerAdapter(mContext); mainRecyclerView.setAdapter(adapter); main.setLayoutManager(new LinearLayoutManager(mContext));  To access the itemView of each position the following functions can be used,  1. View itemView = mainRecyclerView.getChildAt(positionOfItem);  2. View itemView = mainRecyclerView.findViewHolderForAdapterPosition(Position).itemView;  3. View itemView = mainRecyclerView.findViewHolderForPosition(Position).itemView;  4. Long itemId = mainRecyclerView.getAdapter().getItemId(position);       View itemView = mainRecyclerView....

A.P.I call or async await not working in Array forEach ?

H ello Readers, Welcome back. You would have wondered why does forEach function on the array does not wait for any asynchronous function even if we provide the async await in the node js. If you are the person, wondered about this ever, this post is right for you. Non working example : Lets consider the below snippet, this will not wait for the asynchronous process to wait. I am making a setTimeout to mock the API call async process. This will result the count as, Output : count = 0 OMG !! Why it doesn't work ? Answer probably might be lying inside the Array prototype from JavaScript. Lets take a look inside the "Array.prototype.forEach" function. From the snippet its clear that, the for loop in which they call the callback function does not wait for the asynchronous process to run in the callback. So this forEach is not build for asynchronous process itself. So can't I use forEach any more for running a asynchronous function ? Answer g...

How Machine Learning Works ? - Data Rules the Kingdom

W elcome Back Readers !, For those who have not read the previous post on How Machine Learning works ? Please do read previous one before continuing this so that it would be more easy to understand. You can find the previous post here How Machine Learning Works ? - Mathematics Every Where . For other who are ready to join with me in this journey to understand the Machine Learning, Thank you for being with this post. Till now we are discussing on the the title "How Machine Learning works? " and to understand that we have seen in the first post that there exist the patterns or trends in the data and we try to find that in terms of a mathematical equation called a model and try to fit to the new data that we have not came across for predicting the values. Wow may be we got the entire process complete with this. But there are few thinks that needs to be cared while training the model or creating the model or while processing the data for the model creation. ...