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..
Related
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
I used CommonsWare's ViewPager (With the same code) to make a ViewPager with 6 pages that shows the previous and the next page along the current one.
So here is how it looks: (first image) as you can see when I swipe the pager the first and the 4th page slide out/in from screen. (2nd image)
And here is what I want : ( I just want to fix the 1st and the 4th page's positions so the 2nd page goes on the first page)
(The position of first page is -1 and the 4th is 1 )
I just have problem with using SetTranslationX(float value) for "position >= 1" and "position <= -1"
What should I put in SetTranslationX(float value) to make fixed views ?
So I managed to do it this way :
By looking at JazzyViewPager's code, I used PositionOffsetpixels for setTranslationX() so here is how the code is look like :
pager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
// TODO Auto-generated method stub
OFFSET = positionOffsetPixels;
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
and in pagetransformer I did like this :
if (position <= -1) {
view.setTranslationX(OFFSET);
}else if (position >=1) {
float mTrans = -view.getWidth()-pager.getPageMargin()+OFFSET;
view.setTranslationX(mTrans);
It worked like a charm ...
I have a list that implementing both of PullToRefresh (the old one) and SwipeListView library.
Im following this to combining those library and this to use it in the Activity.
My list can do some basic functionalities like pullUp/pullDown and swipe left/right but i encountered some bug :
The item position in my listview is always started from 1, i believe it should be started from 0, so i need to decrease it in my methods.
When i swipe an item (say, the first item), the item 5th item will be swiped to. So the index+4 item will also be swiped.
The code i used to initialize the objects :
private PullToRefreshSwipeListView ptrListView;
private SwipeListView resultListView;
resultListView = ptrListView.getRefreshableView();
ptrListView.setOnRefreshListener(new OnRefreshListener2<SwipeListView>() {
#Override
public void onPullDownToRefresh(PullToRefreshBase<SwipeListView> refreshView) {
// TODO Auto-generated method stub
}
#Override
public void onPullUpToRefresh(PullToRefreshBase<SwipeListView> refreshView) {
// TODO Auto-generated method stub
}
});
This is the method i used to initialize the listview :
private void setListview() {
adapter = new LibraryAdapter(this, R.layout.item_library_list, new ArrayList<PurchasedItem>(), resultListView);
adapter.setListener(new LibraryListListener() {
//set the adapter
});
resultListView.setSwipeListViewListener(new BaseSwipeListViewListener() {
//position di -1 karena sejak gabung library swipelistview + pulltorefresh, position slalu kelebihan 1 & menyebabkan OutOfBound error.
#Override
public void onClickFrontView(final int position) {
//do something here
}
#Override
public void onOpened(int position, boolean toRight) {
// TODO Auto-generated method stub
super.onOpened(position-1, toRight);
lastPos = position-1;
}
#Override
public void onMove(int position, float x) {
// TODO Auto-generated method stub
super.onMove(position-1, x);
}
#Override
public int onChangeSwipeMode(int position) {
// TODO Auto-generated method stub
return SwipeListView.SWIPE_MODE_DEFAULT;
}
#Override
public void onStartOpen(int position, int action, boolean right) {
// TODO Auto-generated method stub
super.onStartOpen(position-1, action, right);
}
});
resultListView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> adapter, View arg1,
final int pos, long arg3) {
//do something here
}
});
ptrListView.setAdapter(adapter);
ptrListView.setLongClickable(true);
resultListView.setSwipeOpenOnLongPress(false);
}
And this is my xml :
<com.handmark.pulltorefresh.library.PullToRefreshSwipeListView
xmlns:swipe="http://schemas.android.com/apk/res-auto"
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:cacheColorHint="#android:color/transparent"
android:divider="#drawable/line_separator_repeat"
android:listSelector="#00000000"
ptr:ptrMode="pullFromEnd"
swipe:swipeActionLeft="reveal"
swipe:swipeBackView="#+id/back"
swipe:swipeCloseAllItemsWhenMoveList="true"
swipe:swipeFrontView="#+id/front"
swipe:swipeMode="both" />
Please kindly help me, any help is appreciated. Thanks
I had the same first issue like you. i think the first item (index 0) is the header.
int _index = index - listView.getHeaderViewsCount();
I had never met your second problem. You can try this to solve touching problems :
listView.setOnScrollListener(listView.getRefreshableView().getTouchListener().makeScrollListener());
Hope this help
In order to fix problem no. 2, just add those both Override functions to your adapter:
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public int getViewTypeCount() {
return 500;
}