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
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 want to know the current page position of the ViewPager. I create an adapter and set this adapter to the ViewPager. Now I want to know the current page number or position of the current view, because I want to start some extra events on that particular page.
I used viewPager.setCurrentItem(1); for setting the first item.
Is there a similar method for getting the current page?
in the latest packages you can also use
vp.getCurrentItem()
or
vp is the viewPager ,
pageListener = new PageListener();
vp.setOnPageChangeListener(pageListener);
you have to put a page change listener for your viewPager. There is no method on viewPager to get the current page.
private int currentPage;
private static class PageListener extends SimpleOnPageChangeListener{
public void onPageSelected(int position) {
Log.i(TAG, "page selected " + position);
currentPage = position;
}
}
There is a method object_of_ViewPager.getCurrentItem() which returns the position of currently Viewed page of view pager
If you only want the position, vp.getCurrentItem() will give it to you, no need to apply the onPageChangeListener() for that purpose alone.
You will figure out that setOnPageChangeListener is deprecated, use addOnPageChangeListener, as below:
ViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
if(position == 1){ // if you want the second page, for example
//Your code here
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
For this problem Onpagechange listener is the best one But it will also have one small mistake that is it will not detect the starting time time of 0th position Once you will change the page it will starts to detect the Page selected position...For this problem I fount the easiest solution
1.You have to maintain the selected position value then use it....
2. Case 1: At the starting of the position is always Zero....
Case 2: Suppose if you set the current item means you will set that value into maintain position
3.Then do your action with the use of that maintain in your activity...
Public int maintain=0;
myViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int i, float v, int i2) {
//Toast.makeText(MyActivity.this, i+" Is Selected "+data.size(), Toast.LENGTH_SHORT).show();
}
#Override
public void onPageSelected( int i) {
// here you will get the position of selected page
maintain = i;
}
#Override
public void onPageScrollStateChanged(int i) {
}
});
updateButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MyActivity.this, i+" Is Selected "+data.size(), Toast.LENGTH_SHORT).show();
data.set(maintain, "Replaced "+maintain);
myViewPager.getAdapter().notifyDataSetChanged();
}
});
getCurrentItem(), doesn't actually give the right position for the first and the last page I fixed it adding this code:
public void CalcPostion() {
current = viewPager.getCurrentItem();
if ((last == current) && (current != 1) && (current != 0)) {
current = current + 1;
viewPager.setCurrentItem(current);
}
if ((last == 1) && (current == 1)) {
last = 0;
current = 0;
}
display();
last = current;
}
The setOnPageChangeListener() method is deprecated. Use
addOnPageChangeListener(OnPageChangeListener) instead.
You can use OnPageChangeListener and getting the position inside onPageSelected() method, this is an example:
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
Log.d(TAG, "my position is : " + position);
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
Or just use getCurrentItem() to get the real position:
viewPager.getCurrentItem();
There is no any method getCurrentItem() in viewpager.i already checked the API
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 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);
}