animatedVectorDrawable is not animating in for loop - java

I have been trying to implement animation vectors . I managed to do it correctly but there's seem to be a problem.
I used recycler view to display all contacts.
I have used selected and unselected functions to play animations on contact profile pic. I did it and selecting and unselecting animations have been working correctly but when I tried to unselect all selected contacts through this loop it seems to be not working and it only happens when I want to unselect them all .
private void deselectAll(viewHolder holder) {
for(int i = 0; i < selected_contacts.size(); i++){
unselected(holder, contacts.indexOf(selected_contacts.get(i)),true);
}
selected_contacts.clear();
}
i have used unselected function which's code is down below. can anybody help me with this problem here cause i cant seem to figure it out.
private void unselected(viewHolder holder, int position, boolean b) {
if(!b){
selected_contacts.remove(contacts.get(position));
}
contacts.get(position).setSelected(false);
AnimatedVectorDrawable animatedVectorDrawable = (AnimatedVectorDrawable) AppCompatResources.getDrawable(context, R.drawable.item_unselected);
holder.image2.setBackground(animatedVectorDrawable);
animatedVectorDrawable.start();
Contact.selectedItems--;
Log.i("items", "numbers of selected items are updated to " + Contact.selectedItems);
String top_text = Contact.selectedItems + " Contacts selected";
selectedItems.setText(top_text);
Log.i("animation", "animation of item no " + position + " is unselected");
}
And also animation is not working even for single item selected when tried to use setImageDrawable instead of setBackground idk what's the difference.
If you wanna see my whole project here is link to My project :
https://github.com/kevaldonga/contacts

Related

Changing Background based on update of TextView

I am trying to create a simple android app using JAVA(Android Studio). What I am willing to implement is the background color of the app to change based on the content of the TextView (which is updated constantly between two values "Open" and "Closed"). I have seen that onChanged() must be used but I don`t really understand how to Implement the listener for the TextView.
I tried wrote this :
measurementValue = (TextView) findViewById(R.id.textbox_main); measurementValue.addTextChangedListener(new TextWatcher() {}
based on what I have found online, but can`t really figure out what the next step is. I am a total newbie so please be kind.
Best regards!
You can listen for text change on those 3 callbacks. It depends on your use case.
#Override
public void onTextChanged(CharSequence textInput, int start, int before, int count) {
String strInput = textInput.toString();
if ("open".equalsIgnoreCase(strInput)) {
// text is "open"
// change background to desired color
} else if ("closed".equalsIgnoreCase(strInput)) {
// text is "closed"
// change background to desired color
}
}

How to get the views which are not visible currently in the linear layout of the recycler view

I was designing an Instagram story type template. I am stuck at a very weird problem. I have used recycler view in the main activity.
MainActivity: (This is just the layout shown. I have change the orientation to Horizontal).
My layout:
Then I have designed a custom adapter and in the layout I have used a linearLayout. When clicked on each view It opens a new Activity which shows the whole story content.
Just like in Instagram when a user opens any story, the user can click on the right side of the screen to get to the next story, or left of the screen to get to the previous one. I tried to implement this same functionality. Opening the story was implemented successfully. The problem comes when I added the functionality of right and left click on the screen. I added two button; one to the right and one to the left. The problem is like, if there are currently 3 views visible, then I can navigate in between these stories only and not to the stories which are not visible in the screen because of the recycler view.
The below code is for right and left clicks
leftArrow = findViewById(R.id.leftArrow);
leftArrow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(myAdapter.currentPosition - 1 >= 0) {
int firstVis = MainActivity.linearLayoutManager.findFirstVisibleItemPosition();
MainActivity.linearLayoutManager.scrollToPosition(firstVis - 1);
rightOrLeftClicks(myAdapter.currentPosition - 1);
}
}
});
rightArrow = findViewById(R.id.rightArrow);
rightArrow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(myAdapter.currentPosition + 1 < myAdapter.localDataSet.length) {
int lastVis = MainActivity.linearLayoutManager.findLastVisibleItemPosition();
MainActivity.linearLayoutManager.scrollToPosition(lastVis + 1);
rightOrLeftClicks(myAdapter.currentPosition + 1);
}
}
});
}
Below code is of the function rightOrLeftClicks
public void rightOrLeftClicks(int position) {
finish();
Log.d("rl", "working");
nextView = MainActivity.linearLayoutManager.getChildAt(position);
Log.d("ll", "The last element is " + MainActivity.linearLayoutManager.findLastVisibleItemPosition());
if(nextView != null)myAdapter.onClickView(position, myAdapter.localDataSet[position].symptom, nextView);
}
Below code is for onClickView, It is same code for clicking any view (Story) or clicking the right or left buttons. Basically I just opened the another activity by passing an intent with the next or previous view I obtained when clicked on right or left respectively.
public static void onClickView(int position, String element, View v) {
Intent intent = new Intent(v.getContext(), MainActivity2.class);
idPosition = "";
idPosition = element;
ArrayList<String> passingContent = new ArrayList<>();
currentPosition = position;
passingContent.add(localDataSet[position].description);
passingContent.add(localDataSet[position].imageUrl);
intent.putExtra(element + "", passingContent);
v.getContext().startActivity(intent);
}
The problem is it only gives the visible views to me. Not the rest of the views. I tried auto scrolling so that the next or the previous view become visible but it doesn't seems to work. The last visible position of the child remains the same and it return a null view to me when I try to open the view just after the last visible view.
Assume there are 5 stories. Then I will see only 3 stories in the screen the rest can be seen by scrolling. When I click any of the stories. I can only navigate between 1,2 and 3. When I try to click right from story 3 to see the story 4. The current story is killed by finish() function and null is returned in the nextView variable because of which the story 4 is not loaded and I am returned to the MainActivity.
I have to navigate to all stories present in my recycler view.
Do these changes may help:
1/
I think you should use ViewPager. It automatically snaps to the item without scrollTo to index. Or keep using RecyclerView with the help of SnapHelper
2/
Modify onClickView method not to be static, and don't need View v to work. I see you just need View v just for the context. Why not just pass the context to that method. It's not proper to use a parameter like that, and that approach traps you into 3/ problem.
3/
nextView = MainActivity.linearLayoutManager.getChildAt(position);
You already know how RecyclerView works, just avoid using getChildAt because in some cases, the child you want hasn't been created yet. Base on your code, I think you don't even need to get the nextView
As told by Tam Huynh. The 1st point helped me in getting the new views in the linear layout. The views were working the fine.
There was a problem in the parameter of the function getChildAt. I have to pass the same position I was in previously. Because relatively the position(index) will not change for the child views.
index 0 -> story number 1
index 1 -> story number 2
index 2 -> story number 3
Like if there were story number 1,2 and 3 visible, the index 0 will contain 1st story, index 1 will contain 2nd and index 2 will contain 3rd story. when user clicked the right button on the 3rd story. The 4th story is first visible and now the screen have 2,3 and 4 visible. but the index of the 4th story will remain 3rd only. As now indexing will be like
index 0 -> story number 2
index 1 -> story number 3
index 2 -> story number 4
So, instead of passing position as parameter, currentPosition should be passed in getChildAt

how to override the position of an onItemClick in Android

So I have a button, and this button switches the cell of a listview with the cell above, and vise versa for ANOTHER button which is for down (this one is for up... It doesn't matter which, I just decided to talk about this one). The whole button and list view thing is working, but my problem is when I press the up button, and then decide to press it again, it just acts as a down button. The reason for this is because it's still stuck on the same item/position of the list view, and I need to figure out a way to Override the position of the onItemClick in the code??
```
upButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//upButton onClick
Integer myTeam = position; //7 -159
Integer otherTeam = position - 1; //6 - 1678
Map<Integer, String> onClickMap = sortByValue(Constants.picklistMap);
String extraValue;
Log.e("myposition", myTeam.toString());
Log.e("otherposition", otherTeam.toString());
extraValue = onClickMap.get(myTeam); //159
String team = onClickMap.get(otherTeam);
Constants.picklistMap.put(myTeam, onClickMap.get(otherTeam));
Constants.picklistMap.put(otherTeam, extraValue); //6
Log.e("Position: ",Constants.picklistMap.get(position));
Log.e("Position - 1: ",Constants.picklistMap.get(position - 1));
if(myTeam != 0) {
dref.child("picklist").child(myTeam.toString()).setValue(Integer.parseInt(Constants.picklistMap.get(myTeam)));
dref.child("picklist").child(otherTeam.toString()).setValue(Integer.parseInt(Constants.picklistMap.get(otherTeam)));
} else {
Toast.makeText(getActivity(), "Nice try.
If you try it again, the app is going to crash as punishment ... (:",
Toast.LENGTH_LONG).show();
}
}
});
```
By overriding the position, I just mean how
position =+ 1
would make position = position + 1 in python, and I want to do the same thing in Java. The problem is I know that can't be done using the snippet of code I just used to increment the value of position!
Please help!
The reason I can't use position = position + 1 is because on the onItemClick, int position is defined, and then the onClick is created for the buttons, so I need position to be final for me to use it in the onClick, and if I got rid of final, i wouldn't be able to use it in the onClick as it can't be accessed from within an inner class when it's not final
Define which position you want to change, and use this method to update position. You can use this method in onClickListener. This should work.
private int position = 0;
private void up(ArrayList<Integer> arr){
if (position < 1 || position >= arr.size()) return;
int temp = arr.get(position - 1);
arr.set(position - 1, arr.get(position));
arr.set(position, temp);
position --;
}

Highlight selected ListView item

Okay so I've been able to hightlight an item in a ListView, but it ends up highlighting every fourth item. I'm pretty sure that's because of recycling. Then I had the issue where the highlighted item would go back to normal after I scrolled, and that's also because of recycling. Is there any way to keep it highlighted, or to maybe stop the ListView from recycling?
This is what the code looks like right now...
runTimes.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> list, View v, int pos, long id) {
v.setSelected(true);
}
});
This is the code where the highlighted item goes back to normal after you scroll.
to hilight a row you should not touch the view at all. you should use listviews setItemChecked with a selector as the background of your view.
runTimes.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> list, View v, int pos, long id) {
runTimes.setItemChecked(pos,true);
}
});
you also need to make sure you keep track of the last position you selected so that you can deselect it when you select a new one
If you want to stop ListView from recycling you should think again if you really need a ListView.
To properly accomplish this with a ListView, though, you need to save the highlighted item states inside of your adapter. Then in getView highlight the items based on their position.
There have been A LOT of questions about saving the state of the ListView items, I'm sure you can find some.
I ended up finding another question that helped me figure out how to do it. This is what I did. In the OnClickListener I check to see if something has been pressed before. If it hasn't been pressed before, then I set the views background color, and prevRunView to the view. If something has been pressed before, then I change the previous view background color to white. After that I do the same thing as before, but for the different view.
if(runIndex == -1){
runIndex = pos;
v.setBackgroundColor(Color.parseColor("#A6A6A8"));
prevRunView = v;
}else{
prevRunView.setBackgroundColor(Color.parseColor("#FFFFFF"));
runIndex = pos;
v.setBackgroundColor(Color.parseColor("#A6A6A8"));
prevRunView = v;
}
In my adapter I wrote this code so it won't seem like it's recycling.
if(ScoreActivity.runIndex == position)
v.setBackgroundColor(Color.parseColor("#A6A6A8"));
else
v.setBackgroundColor(Color.parseColor("#FFFFFF"));

Error RecyclerView with Picasso Android

I have recyclerview that contains image.
#Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
// Get element from your dataset at this position and replace the contents of the view
// with that element
if(mEntities.get(position).url.equals("kosong")) {
Log.e("LOAD", "KOSONG " + position);
viewHolder.getTextDataView().setText(mEntities.get(position).data);
}
else{
Log.e("LOAD", "ISI " + position);
Picasso.with(mContext).load(mEntities.get(position).url).skipMemoryCache().into(viewHolder.imageView);
viewHolder.getTextDataView().setText(mEntities.get(position).data);
}
}
Im success to make the image loaded to recyclerview in correct list, but somehow it duplicate in another list in that recyclerview. Why ?
Thanks for your answer :)
So from what I understand if your url contains "kosong" you shouldn't display any image right?
However you're not clearing the imageview of it's previous image. Keep in mind that listviews/recycler views recycle their views. So if you display an image on your imageview and later on (scrolling up and down) that imageview is recycled it will contain the last image that was set to it.
Remember to clear the image when you don't want to use it (if(mEntities.get(position).url.equals("kosong"))) using something like:
viewHolder.imageView.setImageResource(android.R.color.transparent);
So it should be something like this:
if(mEntities.get(position).url.equals("kosong")) {
Log.e("LOAD", "KOSONG " + position);
viewHolder.getTextDataView().setText(mEntities.get(position).data);
//we don't need to display an image here however it's possible that our listview contains another image from a recycled row. Let's clear it
viewHolder.imageView.setImageResource(android.R.color.transparent);
}
else{
Log.e("LOAD", "ISI " + position);
Picasso.with(mContext).load(mEntities.get(position).url).skipMemoryCache().into(viewHolder.imageView);
viewHolder.getTextDataView().setText(mEntities.get(position).data);
}

Categories

Resources