how to hide a view in all recyclerview row after button clicked? - java

I want to create a recycler view like below images, when the user clicks on radio Button seek bar visible and radio button invisible in all recycler view's items.
brfore click image and after click image
this is my Recycler view adapter code:
#Override
public void onBindViewHolder(#NonNull final VoteRvViewHolder voteRvViewHolder, final int i) {
final VoteRecyclerViewDataModel dataModel = voteRecyclerViewDataModels.get(i);
voteRvViewHolder.optionContent.setText(dataModel.getOptionContent());
voteRvViewHolder.optionPercent.setText(dataModel.getPercentOption());
voteRvViewHolder.seekbar.setProgress(Integer.parseInt(dataModel.getPercentOption()));
voteRvViewHolder.seekbar.setVisibility(View.INVISIBLE);
voteRvViewHolder.optionPercent.setVisibility(View.INVISIBLE);
voteRvViewHolder.radioBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (voteOptionClickListener != null) {
if (i != RecyclerView.NO_POSITION) {
if (wholeView != null) {
voteOptionClickListener.OnVoteOprionClick(wholeView, voteRecyclerViewDataModels.get(i), i);
voteRvViewHolder.seekbar.setVisibility(View.VISIBLE);
voteRvViewHolder.optionPercent.setVisibility(View.VISIBLE);
}
}
}
}
});
Problem is when I click on one of the radio button that row is changed I want all rows changed

Maintain click state in data list of Recycler view using variable and put layout(seekbar/ radio button) in Frame layout.On radio button click change the variable in data list .On the basis of that variable you can display radio button/Seekbar in onBindView holder.

Related

Google map Info window won't open at first click, instead it will show on second click (ANDROID JAVA)

I am using below code to open up an infowindow in my map:
//Define popup
private View popup=null;
mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter(){
#Override
public View getInfoWindow(Marker marker) {
return null;
}
#Override
public View getInfoContents(final Marker marker) {
if(popup == null){
popup = getLayoutInflater().inflate(R.layout.custom_adress,null);
}
final ImageView customadresimage = (ImageView) popup.findViewById(R.id.customadresimage); //my imageview
Picasso.get().load(marker.getSnippet()).resize(200,200).into(customadresimage); //save and show photo from firebase
return popup;
}
});
When I clicked pin, photo dont show firstly, when i clicked secondly, photo is shown.
First Click
Second Click:
What is wrong ?

How to set an onClickListener in android for anywhere except an element on the screen

So, I have an activity in android with an attach button on the Action bar.
When I click the button, two floating action buttons appear (one for image, one for camera). When I click the button again, they disappear.
Now, I want to make it such that if the user clicks anywhere on the screen that isn't one of those two FABs, that they will disappear. I already have the action / commands to make them disappear; however, I am looking for how to set up an onClickListener for the whole screen EXCEPT the region covered by the two FABs.
I was solved the same scenario you can compare it with my scenario and can apply on yours :
my scenario : Hide the keyboard on touch of screen any where accept EditText.
Solution : Set up touch listener for non-text box views to hide keyboard.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
setupUI(findViewById(R.id.scrolview)); // === scrolview is container of all views inside it ===
}
public void setupUI(View view) {
//Set up touch listener for non-text box views to hide keyboard.
if(!(view instanceof EditText)) {
view.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
hideSoftKeyboard(SignupActivity.this);
findViewById( R.id.first_linear_layout ).requestFocus();
return false;
}
});
}
//If a layout container, iterate over children and seed recursion.
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
View innerView = ((ViewGroup) view).getChildAt(i);
setupUI(innerView);
}
}
}
public static void hideSoftKeyboard(Activity activity) {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}

How to select only one item at a time and avoid refresh checkbox while expand list in android [duplicate]

I'm making NLevel expandable list using listview. I've added checkbox only last level data in list view.
I want to check only one checkbox at a time. Please any one help me!
Here my code
#Override
public View getView(final NLevelItem item) {
// .......
final CheckBox checkBox = (CheckBox)view.findViewById(R.id.check);
checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//store checkbox state, note that NLevelItem item might need to be defined with 'final'
item.setChecked(checkBox.isChecked());
if(checkBox.isChecked()){
tempList.add((String) ((SomeObject)childs.getWrappedObject()).getName()+"+"+name);
}
else {
tempList.remove((String) ((SomeObject)childs.getWrappedObject()).getName()+"+"+name);
}
}//onClick()
}//setOnClickListener()
//update checkbox state from the corresponding NLevelItem
checkBox.setChecked(item.isChecked());
//.......
}//getView()
Thanks in advance....
There's RadioButton in RadioGroup for doing that, why would you want to use checkbox

How to get the value of a TextView that is part of a ListView row in all checked items

As illustrated in the photo, i have a list view that is made up of a custom layout which has two TextView. One TextView is for storing numbers which has a visibility of gone, the other is for storing the name, which is visible.
all i want is to be able to get a string of all numbers that are selected when the send button is clicked.
A good suggestion here is to use a recyclerview instead of list view.
To achieve want you want with a list view you just set an item click listener to your list view in your activity. The item click listener will pass the View which is the current cell in the list view. Then you can find textview by id to locate. The Textview ID is set in the layout xml.
Example
listView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View cell,int pos, long arg1)
{
TextView textView = (TextView)cell.findViewByID(R.id.myTextView);
String texViewContents = textView.getText().toString();
}
});

How to keep track of the selected items in a RecyclerView in Android?

How does one achieve a consistent item selection with RecyclerView ? I get the whole idea of RecyclerView recycling old views and what not. My problem is I have a list of items with an icon on each item, when an item is clicked, the icon changes colour. I managed to achieve all of this, but I just realised, as I scroll down the list, that other items have their icons changed too, and when I scroll back to the item I clicked on, the icon is no longer in the "clicked color".
Does anyone know how to keep track of the selected items? I keep seeing something called SparseBooleanArray, but I am not sure how to implement it.
Here is my adapter code:
public class TableRVAdapter extends RecyclerView.Adapter<TableRVAdapter.TableHolder> {
List<Tables> tableList;
private SparseBooleanArray selectedItems;
public TableRVAdapter(List<Tables> tableList)
{
this.tableList = tableList;
selectedItems = new SparseBooleanArray();
// setHasStableIds(true);
}
class TableHolder extends RecyclerView.ViewHolder
{
TextView tableTV;
CardView tableCV;
View circle;
View parentView;
TableHolder(final View itemView)
{
super(itemView);
tableTV = (TextView)itemView.findViewById(R.id.tableTV);
tableCV = (CardView)itemView.findViewById(R.id.tableCV);
circle = itemView.findViewById(R.id.statusCircle);
itemView.setClickable(true);
parentView = itemView;
tableCV.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
circle.setBackgroundResource(R.drawable.circle);
}
});
}
/*
#Override
public void onClick(View view) {
if (selectedItems.get(getAdapterPosition(), false)) {
tableCV.setSelected(false);
circle.setBackgroundResource(R.drawable.circle2);
}
else {
selectedItems.put(getAdapterPosition(), true);
tableCV.setSelected(true);
circle.setBackgroundResource(R.drawable.circle);
}
}*/
}
#Override
public TableHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.table_item,parent,false);
TableHolder tableHolder = new TableHolder(view);
return tableHolder;
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView)
{
super.onAttachedToRecyclerView(recyclerView);
}
#Override
public void onBindViewHolder(TableHolder holder, int position) {
holder.tableTV.setText(tableList.get(position).getTableNumber());
}
/**
* Returns the total number of items in the data set hold by the adapter.
*
* #return The total number of items in this adapter.
*/
#Override
public int getItemCount() {
return tableList.size();
}
}
I know this question is already answered but someone might find this answer useful and easier.
There is one useful method in the recycler view is onViewRecycled.
This can help you to get away from the pressed state. When working with the images/drawables in the recylcer view when some of the elements doesn't use the drawables, previews recycler view's drawn image will be present and it makes look all the data wrong as it happened with you with your selected view.
To achieve this in a more simpler way you can use this method to clear the drawn images to reset its values.
For example, you mark you selected element's background to green, and some of your views contains images(Note: Only some of your elements), than use this method as like this:
#Override
public void onViewRecycled(MyViewHolder holder) {
super.onViewRecycled(holder);
// Set ImageView's Drawable as transperent
holder.myImageView.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), android.R.color.transparent));
// Set background color as transperent
holder.bgContainer.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), android.R.color.transparent));
}
And now you can manage an ArrayList of integers with ids/position of selected elements (using onClick method on recycler view's item).
With onBindViewHolder(MyViewHolder myViewHolder, int position) you can normally fill the data. And for setting background you can simply check if the id/position is in ArrayList or not (If true, set background color green else leave it as it is. As it will be managed by onViewRecylcer method and wont affect other scrolled items with green background even if it is not selected).
Hope it will help someone and make the recyler view with selection a bit more easier.
RecyclerView.adapter has got 2 important functions to override:
onCreateViewHolder(parent, viewType)
onBindVIewHolder(viewholder, position)
The first function is used to inflate views that will be used inside the recyclerview, the second is used to bind the data you have to this view, and thus set the correct viewstate on the view.
The recyclerview itself will only inflate a certain amount of views and then will start to re-use already inflated views (hence recyclerview). So you need to set the correct state for each item in onBindViewholder() and use the item in your collection on that position to set the correct viewState.
For your example: change the color of the icon, dependent on a boolean value in your objects, eg : isPressed
You have to save the instance like this
if (isPressed) {
icon.setCustomIcon();
} else {
icon.setDefaultIcon();
}
if you have a custom object to have a boolean isPressed or something similar, this should work

Categories