I am using Staggeredgridview in my Project.
I've downloaded StaggeredGridView library and demo from here: https://github.com/maurycyw/StaggeredGridViewDemo https://github.com/maurycyw/StaggeredGridView
I need to PullToRefresh for that entire gridview
Thanks all
Just Added Support for StaggeredGridView in Chris Banes
Android-PullToRefresh.
Also support Actionbar-PullToRefresh for ActionBarSherlock.
Enclose the gridview in SwipeRefreshLyout.
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.widget.SwipeRefreshLayout>
Add onScroll listener for the gridview in onViewCreated. Set conditions when the refresh is enabled.
swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swipeLayout);
swipeRefreshLayout.setColorSchemeResources(R.color.theme_color);
swipeRefreshLayout.setOnRefreshListener(this);
gridView.setOnScrollListener(new AbsListView.OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView absListView, int i) {
}
#Override
public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
int dist = gridView.getDistanceToTop();
if (dist == 0)
swipeRefreshLayout.setEnabled(true);
else {
swipeRefreshLayout.setEnabled(false);
}
}
});
Then add a onRefresh function.
#Override
public void onRefresh() {
if (!swipeRefreshLayout.isRefreshing()) {
swipeRefreshLayout.setRefreshing(true);
}
//add functionality then set refresh to false
swipeRefreshLayout.setRefreshing(false);
}
Related
I have an activity which have a toolbar and a FrameLayout. FrameLayout is placeholder for fragments. In one of my fragments, there is my main screen ListView.
Currently, when user scrolls down in this ListView, I hide Toolbar. When user scrolls up, I show the Toolbar again.
The code looks like this so far:
listView.setOnScrollListener(new OnScrollObserver(){
#Override
public void onScrollUp() {
toolbar.setVisibility(View.GONE);
}
#Override
public void onScrollDown() {
toolbar.setVisibility(View.VISIBLE);
}
});
And this is OnScrollObserver:
public abstract class OnScrollObserver implements AbsListView.OnScrollListener {
public abstract void onScrollUp();
public abstract void onScrollDown();
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
int last = 0;
boolean control = true;
#Override
public void onScroll(AbsListView view, int current, int visibles, int total) {
if (current < last && !control) {
onScrollUp();
control = true;
} else if (current > last && control) {
onScrollDown();
control = false;
}
last = current;
}
}
It works so far but there is a problem. When Toolbar is hidden, ListView takes up the whole screen which is intended behavior. But the problem is when this happens, ListView is reloaded due to change in size (I assume). Everything is redrawn and recalculated which slows the performance.
Is there any alternatives to this? How to prevent reload each time Toolbar is hidden or shown?
You have to use below code instead of yours :
listView.setOnScrollListener(new OnScrollObserver(){
#Override
public void onScrollUp() {
toolbar.setVisibility(View.INVISIBLE);
}
#Override
public void onScrollDown() {
toolbar.setVisibility(View.VISIBLE);
}
});
I've been using Realm on android for a couple of days now, and I'm loving it. However, of late I've noticed an issue when using a realm adapter to load data into a list view. They is a delay whenever I'm launching the activity with the listview or resuming. The delay is noticeable. It's actually disturbing the whole ux interaction. I've tried using asynchronous methods but the delay is still there. Btw, I'm only loading 15 items into the listview!
Heres my current code.
realm = Realm.getDefaultInstance();
adapter = new MemosListAdapter(this, null);
listView.setAdapter(adapter);
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == SCROLL_STATE_IDLE) {
newMemoButton.setVisibility(View.VISIBLE);
} else {
newMemoButton.setVisibility(View.INVISIBLE);
}
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
});
memos = realm.where(Memo.class).equalTo("latest", true).findAllSortedAsync("date");
memos.addChangeListener(new RealmChangeListener<RealmResults<Memo>>() {
#Override
public void onChange(RealmResults<Memo> element) {
adapter.updateData(element);
empty.setVisibility((adapter.isEmpty()) ? View.VISIBLE : View.GONE);
}
});
I cant seem to get what the problem is. Thanks in advance.
after loading more data the grid view back to top, I want to make it keep scrolling from the last item after loading.
I tried to use onScrollStateChanged, and make it loading in state == SCROLL_STATE_TOUCH_SCROLL, but I faced the same problem.
#Override
protected void onPostExecute(ArrayList<productDetails> AProducts) {
final ArrayList<productDetails> products = AProducts;
super.onPostExecute(products);
productAdapter = new productAdapter(category.this, productDetailsArr);
gridView.setAdapter(productAdapter);
productAdapter.notifyDataSetChanged();
if (products != null) {
gridView.setOnScrollListener(new AbsListView.OnScrollListener() {
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (firstVisibleItem + visibleItemCount >= totalItemCount) {
// End has been reached
limit = Integer.parseInt(((productDetails) (products.get(products.size() - 1))).getProductID());
// limit = Integer.parseInt(products.get(3).get(products.get(3).size() - 1));
if (flimit != limit) {
flimit = limit; //to stop using last element in fatching last products more than one time.
if (UtilityClass.isInternetAvailable(getApplicationContext())) {
new getProductsTask().execute(category);
} else {
Intent internet = new Intent(getBaseContext(), NointernetConnection.class);
startActivity(internet);
finish();
}
} else {
Log.d("FLimit", ">>" + "END");
}
} else {
progressDialog.dismiss();
}
}
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
}
});
}
}
First, you don't need to create a new object of your adapter class (productAdapter) every time in onPostExecute method and set adapter to gridview every time when data change or new response of your network call .This causes of scrolling gridview to top position.Instead you can create a setter method in your adapter class.
ArrayList <productDetails> productDetailsArr;
public void setProductDetailsArr(ArrayList<productDetails> productDetailsArr) {
this.productDetailsArr = productDetailsArr;
}
and in onPostExecute method write down the following code to check if adaper is null or not.If null then only you have to create a new instance.Otherwise you only have to provide a new dataset and just call notifyDataSetChanged().
if(productAdapter == null)
{
productAdapter = new productAdapter(category.this, productDetailsArr);
gridView.setAdapter(productAdapter);
}else{
productAdapter.setProductDetailsArr(productDetailsArr);
productAdapter.notifyDataSetChanged();
}
I'd like to create ListView that can be expandable.
So, i found some method, and set listener in listview.
like this.
listView.setOnScrollListener(new OnListViewScrollListener() {
#Override
protected void onFlinging() {
Log.d(TAG, "onFlinging");
}
#Override
protected void onScrollFinished() {
Log.d(TAG, "onScrollFinished");
}
#Override
protected void onScrolling() {
Log.d(TAG, "onScrolling");
}
});
and here is OnListViewScrollListener.java
public abstract class OnListViewScrollListener implements AbsListView.OnScrollListener {
private final String TAG = OnListViewScrollListener.class.getSimpleName();
int mCurrentVisibleItemCount;
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if(isValidItemCount()){
switch (scrollState){
case SCROLL_STATE_FLING :
onFlinging();
break;
case SCROLL_STATE_IDLE :
onScrollFinished();
break;
case SCROLL_STATE_TOUCH_SCROLL :
onScrolling();
break;
};
}
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
this.mCurrentVisibleItemCount = visibleItemCount;
Log.d(TAG, "firstVisibleItem, visibleItemCount, totalItemCount # ["+firstVisibleItem+", "+visibleItemCount+", "+totalItemCount+"]");
}
private boolean isValidItemCount() {
return mCurrentVisibleItemCount > 0;
}
protected abstract void onFlinging();
protected abstract void onScrollFinished();
protected abstract void onScrolling();
}
But i don't know how to expand listview that is include many rows which is photos.
like gallary.
Please let me know that how to expand listview.
Thanks.
Here is an example of how to resize a view with an Animation. You can use this for both the expanding and collapsing of the ListView.
If you want the ListView to display more detail when expanded (bigger photos, etc), you'll need to call notifyDataSetChanged and return different view in the adapter, once the animation is completed.
You are talking about something like bottom sheet
https://www.google.com/design/spec/components/bottom-sheets.html?utm_campaign=android_launch_supportlibrary23.2_022216&utm_source=anddev&utm_medium=blog#bottom-sheets-persistent-bottom-sheets
This one is already provieded in the support library 23.2
In my application i have used an "Listview". In that what i want is I have to get the count of the visible child.For example if i load that listview in my device first shoes 3 rows ,so i have to show in a textview that "Showing 3 of 10",and when user scroll the listview i have to update that textview according to scroll..I tried a lot but i didn't acheived this..
I tried for this also:
int count=0;
for(int i = 0; i <= listView.getLastVisiblePosition(); i++)
{
if(listView.getChildAt(i)!= null)
{
count++; // saying that view that counts is the one that is not null, because sometimes you have partially visible items....
}
}
Always returning 0. Please help me to solve this.
Y i got the answer....
I implemented onscroll listener..
library_listview.setOnScrollListener(new OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
firstVisibleItem = firstVisibleItem+1;
String vischild="Showing "+firstVisibleItem+" to "+visibleItemCount+" of "+totalItemCount+"";
visiblechild.setText(vischild);
}
});
Thanks a lot..