Limiting the number of items in a gridview - android

I am implementing a gridview in Android and my requirement is that I want that only 9 items come on 1 page and if there are more than 9 items in a gridview than rest of them should come on the next page. Is it possible??
Thanks

The method that you override to get count in the Custom Image Adapter that extends Base Adapter. This method gives the number of items to be added.
public class ImageAdapter extends BaseAdapter{
#Override
public int getCount() {
return imageList.size();
}
}

Divide your data set into blocks of nine, give the first set of nine to the adapter that you are using for the GridView. Have a Button for next page give the next data set block to the adapter and tell the adapter that the data set has changed.

Related

GridView Adapter single selection mode

i have one Fragment in that i defined one GridView,so iam attaching listener for the LinearLayout from GridView Adapter so i click the LinearLayout of the Adapter it should change the background color of selected layout..
First in your girdview adapter class take on variable exp.
int selected;
then make on method in grid view like that
public void selectedPosition(int postion)
{
selected = postion;
}
then you can also put a code in getview method of gridview. Like this
if(position==selected)
{
imageView.setBackgroundColor(Color.WHITE);
}
else
{
imageView.setBackgroundColor(Color.parseColor("#578FFF"));
}
now in onitemclick of grid view you can post like that
adapter.selectcrop(position);
adapter.notifyDataSetChanged();
here is a solution example I will give you step by step i think you can solve your problem using that.

Transition in ListView to replace all elements

I have a ListView that displays a list of states. Whenever the user selects a state, I will load a list of cities that are in this state. For this, I have a manager that, whenever an item is selected, will replace the underlying list that my custom BaseAdapter reads with the item's children.
The problem is, this works fine, but the items are just replaced on the spot, I'd really want a nice transition for this, that is, the list of states moving to the left and being replaced by the list of cities coming from the other edge
Is there a class or method that could help me animate that? The only option I've thought is to keep two different Listview and fade the first one out while the other one fades in. Not the most elegant solution, I was wondering if this could be achieved somehow with only one ListView
I'm not sure how to do this with ListView, but I know you could use a RecyclerView and add animations in the adapter's onBindViewHolder method.
This method is called once for every visible item when you call notifyDataSetChanged().
Edit:
Something like this:
Some Class:
RecyclerView recyclerv;
MyAdapter mAdapter;
LinearLayoutManager mLayoutManager;
List<String> mDataset;
//instantiate recyclerview, adapter, layoutmanager, dataset. populate dataset.
recyclerv.setDataset(mDataset);
MyAdapter.java:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<String> dataset;
public MyAdapter(){}
//extend ViewHolder class here
public class ViewHolder extends RecyclerView.Holder { ... }
//implement onCreateViewHolder
/*implement onBindViewHolder.
*animate your view here, but be sure to add an animation count
else the items will keep getting animated everytime a new item appears */
//implement getItemCount
public void setDataset(List<String> dataset) {
this.dataset = dataset;
notifyDataSetChanged(); //this will trigger onBindViewHolder for every visible item
}
}

Android RecyclerView infinity scrolling: how to add more items to dataset

I have implemented my RecyclerView and even added an onscrolllistener to support infinity scrolling and now I'm stuck with a, hopefully, easy problem: How can I add the newly loaded data to the existing dataset?
My current approach: I create a new array with the length of the existing dataset + the length of the newly loaded data. I System.arraycopy my existing dataset and add the new content with a for-loop.
This works but the list is always reset (scrolls back to the top) and I assume my way to add additional content is overly complicated/wrong, though the tutorials I have looked at seem to pass over this "detail".
Update: I'm currently calling "scrollToPosition" on the UI-Thead after the data has been loaded, but I doubt this is the correct way of doing this or am I wrong?
You shouldn't be adding stuff to your dataset, you will sooner or later run out of memory. What you can do is return a big number (I used Short.MAX_VALUE) item in getItemCount inside your adapter and in the method that requests a view for postion you should do position % list.size();
It is not a truly endless RecyclerView this way, but good enough. I will paste some code tomorrow, I don't have it here now :/
I think you have to add items inside your adapter. Let`s say
class Adapter extends Recycler.Adapter<Recycler.ViewHolder>{
List<YourCustomObject> list;
public Adapter(){
list = new ArrayList<>();
}
public void addItem(YourCustomObject item){
list.add(item);
notifyItemDateSetChanged(); //This method for adapter to notice that list size have been changed
}
// Here your views
}
There is implementation of Your fragment or Activity where you retrieve data from internet.Let` say
class MainActivity extends AppCompactActivity{
Adapter adapter = new Adapter();
List<YourCustomObjects> objects;
public void onCreateView(){
//////// Something yours
}
public void onLoadMore(){
///// Your operation to retrieve data and init it to your list objects
for(YourCustomObject object : objects){
adapter.addItem(object);
}
}
}

Setting CustomAdapter for NavigationDrawer

I have integrated Navigation drawer in my app.The issue is that CustomAdapter is not showing the content.However if i use default ArrayAdapter content is shown.I failed to understand where am I going wrong in setting CustomAdapter.
Try to add below api to tell listview the total number of rows in listview :
#Override
public int getCount() {
return optionList.length;
}

How can i hide a listview item in android?

Hi friends i have a listview and the contents are fetched from a webservice call. In that webservice call, there are fields like
"OGType": "ORG" and "OGType": "GROUP"
If click a button, the listview must shows the item having "OGType": "ORG", and hide the item having "OGType": "GROUP". Hope you understand what i meant. Please anyone help me for that. Thanks in Advance.
Try to set new data (only with ORG) to adapter and then call
adapter.notifyDataSetChanged();
You can do it in your getView Method in your Adapter Class. That's the header
public View getView(int pos, View convertView, ViewGroup, parent)
There you can properly hide the element(s) you want, you know, using the method setVisibility()
For more help you can take a look here
You can create a custom adapter and pass data to it in the form of Array or ArrayList (ArrayList is better when dealing with Custom Adapters). Whenever you need to add or remove the data from ListView, just add or remove the item to or from you ArrayList and call notifyDataSetChanged() on your custom adapter and it will update the ListView automatically.
In your case, whenever you click a button, edit you ArrayList and call your custom adapter's method called notifyDataSetChanged() and that's it. You'll see every time you call this method ListView will refresh itself if you have made any changes to the data. Hope it helps.
NOTE - CUSTOM ADAPTER IS NOT COMPULSORY. ANY ADAPTER CAN BE USED e.g SimpleAdapter, ArrayAdapter etc.
You can use a visible list and filters lists. You should use "visible" for complete the BaseAdpter as always, then, you can change the pointer of visible to other list (all, filter...)
Don't worry by the memory, are pointers, you only have each element only once.
public class MyAdapter extends BaseAdapter {
private ArrayList<MyItem> visible;
private ArrayList<MyItem> all;
private ArrayList<MyItem> filter;
public MyAdapter(ArrayList<MyItem> items) {
all = items;
visible = all; //Set all as visible
filter = new ArrayList<Item>();
for (Item i : items)
if (i.getType().equals("ORG"))
filter.add(i);
}
//Complete adapter using "visible"
public void showOnlyOrg() {
visible = filter;
notifydatasetchanged();
}
}
The non hackish way will be to remove the items from your Collection which you use to generate the listview and then call notifyDataSetChanged();

Categories

Resources