आप इस व्यवहार को लागू करने के लिए RecyclerView.ItemDecoration का उपयोग कर सकते हैं।
public class StickyFooterItemDecoration extends RecyclerView.ItemDecoration {
/**
* Top offset to completely hide footer from the screen and therefore avoid noticeable blink during changing position of the footer.
*/
private static final int OFF_SCREEN_OFFSET = 5000;
@Override
public void getItemOffsets(Rect outRect, final View view, final RecyclerView parent, RecyclerView.State state) {
int adapterItemCount = parent.getAdapter().getItemCount();
if (isFooter(parent, view, adapterItemCount)) {
//For the first time, each view doesn't contain any parameters related to its size,
//hence we can't calculate the appropriate offset.
//In this case, set a big top offset and notify adapter to update footer one more time.
//Also, we shouldn't do it if footer became visible after scrolling.
if (view.getHeight() == 0 && state.didStructureChange()) {
hideFooterAndUpdate(outRect, view, parent);
} else {
outRect.set(0, calculateTopOffset(parent, view, adapterItemCount), 0, 0);
}
}
}
private void hideFooterAndUpdate(Rect outRect, final View footerView, final RecyclerView parent) {
outRect.set(0, OFF_SCREEN_OFFSET, 0, 0);
footerView.post(new Runnable() {
@Override
public void run() {
parent.getAdapter().notifyDataSetChanged();
}
});
}
private int calculateTopOffset(RecyclerView parent, View footerView, int itemCount) {
int topOffset = parent.getHeight() - visibleChildsHeightWithFooter(parent, footerView, itemCount);
return topOffset < 0 ? 0 : topOffset;
}
private int visibleChildsHeightWithFooter(RecyclerView parent, View footerView, int itemCount) {
int totalHeight = 0;
//In the case of dynamic content when adding or removing are possible itemCount from the adapter is reliable,
//but when the screen can fit fewer items than in adapter, getChildCount() from RecyclerView should be used.
int onScreenItemCount = Math.min(parent.getChildCount(), itemCount);
for (int i = 0; i < onScreenItemCount - 1; i++) {
totalHeight += parent.getChildAt(i).getHeight();
}
return totalHeight + footerView.getHeight();
}
private boolean isFooter(RecyclerView parent, View view, int itemCount) {
return parent.getChildAdapterPosition(view) == itemCount - 1;
}
}
RecyclerView ऊंचाई के लिए match_parent स्थापित करने के लिए सुनिश्चित करें।
कृपया नमूना आवेदन https://github.com/JohnKuper/recyclerview-sticky-footer पर एक नजर है और यह कैसे काम करता http://sendvid.com/nbpj0806
इस समाधान का एक बड़ा दोष यह है इसे सही ढंग से केवल notifyDataSetChanged() एक आवेदन पत्र भर (अंदर नहीं सजावट) के बाद काम करता है। अधिक विशिष्ट अधिसूचनाओं के साथ यह ठीक से काम नहीं करेगा और उनका समर्थन करने के लिए, इसे एक और तर्क की आवश्यकता है। इसके अलावा, आप लाइब्रेरी रीसाइक्लर्विव-स्टिकहेडर्स से eowise द्वारा अंतर्दृष्टि प्राप्त कर सकते हैं और इस समाधान को बेहतर बना सकते हैं।
आप स्क्रीन के नीचे करने के लिए एक और लेआउट के साथ अपनी अंतिम आइटम डाल सकते हैं और आप के शीर्ष पर अपनी recyclerview डाल सकते हैं यह भी होगा। इसलिए यह हमेशा नीचे रहेगा और आपके रीसायकलव्यू को – nomad
नामांकित नहीं किया जाएगा, क्षमा करें, लेकिन इसकी अपेक्षा नहीं की जाती है: यदि मेरे पास अंतिम रीसाइक्लिंगव्यू आइटम पारदर्शी होगा - तो अंतिम आइटम (मेरे पाद लेख) पर उचित एनीमेशन नहीं होगा (अंतिम -1 आइटम के चिपचिपा होने की बजाय यह नीचे से दिखाई देगा)। मुझे आशा है कि आप मेरे दिमाग को समझ चुके हैं। – resource8218
चेकआउट https://gist.github.com/mheras/0908873267def75dc746 और यह SO उत्तर: http://stackoverflow.com/a/29168617/3140227 –