hello I'm sorry for the ambiguous nature of my question but this is the easiest way I can explain the problem. Okay so I have an adapter which pulls parsed json data from mysql database and places each json object in a cardview and I have a recyclerview where these cardviews are displayed. Each cardview has a button with an onClick method to change color of button when it is clicked. This works fine except that the action performed on the first cardviews button is also performed on the seventh cardview, action of second cardview performed on the eighth cardview and that's how it continues. In a nutshell when button1 in cardview one is clicked, color of button1 in cardview 1,7,13,19,etc changes. That is looping through pairs of 6. Any idea how to resolve this please.
I solved it by implementing
#Override
public int getItemViewType(int position){
return position;
}
Related
I want to show 2 items by default and add items in recycler view when we click button in main activity. Those item contains edittext so I have to get the data from edittext in my main activity.
Basically you have to create two different ViewType/Layouts to implement this.
Sharing related links below..
https://www.geeksforgeeks.org/how-to-create-recyclerview-with-multiple-viewtype-in-android/
How to create RecyclerView with multiple view types
For adding items to Recycler view when you click in a button you should add the item in the list and notify adapter that new item added at the end of the recyclerview using notifyItemInserted()
and for the other question for getting the edittext value from the adapter to the activity you can create a callback interface that implemented in the activity and pass the reference to the adapter and fire it from adapter when Edittext value is changed
I have a Listview with EditTexts in every cell. Whenever I add text to the point that the EditText grows 1 line, the ListView scrolls to the top (and usually removes the EditText from view by doing so).
I found a few questions about this but they only seem to have "hacks" as answers and nothing that works for me.
ListView scrolls to top when typing in multi-line EditText
Listview with edittext - auto scroll on "next"
EDIT:
After a lot of research, I have found that it is because the editText is resizing, causing the ListView layout its subviews and then resetting to the top. I want to find a way for the EditText to grow while staying at the same place.
I tried this because of a suggestion I found online but, even though it keeps the ListView at the right height, it clears the focus from the EditText and I cannot seem to get it back with requestFocus.
private Stirng getTextWatcher(){
return new TextWatcher(){
#Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
#Override public void onTextChanged(CharSequence s, int start, int before, int count) { }
#Override public void afterTextChanged(Editable s) {
listView.setSelection(index);
}
}
}
So what I would need is either:
Have a way for the ListView to not scroll automatically to the top when the subviews are being layed out again or
A way to focus my EditText after having called ListView.setSelected() or 3. Have another way to scroll the ListView that will not clear focus from the EditText.
For option
I have tried scrollTo() and smoothScrollToPosition(). scrollTo for some reason works but hides certain items from the ListView (they become invisible, not loaded). And smoothScrollToPosition() creates an animation where the ListView jumps to the top and then it brings the user back to the Item (and does it 2 or 3 times).
As par my view if you want to take input from ListView item by using EditText then you should use TextView in place EdiText and by click of TextView you should open and Input Dialog with EditText that is more simple and good solution. handling of EditText focus in ListView is vary difficult and you need to put extra Listeners for this.
OR
And if you have limited number of input box in your UI/UX then you can implement EditText with CustomListView by using ScrollViews.
ListView Scrolls to Top when EditText Grows
ListView has some known limitations related to child focus.
Solution
Why don't you try to use RecyclerView instead of ListView? Google introduced RecyclerView as the default recommended way of such UI. So custom ListView or hacks ore no more needed.
You can try adding this attribute to your ListView:
android:transcriptMode="normal"
This makes list automatically scroll to the bottom when a data set change notification is received and only if the last item is already visible on screen.
Read more here: attr_android:transcriptMode | Android Developers
In my list view I have an textview in expandable group and I want to open the dialog when textview is clicked to fill the information through edittext and update textview.
Problem: how could I get the groupview textview item in my fragment oncreateview() method.
You shouldn't pass your view item form a fragment to an other. You should retrieve the object associated with your group view, pass this object to your second/edition fragment. You can use
setTargetFragment(...) and onActivityResult(...) to send the modified text from your second to your first fragment. And then you can update you list of object and refresh your expandableListView to display the updated text.
If you need help doing this please post your code.
Below is a picture of my app, I am currently using a modified version of this app to show my expandable content. Now my problem is with my ImageView, I currently have 3 layouts, one for the main dialog (with listview), one for the listview title & arrow and one for the item details (item row once expanded). I want to add animation to the ImageView so when you click the image or title textview, the list will expand and animate the change. How can I do this? I cannot get the onClick to work because it doesn't know there are 2 imageviews, it seens only one...
I will assume that your ImageView is this arrow inside your ListView element (because you didn't write it clearly enough).
You probably shouldnt try to attach onClickListener to your ImageView at all (nor to your ListView element title). The good way of implementing what you want you achieve would be to use ListView's onItemClickListener to detect clicks performed on specific items inside your listview, and then expanding appropriate expandable content.
Anyone know how to get this result?
Description:
When the '+' button is clicked, another line of EditText, Spinner, EditText and Button will be displayed below the line. And lines go on adding if the user click on the '+' button.
Thanks!
I think the best would be to implement a custom ViewGroup extending a LinearLayout in vertical orientation.
You'll register an OnClickListener on the last button: if the row is the last in the list, you'll inflate a new ingredients_item.xml and add it to your view (also you'll refresh the drawable from + to -), otherwise remove the current row.
Your custom view will provide a method like List<Ingredient> getIngredients(), so you'll be able to pass this list to some save() method.
I pushed the demo on GitHub