How to click button inside Activity to change CustomeView item attributes - android

I have an activity which contains one button and one RecyclerView. The RecyclerView is done by the standard way - AdapterView and ViewHolder(inner class for the Adapter).
Inside the ViewHolder I have an image and text on CardView.
I want to show/hide the image inside the CardView,inside the ViewHolder by clicking the button in my Activity Class.
However I cannot figure it how - I cannot instantiate ViewHolder are access it as static.
When I instantiate the Adapter and call notifyItemChange() it also does not work.
Any help on how to do it?

You can use two different approaches:
GreenRobot EventBus or any other even bus. Dispatch an event from the button click; and any card cell will listen and show/hide depending of the event
every cell view holder would need a model (similar to array of items for a ListView). This model may have a boolean showImage(true/false)
when button is clicked, iterate through the array of models calling Model.showImage(true)
after updating every mode, call Adapter.notifyDataSetChanged()
in [Recycler.onBindViewHolder()](http://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html#bindViewHolder(VH, int)) get a reference for the model of that position and show/hide the image based on the model boolean behind Model.showImage()

Related

Hide checkboxes when button is clicked

I have a RecyclerView.Adapter setted in a RecycleView which belongs to an Activity. This Activity contains a button. This adapter contains rows which have checkboxes. I would like to hide checkboxes of all rows when this button is clicked.
I'm using ViewHolder's pattern in my adapter.
I'm thinking in this solution:
Pass in my activity as a parameter to adapter's constructor. This activity contains a method to verify if button was clicked. And when this button is clicked the value changes and calls notifyDataSetChanged(). So the adapter shows/hides checkboxes.
Based on a MVP approach is this a good idea?
Possible solutions:
Each view holder is related to a model class. in this model class, you can hold a boolean that indicates if the corresponding view holder should show the checkbox.
When the button is clicked you can iterate over the items that should be affected and change the boolean to true. Then notify data changed on the affected items.
The adapter can hold a boolean that indicates if all the checkboxes should be hidden.
Each view holder will receive the adapter as a reference upon creation (and not the activity - which is bad) and will check this boolean whether to show or hide the checkbox.
The adapter boolean will be updated when the button is clicked and then will trigger a notify data changed.

Difference between setting onClickListener in adapter and in fragment

In my code I have fragment and gridview in it. Also I have ArrayAdapter for this Gridview. Now I need to change background color of grid cell on click. I do this by setting onClickListener.
Question is what is difference between setting onClickLister for GridView cell in adapter and in fragment?
I'm gonna try to clarify the different android entities involved in your question a little.
A GridView is a View.
You can assign Click Listeners to Views so they react to a user click. Any view has a generic clicklistener (View.setOnClickListener) that gets called whenever the user clicks on any part of the view
Complex views can have several other more specialized clicklisteners, for example, menu-like views (ListViews, GridViews, etc) will have also a setOnItemClickListener / setOnItemLongClickListener that gets called whenever the user clicks on an item (vs. the whole view)
An Adapter is just a class whose purpose is to build views with data to data-consuming views. For example, your GridViewAdapter: It will get called once for every row and it will construct each Row View (in the getView method). Every Row View will be (probably) a ViewGroup (FrameLayout/RelativeLayout...) with some other views inside (ie. Icon ImageView, name TextView, address...)
So the adapter itself doesn't accept clicklisteners. But the Views created by the adapter can! For example, let's assume your GridView is a Phone List:
Your GridView has an ItemClickListener to react to the selected phone list entry and show info about the contact
Your GridView adapter builds views for every row. Imagine your 'contact' rows have 3 views: A title, an Icon, and a button to delete the contact
Inside your adapter, you will assign an onClickListener to the "delete contact" button View. Mind you always assign onClickListeners to Views, not to the adapter itself ("you can't click an adapter!")
About Fragments, think of them as "sub-activities". A fragment contains a root layout with several views. Again, it will be in those views where you assign the clicklisteners, not to the fragment itself.

Android setOnItemClickListener vs setOnClickListener

As I know, there are two methods to handle click on different list items:
Use setTag() to set types for items of list in Adapter, then setOnItemClickListener() for the list and getTag() of the view to differentiate the type, like this:
listview.setOnItemClickListener(new OnItemClcikListener(){});
Inside the adapter, setOnClickListener() individually for each item during getView(), like this:
item.setOnClickListener(new OnClickListener() {});
What is the difference and which one is preferred?
OnItemClickListener is very easy to manage compare to OnClickListener.
If you still would love to manage OnClickListener I will tell why OnItemClickListener is much better than OnClickListener.
Once you start scrolling ListView items will start getting reused and you ended up creating lot of OnClickListener. Don't worry, this is not a memory leak as GC will come into picture and collect those but you should not feel safe also because GC pauses your activity even if it is fraction of second that's considerable.
So would I go with OnItemClickListener unless you planned something different for individual list item.
If you need to create specific portions of each item to be clickable or want more than one action to be performed for a given item, it would be best if possible to collect those actions into a single OnClickListener that is created once and then attached to each item in getView(). You can differentiate which item was clicked by attaching metadata about the click action and maybe list position to the views themselves with setTag().
I'm not sure i really understood what do you mean but i will try to give you a reply.
Use setTag() to set types for items of list in Adapter, then setOnItemClickListener() for the list and getTag() of the view to differentiate the type, like this:
listview.setOnItemClickListener(new OnItemClcikListener(){});
You can use setTag() for set an Object like a tag, it means you can use it to put some informations into your cell view (ex: text, id and so on).
To "differentiate the type" of your view i would suggest you to use `getViewTypeCount().
inside the adapter, setOnClickListener() individually for each item during getView(), like this:
item.setOnClickListener(new OnClickListener() {});
This actually depends more on what you want to do with your list, if the behaviour of the click is strictly related to the informations which belong to your adapter or for example if you have a button inside your cell view, then set a listener for the cell view inside the adapter could be a good solution.
But except for the last case i would say the first is the best solution, since you can put everything you want into your tag, and it gives you the chance to manage your list's click from your main Activity or Fragment.

how to get focus on the items of a listview toching the screen and moving the finger upper and down withoud untouch it - Android

I have a listview with two columns where the user can compare the productName and proudctQty against the stock. There are loads of products in that list and the user may get confuse checking all of them. So, the idea is to highlight the products and their price by using focus.
Does anyone know if would be possible to implement it in listview like on the virtual keyboard (it isnot onLongTouch or click)?
Could anyone give a hint about how to do it?
Many thanks
ok like in case of wheelpicker iPhone we can highlight the product just follow these steps
implement the list's onscrollchangedlistener .
put the list in a relative layout with another view at the centerinparent and fillparent
on this view you will display the highlighted item.
as the list is scrolled update the content over that view inside the overrided onscroll method.
You need to use a View Holder Pattern. You can find more information on Google.
Simply create a class called AdapterViewHolder, declare properties, the Views on each row.
For example:
public class AdapterViewHolder
{
ImageView myImageView;
TextView myTextView;
...
}
In constructor pass your values to set to the views, initialize your views and set your values like:
public AdapterViewHolder(String param, Drawable imageDrawable){
// set values to views
}
In the adapter in getView method:
if convertView == null
//then create your layout and holder class, set your holder to your layout by view.setTag method, prepare your view and return it
else
//get your layout tag by view.getTag(), cast it to AdapterViewHolder and update your views and values of them as you like and then return updated view.
For more info search for ViewHolder Pattern in android listviews.

Android ListView: How to use the activity onClickListener from a custom Adapter?

I have a listView using a custom adapter. Each row contains a button and some other Views. I want to be able to click either on the button, or on the row itself (to edit the item of the list that is clicked).
Setting an onItemClickListener in the activity won't work because of this problem
I think I have to set an onClickListener in the getView() method of my adapter for it to work properly.
I would like to use my activity's onClickListener, in order to use a startActivityForResult() when the row is clicked, in order to have something returned to my activity when the item edition activity is over.
How can I do that?
Thanks!
You'll need to add an onclick listener to every button you add to every row. The best way to do this is probably to make your own custom layout in code, and every time you create a new view in your adapter, set the onclick listener in the layout code.

Categories

Resources