ListView with date as section headers position varies onScroll - android

Im trying to load custom callLogs in a listView based on date as section header.In ListAdapter i compare each date with the previous date and set SectionHeaderLayout Visible/Invisible. When the ListView has been loaded the section header are displayed correctly but when i scroll the section headers are set Visible to wrong ListItems.
Please help me to figure out a solution.
This is how im trying to set SectionHeader through the adapter.
if (position == 0) {
checkDate = mDateStr;
holder.sectionHeaderDate.setVisibility(View.VISIBLE);
holder.sectionHeaderText.setText(mDateStr);
}
} else if (checkDate == null || !checkDate.equals(mDateStr)) {
checkDate = mDateStr;
holder.sectionHeaderDate.setVisibility(View.VISIBLE);
holder.sectionHeaderText.setText(mDateStr);
} else {
holder.sectionHeaderDate.setVisibility(View.GONE);
}
Thanks in Advance

I see this is old question, you have probably solved your problem, but I'll answer for others who will have the same problem.
If you want to show header based on previous date you can't do that by remembering last item that was passed to getView function.
The reason is scrolling, i.e. different direction when going up and down.
For example, if you have items
1,
2,
3,
4,
5
when you're going down, and current item is 3, previous was 2, and all will work.
But if you are going up, your previous item for 3 was actually 4, and that's where your problem happens.
so instead of keeping item, you should use positions.
this would be the sketch of solution that you can call inside of your getView function:
private void showHeader(ViewHolder holder, Call item, int position) {
boolean shouldShowHeader = false;
if (position == 0
|| !DateHelper.isSameDay(item.getDateTime(),
items.get(position - 1).getDateTime()))
shouldShowHeader = true;
if (shouldShowHeader) {
holder.header.setVisibility(View.VISIBLE);
holder.date.setText(DateHelper.getSimpleDate(item.getDateTime()));
} else {
holder.header.setVisibility(View.GONE);
}
}

Related

On scroll recyclerview item Background color changed when recylcerview scroll

I want to change Recyclerview item background color on my condition but when I scroll background color automatically changed I know holder.setIsRecyclable(false); but I don't want to set holder.setIsRecyclable(false);
and I know
if(item.value == 1){
// do something
} else{
// do something
}
but i want to do using nested if else
if (feetInt > 0 && feetInt < 4) {
((PatientViewHolder) holder).rlClientItemMain.setBackgroundColor(ContextCompat.getColor(mContext, R.color.player_list_green));
} else if (feetInt >= 4 && feetInt < 6) {
((PatientViewHolder) holder).rlClientItemMain.setBackgroundColor(ContextCompat.getColor(mContext, R.color.player_list_yellow));
} else if (feetInt == 0 || feetInt >= 6) {
((PatientViewHolder) holder).rlClientItemMain.setBackgroundColor(ContextCompat.getColor(mContext, R.color.player_list_red));
}else{
((PatientViewHolder) holder).rlClientItemMain.setBackgroundColor(ContextCompat.getColor(mContext, R.color.player_list_grey));
}
Instead of changing color on feetInt maintain a flag in your object class and on the basis of particular flag change your background color
It looks like you are not updating the value of feetInt for every single object in list. That's why you are facing this issue.
Update feet int value in list for different different position, you will get the desire result.
Also, remove getItemViewCount() and setIsRecyclable(). These are not required at all. Also, share your code for more specific answers.
holder.setIsRecyclable(false);

How to create section header listing like whatsapp?

I'm developing chat application and i've already design dummy chat history.But i'm stuck at how to group messages according to it's date and when we scroll it down,date indicator stick at the top position just like whats app. can you just show me the way, how can i achieve that? I've attached some screenshot below to elaborate my question.
Put your header in your custom lisview adapter layout and check everytime your current message date and previous message date. If date is same then hide your header otherwise show your header. See below:
holder.tvDate.setText(chatMessage.getDate());
if (position > 0) {
if (chatMessages.get(position).getDate().equalsIgnoreCase(chatMessages.get(position - 1).getDate())) {
holder.header.setVisibility(View.GONE);
} else {
holder.header.setVisibility(View.VISIBLE);
}
} else {
holder.header.setVisibility(View.VISIBLE);
}
Simple. Just add a header view to your ListView
TextView textView = new TextView(context);
textView.setText("Hello. I'm a header view");
listView.addHeaderView(textView);
for more details- https://developer.android.com/reference/android/widget/ListView.html#addHeaderView(android.view.View)
Update:
By far the simplest way to do this is to embed the date header view in every item. Then, all you need to do in bindView is compare the previous row's date to this row's date, and hide the date if it's the same. Something like this:
String thisDate = cursor.getString(dateIndex);
String prevDate = null;
// get previous item's date, for comparison
if (cursor.getPosition() > 0 && cursor.moveToPrevious()) {
prevDate = cursor.getString(dateIndex);
cursor.moveToNext();
}
// enable section heading if it's the first one, or
// different from the previous one
if (prevDate == null || !prevDate.equals(thisDate)) {
dateSectionHeaderView.setVisibility(View.VISIBLE);
} else {
dateSectionHeaderView.setVisibility(View.GONE);
}

RecyclerView keeps repeating cached (?) subview and not looking to onBindViewHolder

I have RecyclerView where every list item has an ImageButton, thee image of which I define in the adapter's onBindViewHolder():
int myVote = getMyVote();
if (myVote != 0) {
Log.d("dbg", myVote + "");
holder.ratingButton.setImageResource(R.drawable.ic_star_grey600_36dp);
}
So ratingButton is a star in the right bottom corner of the list item layout. Its shape is filled with gray color (and accordingly, a log record is pushed) if the condition (myVote != 0) is satisfied.
The problem is that when I scroll the list down I can watch other stars became filled even though I can see the only one record in the log window (for the correct list item). Moreover, this list items with incorrectly changed buttons repeat every 5 rows, and that's what's confusing me. If I changemListView.setItemViewCacheSize(0); the repeat period changes to 3, so we can assume it somehow connected with the RecyclerView's caching and recycling mechanism.
Please, help me to work the problem out. Thanks!
Don't forget to implement
public long getItemId(int position) {}
otherwise, you'll see repeated items in RecyclerView.
Try to change your code to:
if (myVote != 0) {
Log.d("dbg", myVote + "");
holder.ratingButton.setImageResource(R.drawable.ic_star_grey600_36dp);
} else {
holder.ratingButton.setImageResource(int another resource);
}
}
You may also have to write else part of main condition with some another resource like:
if (myVote != 0) {
Log.d("dbg", myVote + "");
holder.ratingButton.setImageResource(R.drawable.ic_star_grey600_36dp);
} else {
holder.ratingButton.setImageResource(int another_resource);
}
It is worked for me.

scroll to list item in AlertDialog without selection

I have a huge list of items that I present in an AlertDialog. I would like to present the user the list scrolled to the most likely area they will select one item from. I'm using
AlertDialog.Builder.setSingleChoiceItems(myAdapter, ...).
ArrayAdapter<MyType> myAdapter;
The problem I'm having a hard time with is how to scroll to an item when it's not logically correct to present the item as selected.
I tried getting the ListView from the resulting AlertDialog. But it's empty (even after the Builder creates and shows it).
I tried forcing a populated ListView by inflating a plane ListView in res/layout. listView.scrollTo(x, y) didn't seem to have an effect.
I tried up setting the OnShowListener for the AlertDialog. onShow() is never invoked.
Does anyone know of a work around?
You could use the functions which are part of the ListView class:
smoothScrollByOffset(int offset);
or
smoothScrollToPosition(int position);
Or
if you want to scroll one by one you could use functions like:
private void scrollToNext() {
int currentPosition = getListView().getFirstVisiblePosition();
if (currentPosition == getListView().getCount() - 1)
return;
getListView().setSelection(currentPosition + 1);
getListView().clearFocus();
}
private void scrollToPrevious() {
int currentPosition = getListView().getFirstVisiblePosition();
if (currentPosition == 0)
return;
getListView().setSelection(currentPosition - 1);
getListView().clearFocus();
}

Android: Help required in card matching game?

I have 20 cards of which there are 10 pair of images. The player is meant to find a match of each card/image.
The problem is that if a player taps or clicks each card twice then that card gets decremented from the remaining ones . I need to disable the click listener of the ImageView. How can do that?
ivOne = (ImageView)findViewById(R.id.ivOne);
ivOne.setId(a[0]);
//final ImageView ivOne = (ImageView)findViewById(R.id.ivOne);
//ivOne.setEnabled(false);
ivOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ivOne.setBackgroundResource(images[a[0]]);
Log.e("r[0]:", Integer.toString(a[0]));
if (firstTap)
{
firstId = v.getId();
firstTap = false;
}
else
{
//ivOne.setEnabled(false);
int secondId = v.getId();
Log.e("secondId", Integer.toString(secondId));
if ((secondId == firstId) && (score != 0))
{
//ivOne.setEnabled(false);
if (ivOneScored == false)
{
score--;
ivOneScored = true;
}
}
firstTap = true;
}
tvScore.setText("Remaining:" + Integer.toString(score));
}
});
I have a memory matching game on the market that works very similar. What I did was make a class that represents the card (with image and id properties) and assign an id (0-9) to the cards and the image as I loaded them into a temporary arraylist of just the 10 image objects.
public class GameTile {
public int id;
public Drawable image;
}
Then I iterated over the list twice and added the cards to the main arraylist that I use for my adapter... after which I used Collections.shuffle to shuffle the cards... Now... on to the selection. AS you know, in a memory game, you only want 2 cards flipped at any time. I am using a gridview to hold my cards... so what I did was use the OnItemClickListener from the gridview and not the imageview. Secondly, when a card is clicked, I add the position to another arraylist called "selected" that never contains more than 2 items... the position of the items we are attempting to match... In the OnItemClickListener, when the event is fired, I check to see if the item already exists in "selected" and return if it does... in effect ignoring the click.
if (selected.contains(position)) {
return;
}
When "selected" contains 2 items, I ignore all clicks until the handler finishes checking for a match.
if (selected.size() > 1) {
return;
}
So when a user has selected 2 items, I set a handler to call a runnable that checks for a match. If a match is made (by comparing the id fields I set when I first loaded the images), I add the two positions to another arraylist that contains only matched items and that handler also clears "selected" and, if there were matches, I change the images to blanks. When all 20 cards have been matched I fire a win which does all of the win stuff and resets the gameboard.
You have to make sure that those cards have the same symbol, but are not the same card.
So your line
if ((secondId == firstId) && (score != 0))
must rather look like something in the lines of
if ((firstCard != secondCard) && (secondId == firstId) && (score != 0))
where firstCard and secondCard identify the card on the table (not the symbol on the card)
If I understand you right, you just need to say view.setOnClickListener(null) if those two match? If you want to disable both of them, you just store a map to link an ID to a View, and then clear the click listener for both "view" and whichever view belongs to the second ID.

Categories

Resources