I have a textView inside a recyclerview currently that looks like this:
The full text says: "This is the place where I would put the information about the food item. I want to truncate the line to only show 3 lines and then when I press on the caret, it will expand to the maximum amount of lines."
If I click on the caret (the little down arrow at the top), I want it to expand so that I can see all the lines.
In my recyclerview adapter, I have the following code:
if (holder instanceof InfoViewHolder) {
((InfoViewHolder) holder).more.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!expandedInfo) {
((InfoViewHolder) holder).info.setMaxLines(Integer.MAX_VALUE);
expandedInfo = true;
}
((InfoViewHolder) holder).info.setMaxLines(3);
notifyItemChanged(position);
}
});
}
I'm just manipulating the maxLines in the textView to try and expand the space it will take in the textView so that the view will adjust itself when I notifyItemChanged, however, this does not work and my information textview does not expand.
Can anyone tell me how I can get this to work properly?
Using this library was probably the easiest and the quickest way to solve this puzzle:
https://github.com/Manabu-GT/ExpandableTextView
Although, I would have like to have done it the old fashion way instead of importing a new library so if anyone else has any ideas on how to do this without a library, feel free to post your suggestions below.
The reason it didn't work because ((InfoViewHolder) holder).info.setMaxLines(3); is called no matter what.
So, inside the OnClickListener, it should be:
int maxLines = expandedInfo ? Integer.MAX_VALUE : 3;
((InfoViewHolder) holder).info.setMaxLines(maxLines);
notifyItemChanged(position);
Also, about the ExpandableTextView library, currently it doesn't work in RecyclerView. See my comments on the issue.
Related
Basically, my question is similar to this one:IBM Watson Assistant in Flutter: How to show options?
There is only one answer, telling me to decode the response and show the options as clickable UI elements. I already know how to decode it, but how to make them clickable as a button? Like this:
Example
I am not familiar with RecyclerView enough, maybe some method could do it?
you can add onClickListener to views so if you are using textviews for showing options you can add click listener like this in your RecyclerView Adapter class
textViews.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//YOUR CODE
}
});
everyone.
I'm currently writing a vocabulary app ("Vocabulator) by using a room database and a RecyclerView to display the information.
My problem is that the way I implemented the feature to delete data from the RecyclerView (and the database) does not work on a dataset>10 items.
gif of how the delete function works
On the gif you can see that whenever an item is longClicked, a radio button for each item appears.
Upon clicking the button, the respective item is removed from the RecyclerView (and the db).
As you can see, this works on this very small dataset, but once I add more items the app starts to crash when performing the longClick.
Under the hood, it is implemented using a for loop to reiterate over each item like this:
#Override
public void onLongItemClick(int adapterPosition) {
//wordList.get(position);
// Show radio buttons
for (int i = 0; i < adapter.getItemCount(); i++)
{
vh = recyclerView.findViewHolderForAdapterPosition(i);
v = ((RecyclerAdapter.MyViewHolder) vh).getView();
rb = v.findViewById(R.id.delete_selector);
if (rb.getVisibility() == View.INVISIBLE)
{
rb.setVisibility(View.VISIBLE);
}
else
{
rb.setVisibility(View.INVISIBLE);
}
}
longClicked = true;
}
I can imagine this isn't among the most performant lines of code ever written.
However, that's the solution a beginner like me came up with.
I'd love to implement my delete feature in terms of its functionality exactly as demonstrated in the gif.
However, I can't find a way to make this work.
Can you give me any advice on the issue? :)
Thank you for reading.
I tried to change the ImageButton background (like in Twitter app ) but if I change the ImageButton in first row then in some other row the ImageButton will also be changed.
And also I need to keep the changed ImageButton background in multiple rows as it is.
And I refered this link
RecyclerView causes issue when recycling
But in the above link solution is there for :
To select only one row at a time (As in Navigation drawer ).
And the other solution will cause the problem I mentioned.
Thanks in advance
Here you can find a clear and fast example about recycler view. Say that
you should work on onBindViewHolder overrided function.
Store image information and then apply it every time onBindViewHolder is triggered.
#Override
public void onBindViewHolder(ContactsAdapter.ViewHolder viewHolder, int position) {
Object yourItem = items.get(position);
ImageView yourImageView = viewHolder.yourImageView ;
if(yourItem.containsDifferentImage()){
yourImageViewsetImageResource(R.drawable.my_new_image);
}
else{
yourImageViewsetImageResource(R.drawable.my_standard_image);
}
}
I am writing an android app where I am using a grid view to display some items. I want to give users a configurable view where they can change the no of columns on the activity by clicking floating action button. I am changing the column no using
gridView.setNumColumns(selectedColumnNo);
This is working fine But the problem is if a user changes no of column after some scrolling the First Visible Position is set to the first item of the array list, so the user has to scroll the view again. Can someone please tell me where I am doing wrong. Or Is this the proper way to do this or should I use a different approach.
A code snippets will be helpful
Thanks.
Update::
currently I am using the bellow snippets
findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int firstPosition = gv.getFirstVisiblePosition();
if(gv.getNumColumns()==2)
{
gv.setNumColumns(1);
gv.setSelection(firstPosition);
}
else {
gv.setNumColumns(2);
gv.setSelection(firstPosition);
}
}
});
Now the problem is on every 4th switch grid view is showing the first element of the arraylist
Right before you call setNumColumns(), save the GridView's first visible position:
int firstPosition = gridView.getFirstVisiblePosition();
Then, after you change the number of columns, pass that integer to setSelection():
gridView.setSelection(firstPosition);
"Selection", counter-intuitively, is not the same thing as "activation". It will ensure that the view is on-screen, but not visibly affect it in any other way.
I'm trying to implement this using Google's Espresso, however I'm not finding any ViewAssertion or ViewAction that'd allow me to do this.
I'm not sure if these can be done using bundled in matchers or should I write my own.
Thanks!
Even though #denys answer seemed to be correct at first glance, it looks like it works only on certain cases (as suggested by Espresso author). The proper way to scroll to an specific item is as replied here on android-test-kit-discuss forum, and reproduced below:
onData(instanceOf(Item.class))
.inAdapterView(allOf(withId(android.R.id.list), isDisplayed()))
.atPosition(9)
.check(matches(isDisplayed()));
It could look something like this:
1. Find the number of elements in listView and save it in some variable:
final int[] counts = new int[1];
onView(withId(R.id.some_list_view)).check(matches(new TypeSafeMatcher<View>() {
#Override
public boolean matchesSafely(View view) {
ListView listView = (ListView) view;
counts[0] = listView.getCount();
return true;
}
#Override
public void describeTo(Description description) {
}
}));
2. Then, knowing the number of elements in listView you can scroll to any element within the range.
onData(anything()).inAdapterView(R.id.some_list_view).getPosition(<item_index>).perform(scrollTo())
If you already have this specific number then you have do something like this:
onData(is(instanceOf(yourClass.class)))
.inAdapterView(withId(R.id.some_list_view))
.atPosition(spicificNumber)
.perform(scrollTo());
This will fail if ListView doesn't have an item at position with specificNumber.
EDITED:
Take a look also at this example - Espresso samples. It will help you to check if there are no more items in the list after spesificNumber.