Fragment inside ViewPager disappears on swiping - android-listview

The Context
I have a HomeActivity that contains a NewsFragment. The NewsFragment contains a ListView to display news items in a list. I have a ViewPager that is added as a header-view to the ListView. This ViewPager appears as a header to the list and contains a DateFragment to display the date. The first time the user opens the HomeActivity, I want "TODAY" to be the date. When the user swipes the DateFragment to the left, I want the date text to show "TOMORROW". Swipe again and it should show the day after tomorrow. Similarly, when the user swipes in the other direction, I want to display "YESTERDAY", the day before yesterday etc. The ViewPager should support swiping infinitely (or a very large number) in both directions.
Here's how they are nested - http://tinypic.com/r/31623qw/8. (I don't have the reputation to post images yet, but this diagram is super useful for understanding this question).
The Problem
For simplicity, I'm displaying the index of the fragment as the date. When I first open the app, I see "Date: 0" being displayed as the date in the DateFragment. When I swipe the fragment to the left (to go to the next day), I can see the next fragment appear with "Date: 1" being displayed, but almost immediately, the fragment disappears. Once the fragment disappears, nothing reappears, and I just see an empty container. I can swipe to bring back the "Date: 0" Fragment, but I can't see any other Fragments. (I'm using the - Infinite View Pager by Antony T. - https://github.com/antonyt/InfiniteViewPager)
The Gory Details
I have a HomeActivity that contains a NewsFragment. The NewsFragment contains a ListView to display news items as a list. Here is the layout.xml for the NewsFragment:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
I have a ViewPager that is dynamically added as a header to this list. So inside NewsFragment, I first initialize the ListView:
#Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.home_news_fragment, container, false);
list = (ListView) view.findViewById(android.R.id.list);
emptyView = view.findViewById(android.R.id.empty);
list.setEmptyView(emptyView);
return view;
}
Next, in onActivityCreated(), I initialize and load the header for the ListView, and then load the news feed items:
#Override
public void onActivityCreated(final Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
loadHeader();
loadNewsFeed();
}
In the loadHeader() method, I then dynamically create a LinearLayout container to contain my ViewPager. I create a PagerAdapter for the ViewPager and I wrap it with an InfiniteViewPager to allow infinite swiping in both directions. I then create a ViewPager and I add it to the LinearLayout container. I then add the container as a Header View to the ListView:
private void loadHeader()
{
final LinearLayout headerContainer = new LinearLayout(getActivity());
headerContainer.setId(R.id.header_container_id);
headerContainer.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
headerContainer.setOrientation(LinearLayout.VERTICAL);
// initialize the pager adapter for the view pager
PagerAdapter pagerAdapter = new HeaderPagerAdapter(getActivity().getSupportFragmentManager());
// Create a view pager. I use a custom implementation of a view pager I found from another Stackoverflow answer - WrapContentHeightViewPager - that allows ViewPager's layout.height to wrap content. The original ViewPager does not allow wrapping of content.
WrapContentHeightViewPager mViewPager = new WrapContentHeightViewPager(getActivity());
mViewPager.setId(R.id.view_pager_header);
mViewPager.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT));
// wrap pager to provide infinite paging with wrap-around
PagerAdapter wrappedAdapter = new InfinitePagerAdapter(pagerAdapter);
mViewPager.setAdapter(wrappedAdapter);
// Add the view pager to the header container
headerContainer.addView(mViewPager);
// add the container as a header view to the list
list.addHeaderView(headerContainer);
}
In the loadNewsFeed() method, I do an async fetch of the news feed items and populate the list by adding the items into the footer view. I won't go into the implementation details for this - outside the scope of this question.
The NewsFragment contains my pager adapter, which contains the getItem method that initializes the Fragment to be displayed by the ViewPager:
private class HeaderPagerAdapter extends FragmentPagerAdapter
{
public HeaderPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
return DateFragment.newInstance(i);
}
#Override
public int getCount() {
return Integer.MAX_VALUE;
}
}
And here is the actual Fragment to be displayed inside the ViewPager:
public class DateFragment extends Fragment
{
private int index;
TextView date;
public static final DateFragment newInstance(int index) {
DateFragment fragment = new DateFragment();
final Bundle args = new Bundle(1);
args.putInt(Constants.Extras.PAGE_INDEX, index);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
index = getArguments() != null ? getArguments().getInt(Constants.Extras.PAGE_INDEX) : 1;
}
#Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState)
{
View v = (ViewGroup) inflater.inflate(R.layout.summary_fragment, container, false);
date = (TextView) v.findViewById(R.id.tvDate);
return v;
}
#Override
public void onActivityCreated(final Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
date.setText("Date: " + index);
}
}
For now I am simply using the index to be displayed as the date. I want to replace this with the actual date. Why does the fragment disappear on swipe?
Considerations:
Is there a refresh/redraw event that gets called?
Does it have to do with extending FragmentPagerAdapter vs. FragmentStatePagerAdapter
Override destroyItem() or instantiateItem()?
Set up a onPageScrolled listener on the ViewPager and then handle the page scroll events? I tried thehayro.blogspot.com/2012/12/enable-infinite-paging-with-android.html and that causes my fragments to disappear and then appear after a while.
Is it because I am adding the ViewPager as a header to the list view? The list view scrolls up and down and the view pager left and right. I've added this to my ViewPager implementation to tell the list view to stop intercepting our touch events:
#Override
public boolean onInterceptTouchEvent(MotionEvent ev)
{
// Tell our parent to stop intercepting our events!
boolean ret = super.onInterceptTouchEvent(ev);
if (ret)
{
getParent().requestDisallowInterceptTouchEvent(true);
}
return ret;
}
I'm sorry about the length of this post. I felt this much detail might be necessary. Thoughts?

Related

Textview resets to old value when scrolling up

I am creating a list view. I am populating the listview using custom adapter. In my listview there is a text view widget. When I reset the value of that text view it displays fine. Then, I scroll down to get new values into the listView. But when I scroll up the text view widget resets back to the old populated value.
How can I retain the new value set by me persistently?
Here is the code of my listview adapter class where I populate the values to my listview. I am clicking on imageview in the listview that fires the event to set new text value to the text view widget in the list view.
private class MyListAdapter extends ArrayAdapter<CommentInfo> {
public MyListAdapter()
{
super(Comment.this, R.layout.listview_xml, myComments);
}
#Override
public View getView(final int position, final View convertView, final ViewGroup parent)
{
itemView = convertView;
if(itemView == null)
{
itemView = getLayoutInflater().inflate(R.layout.listview_xml, parent, false);
}
CommentInfo currentComment = myComments.get(position);
TextView tvLikes = (TextView) itemView.findViewById(R.id.tvLikes);
tvLikes.setText(currentComment.likes);
ImageView ivLikes = (ImageView) itemView.findViewById(R.id.likeBtn);
ivLikes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
likePosition = position;
TextView tvlikes1 = (TextView) convertView.findViewById(R.id.tvLikes);
// Here, i will get the correct value from DB and set it.
// I am setting 999 for sample purpose
tvlikes1.setText("999");
}
});
return itemView;
}
}
I reset the value of listview's text view by getting convertView value in getView() argument in my above adapter class and accessing my listview's textview widget through it. Then, i set it to value 999 (for example purpose). Now, I scroll down to get more new listView items. But, on scrolling up the new value (999) that i set to the old item disappears.
Please help me with it.
I have done a test myself. The ListView adapter will always take the values from the myComments list, so in order to update the TextView as you want, you have to do like this:
ivLikes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
likePosition = position;
myComments.get(position).setLikes("999");//Assume that your CommentInfo's likes is a string.
notifyDataSetChanged();
}
});

how to smooth scroll listview like ios in android?

I am use data from web using XML parser and setting data using custom adapter for this use asyncTask .
My problem is that some devices like Samsang duos,gallaxy work perfectly but on micromax devices it will not work properly.
My adapter is
public class HallBookingAdapter extends ArrayAdapter<MyHall> {
private Context context;
private ArrayList<MCCIAHall> halls;
private int resource;
MyHall objHall;
public int count;
View view;
public static Boolean isScrollingHalls=true;
LayoutInflater inflater;
static class HallBookingHolder
{
public TextView txtTitle,txtLocation,txtCapacity,txtCapacityTitle;
public ImageView imgHall;
public LinearLayout hallBookingLayout;
}
public HallBookingAdapter(Context context, int resource, ArrayList<MyHall> halls) {
super(context, resource, halls);
this.context=context;
this.halls=halls;
this.resource=resource;
inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
count=halls.size();
return halls.size();
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
view=convertView;
objHall=halls.get(position);
HallBookingHolder holder=new HallBookingHolder();
if (convertView==null) {
view = inflater.inflate(resource, null);
holder.txtTitle=(TextView)view.findViewById(R.id.txtListHallTitle);
holder.txtLocation=(TextView)view.findViewById(R.id.txtListHallLocation);
holder.txtCapacity=(TextView)view.findViewById(R.id.txtListHallCapacity);
holder.txtCapacityTitle=(TextView)view.findViewById(R.id.txtListHallCapacityHeadding);
holder.imgHall=(ImageView)view.findViewById(R.id.imgListHall);
view.setTag(holder);
}
else
{
holder = (HallBookingHolder)convertView.getTag();
}
//Creating the Font to the text
Typeface tfLight = Typeface.createFromAsset(context.getAssets(),"OpenSans-Light.ttf");
Typeface tfRegular = Typeface.createFromAsset(context.getAssets(),"OpenSans-Regular.ttf");
Typeface tfsemiBold = Typeface.createFromAsset(context.getAssets(),"OpenSans-Semibold.ttf");
//Setting the font
holder.txtTitle.setTypeface(tfRegular);
holder.txtLocation.setTypeface(tfLight);
holder.txtCapacity.setTypeface(tfsemiBold);
holder.txtCapacityTitle.setTypeface(tfLight);
//Setting data to textview and image
holder.txtTitle.setText(objHall.hallName);
holder.txtLocation.setText(objHall.location);
holder.txtCapacity.setText(objHall.capacity);
//Using Guild Library Image Load using image web url
String imgurl=objHall.getImageUrl();
Glide.load(imgurl).centerCrop().into(holder.imgHall);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(context, HallDetailsActivity.class);
intent.putExtra("position", position);
context.startActivity(intent);
}
});
return view;
}
}
read it listview smooth-scrolling
Using a background thread ("worker thread") removes strain from the
main thread so it can focus on drawing the UI. In many cases, using
AsyncTask provides a simple way to perform your work outside the main
thread. AsyncTask automatically queues up all the execute() requests
and performs them serially. This behavior is global to a particular
process and means you don’t need to worry about creating your own
thread pool.
check this too http://www.itworld.com/development/380008/how-make-smooth-scrolling-listviews-android
Put all your font styles in the if (convertView==null) {} block to set them only once. Now you are setting them every time a new row is created.
Here is a list of quick tips to help you.
Reduce the number of conditions used in the getView of your adapter.
Check and reduce the number of garbage collection warnings that you get in the logs
If you're loading images while scrolling, get rid of them.
Set scrollingCache and animateCache to false (more on this later)
Simplify the hierarchy of the list view row layout
Use the view holder pattern
Here is a link to help you implement these tips. Link

retain searchView state on orientation change in a Fragment

I am trying use the actionbarcompat (with the android support library) following the android developer's blog entry.
Basically, I am trying create a Fragment extending the ListFragment. I am using an ArrayAdapter for the listview. I was able to successfully integrate the actionbar compat with a search menu item and could get the search working as well.
I now want to retain the state of the fragment on orientation change as well. I haven't set the setRetainInstance(true) in my fragment. Inorder to retain the state of the search view, i tried the following:
save the text in the SearchView in onSaveInstanceState()
in onCreateView, retrieve the searchText if available
in onCreateOptionsMenu (which would be invoked on every orientation change), I am trying to set the search query to the SearchView instance mSearchView.setQuery(mSearchText, false);
There are 2 issues that I see with this approach:
the onQueryTextChange() is called twice on orientation change - once with the searchText that has been retained (because of mSearchView.setQuery(mSearchText, false);) and again, with an empty String value. This second call with the empty String value updates the list adapater to have all the items without any filtering. I am also not really sure why this is happening.
mSearchView.setQuery(mSearchText, false); isn't setting the query in the SearchView and is not visible in the UI as well (on orientation change, the search view is expanded by default and is focused without any text value though I have set the query).
Outline of my Fragment is as follows:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// create and initialize list adapter
....
setHasOptionsMenu(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.plainlist, container, false);
if (savedInstanceState != null) {
mSearchText = savedInstanceState.getString(RetainDataKeySearchText, null);
}
return view;
}
#Override
public void onSaveInstanceState(Bundle outState) {
if (isAdded()) {
if (mSearchView != null) {
String searchText = mSearchView.getQuery().toString();
if (!TextUtils.isEmpty(searchText))
outState.putString(RetainDataKeySearchText, searchText);
}
}
super.onSaveInstanceState(outState);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.search_item_list, menu);
MenuItem searchItem = menu.findItem(R.id.menu_search);
mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
mSearchView.setOnQueryTextListener(this);
if (!TextUtils.isEmpty(mSearchText))
mSearchView.setQuery(mSearchText, false);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onQueryTextChange(String newText) {
// Called when the action bar search text has changed.
searchItems(newText);
return true;
}
#Override
public boolean onQueryTextSubmit(String searchText) {
return true;
}
private void searchItems(String searchText) {
// filter results and update the list adapter
}
The menu xml (search_item_list) file is as follows:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:sprinklr="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/menu_search"
android:icon="#android:drawable/ic_menu_search"
android:title="#string/search"
android:orderInCategory="0"
sprinklr:showAsAction="ifRoom|collapseActionView"
sprinklr:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
I would like to know if there is anything that I am missing or if there is a better alternate to retain the state of the SearchView (android.support.v7.widget.SearchView) on orientation change in a ListFragment with actionbarcompat.
As a workaround, replace your
mSearchView.setQuery(mSearchText, false);
with:
final String s = mSearchText;
mSearchView.post(new Runnable() {
#Override
public void run() {
mSearchView.setQuery(s, false);
}
});
It will set the saved string after the system has set the empty string.
I did it with AppCompatActivity rather than with Fragment. I used the equivalent solution as above but the search text wasn't visible even with the post on the search view. I added the menu item expansion before:
mSearchView.post(() -> {
searchItem.expandActionView();
mSearchView.setQuery(mSearchText, false);
mSearchView.clearFocus();
});

Filtering listview data of simple_list_item_2 items using searchview and arrayadapter

I'm writing an android application (Min API 14) which consists of 3 tabs that hold a fragment each.
One of the fragments holds a list view (with a list view item defined as simple_list_item_2) and a search view. Now, I'm trying to filter the data in the list view by a room's name (Room is my model)
protected SearchView searchView;
protected ListView view;
protected List<Room> rooms;
protected ArrayAdapter<Room> roomAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(getLayoutId(), container, false);
view = (ListView)rootView.findViewById(getListViewId());
searchView = (SearchView)rootView.findViewById(R.id.rooms_search_view);
view.setTextFilterEnabled(true);
....
roomAdapter = new ArrayAdapter<Room>(this.getActivity(), android.R.layout.simple_list_item_2, rooms){
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row;
if(convertView == null){
LayoutInflater inflater =
(LayoutInflater)getActivity().getApplicationContext().
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = (View)inflater.inflate(android.R.layout.simple_list_item_2, null);
}else{
row = (View)convertView;
}
Room data = rooms.get(position);
TextView v = (TextView) row.findViewById(android.R.id.text1);
v.setText(data.getName());
v = (TextView) row.findViewById(android.R.id.text2);
v.setText(data.getPhone());
return row;
}
};
view.setAdapter(roomAdapter);
....
return rootView;
}
#Override
public boolean onQueryTextChange(String newText) {
roomAdapter.getFilter().filter(newText);
return true;
}
The problem is that the search results are in no way relevant to the input entered in the search view. When I replace *android.R.layout.simple_list_item_2* with *android.R.layout.simple_list_item* everything work as expected.
I also read that overriding the toString() method in my model(Room) would solve such a problem,
but that didn't work either.
Any help figuring out how to filter by title(android.R.id.text1) would be highly appreciated.
Well, After I couldn't find any solution with the 'standard' android adapters, I ended up writing a custom adapter that extends BaseAdapter and implements Filterable.
You can find some hints in here if you encounter the same problem.

Programmatically change listview so that it is checkable

I've made a decent effort to find a similar question and failed, so please forgive me if this is redundant pollution.
I have a ListView and an ImageButton defined in the xml file "my_layout" corresponding to my Activity as follows:
<ListView
android:id="#+id/listViewItems"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
></ListView>
and
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/btn_delete_dark"
android:id="#+id/buttonDelete"
android:layout_weight="1">
</ImageButton>
I then populate the ListView programmatically in the onCreate() method of my Activity as follows:
public class ViewItemsActivity extends Activity implements OnClickListener,
OnItemClickListener{
ImageButton buttonDelete;
ListView listViewItems;
DatabaseHandler dbHandler;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
buttonDeleteQuestion = (ImageButton) findViewById(R.id.buttonDelete);
listViewQuestions = (ListView) findViewById(R.id.listViewItems);
buttonDelete.setOnClickListener(this);
//get data from sql table containing items
dbHandler = new DatabaseHandler(this);
String [] items = dbHandler.getItems(); //get data to populate ListView
CustomAdapter adapter = new CustomAdapter(this,
android.R.layout.simple_list_item_1, items);
listViewItems.setAdapter(adapter);
listViewItems.setOnItemClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.buttonDelete:
listViewItems.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listViewItems.setItemChecked(0, true);
break;
//other cases are irrelevant
}
}
#Override
public void onItemClick(AdapterView<?> parent, View v, int pos, long id) {
// TODO Auto-generated method stub
//non relevant code
}
}
Everything works as it should except that I want the ListView to change, in appearance and functionality, as a result of clicking buttonDelete, so that it displays checkmarks to the right of each row which can be checked by clicking on them.
It was my understanding that setChoiceMode(ListView.MULTIPLE_CHOICE) and setItemChecked(i,true) would have this effect, but clearly I've missed something as it does not.
I am quite new to android and so it is possible that I've wasted someone's time with trivialities, for which I apologize.
Thank you very much.
The solution was really simple, and is the following:
in the onClick event corresponding to buttonDelete, I simply created a new adapter whose layout id is *simple_list_item_multiple_choice* and assigned it to the ListView with the lines
CustomAdapter adapter = new CustomAdapter(this,
android.R.layout.simple_list_item_multiple_choice, items);
listViewItems.setAdapter(adapter);
which did the trick.