get wrong position in recyclerview which in fragment with viewpager - android-fragments

I have a viewpager with 4 Fragment, there are a RecyclerView and SwipeRefreshLayout in each Fragment. xml code like this:
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipe_refresh_widget"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/orderList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/background"/>
</android.support.v4.widget.SwipeRefreshLayout>
When I switch ViewPager from left to right, I get the RecyclerView in Fragment like
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
//set swipeRefreshWidget can not refresh when recyclerView is not in the top
mSwipeRefreshWidget.setEnabled(orderLayoutManager.findFirstCompletelyVisibleItemPosition() == 0);
}
}
});
But when I switch viewpager from page A to page B and to page C ,when in the page C, the recyclerView is not in the top(findFirstCompletelyVisibleItemPosition is not 0),but at that time ,I can not get the findFirstCompletelyVisibleItemPosition when switch to page C ,so the swipeRefreshWidget can refresh , but I want it can not refresh.
like this.
PS.the recyclerView in page B is in the top,but in page A and page C,recyclerView is in the middle.

I have encountered similar problems. first page display the second pages' content.
Because i set:
static string week;
and week belongs to class. when i return new Fragment()
twice. the first fragment.week had changed. the result is first.week == second.week.
also because FragmentStatePagerAdapter will Execute getitem() twice. so the content of first pager is wrong. he will display second fragment's content in first page.

Related

How does one call a method on the changing of a Tab in JavaFx?

Ok so here is what i am trying to do: I have a page called 'SettingsPage'. Now, in this page i Have a tabPane with 3 tabs; users, buttons, and sales. Buttons and Users both have tables which need to be populated with data before they are viewed by the user. When I was first making the program I just used a 'initialize' method to populate the user table on the opening of 'SettingsPage'. However, now that I am beginning to try and populate the buttons table, I am encountering a memory error because now there is too much data to be loaded at once together.
So, I though a good solution to this would be to make an Event method that is called whenever a certain tab is opened. I am using SceneBuilder atm, and what seemed to be the equivalent of this was onSelectionChanged, however I can't seem to use this as you would use 'methodEg(ActionEvent event)...'. So my question is, how can ensure that a method will be called on the opening of a certain tab.
For example, when 'buttonTab' is clicked, the 'populateButtonTable' method is called.
The answer to this was this code:
And if anyone asks why I didn't use .isSelected()... I did, it for some reason didn't work, but what I have done below does, so I dunno...
'''
#FXML public void initialize(URL url, ResourceBundle rb){
tabs.getSelectionModel().selectedItemProperty().addListener(
new ChangeListener<Tab>() {
#Override
public void changed(ObservableValue<? extends Tab> ov, Tab t, Tab t1)
{
if (tabs.getSelectionModel().getSelectedItem() == ExampleTab)
{
//do whatever
}
}
}
);

JavaFx WebView - how to scroll it to the bottom

I have made a conversation stage in JavaFx with a WebView that is displaying whole conversation. I want to show it from the bottom where the newest messages are so I used a function "execute script":
public void initialize() throws SQLException
{
buildText();
conversationText.getEngine().loadContent(text1);
conversationText.getEngine().executeScript("window.scrollTo(0, document.body.scrollHeight);");
conversationText.getEngine().setUserStyleSheetLocation(getClass().getResource("/html/style.css").toExternalForm());
}
I wrote this function in an initialize() void but it doesn't work, when I open the conversation it shows me all from the top where the eldest messages are :
But what is interesting it works properly when I use this metod form a void that is connected with a button named "script"
public void script()
{
conversationText.getEngine().executeScript("window.scrollTo(0, document.body.scrollHeight);");
}
when I press this button it scrolls to the bottom :
Is there any way to do this correct in an initialize void ? I have tried many ways like inserting the button method script() into initialize but i noticed that if I use function webengine.loadcontent() it always displays me all from the top.
I want to show the user the newest messages at the bottom from the beginning , not after pushing a button.

How to handle up button in toolbar - Kotlin

I have manually/programmatically set up an up button in my toolbar for a fragment page with the following code in onCreateOptionsMenu in the fragment:
(activity as AppCompatActivity).setSupportActionBar?.setDisplayHomeAsUpEnabled(true)
Tapping the system back button will take the user back to the previous fragment but without an up button (that works) I think some users may get lost.
I am having difficulty trying to work out how to catch and handle the up button to pop the fragment off the back stack.
This SO post shows how to catch the the up button click however it is in Java and doesn't go on to explain how you would navigate up.
I think it needs to look something like the code below but there are errors everywhere:
The case android.R.id.home is showing an 'Incompatible types:Int and MenuItem' error?
onBackPressed() is showing an 'Unresolved reference' error.
Bad code:
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
when (item) {
android.R.id.home -> {
onBackPressed()
return true
}
else -> return super.onOptionsItemSelected(item)
}
}
UPDATE:
A comment to another SO post has a potential solution in Kotlin. It catches the click on the up button and goes back to the previous fragment page but then the up button doesn't go away. So the up button now persists even on the top level fragment destinations in my app (the pages corresponding to each tab in the BottomNavigationView).
I think this might have to do with the fact that there is only one activity in my app and the way that I have set up the up button in the fragment as mentioned above? If so, is there a workaround or other way to set up the up button by referencing the fragment instead of the whole activity?
If it helps, this is the code in the RecyclerView inner ViewHolder class in the adapter.kt file that navigates to the fragment page in question:
class AdapterListItemDetails(val items: List<ItemsList>) : RecyclerView.Adapter<AdapterListItemDeatils.ItemsViewHolder>() {
//overrides for OnCreateViewHolder, getItemCount, onBindViewHolder
inner class ItemsViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var currentItem: ItemsList? = null
var currentPosition: Int = 0
init {
itemView.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.goto_details, null))
}
fun setData(itemsList: ItemsList, position: Int) {
itemView.tview_Keys.text = itemsList!!.nameText
this.currentItem = itemsList
this.currentPosition = position
}
}
}
You have to override onBackPressed() method in activity and handle the fragment transactions with your manual code. If you could share some snippet of activity and fragment transactions will help me to give some proper solution.
Hi this is what i usually do:
in an activity find the navController from your navHostFragment
val navController = this.findNavController(R.id.myNavHostFragment)
Make sure it's connected to the ActionBar
NavigationUI.setupActionBarWithNavController(this, navController)
Then simply override onSupportNavigateUp, find your navController then navigate up
override fun onSupportNavigateUp(): Boolean{
val navController = this.findNavController(R.id.myNavHostFragment)
return navController.navigateUp()
}

How to make center the hint of searchView in android?

Can anyone tell how make Search View hint in centre? Thanks in advance.
If you are using androidx. Use this code
val searchEditText = searchView.findViewById(androidx.appcompat.R.id.search_src_text) as EditText
searchEditText.gravity = Gravity.CENTER
To center the hint and query text use a costume style for your search view :
<style name="AppSearchView" parent="Widget.AppCompat.SearchView" >
<item name="android:textAlignment">center</item>
</style>
and then apply this style to your search view :
<android.support.v7.widget.SearchView
android:layout_width="match_parent"
android:layout_height="wrap_content"
...
app:theme="#style/AppSearchView" />
To center the hint on the SearchView you need to center the gravity in the EditText contained in the SearchView.
You can find that specific view by using the Layout Inspector in Android Studio (Tools->Android->Layout Inspector). When you click on the search view in your UI, you can see it has a SearchView$SearchAutoComplete. That's an inner class in the SearchView which extends AutoCompleteTextView, which extends EditText. So, that's the view you want to change. Observe in the property-value pane of the Layout Inspector that the id is id/search_src_text so you can use that to get the view and center the gravity, like this:
int id = getResources().getIdentifier("android:id/search_src_text", null, null);
EditText searchEditText = (EditText) mSearchView.findViewById(id);
if (searchEditText != null) {
searchEditText.setGravity(Gravity.CENTER);
}
Since the id of the EditText isn't officially documented, there's a null check in there just in case the id changes or isn't consistent everywhere.
For android.support.v7.widget.SearchView, use the following:
public static void centerHintText(SearchView searchView){
EditText searchEditText = (EditText) searchView.findViewById(R.id.search_src_text);
if (searchEditText != null) {
searchEditText.setGravity(Gravity.CENTER);
}
}
I followed Cody's example but I would like to add onto Cody's answer above that it might actually be better to cast the found view to SearchView.SearchAutoComplete (a public static class) instead of EditText, since that's the view directly associated with R.id.search_src_text. This is to avoid any future problems if the Android team decides to swap out the inherited EditText class for another class.
See the associated view here: https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/layout/search_view.xml

Where should I put my code in ActionBar.TabListener

I have just started with an ActionBar.TabListener with 3 tabs.
I selected new "Tabbed activity" in Android Studio.
My activity is called test...not the best name but I'm just trying to learn:)
I have a listView in fragment_test.xml that I want to fill with data after a raw sql search.
If I put this code in onCreateView then everytime I click on a tab it will re-write that tab with the same info in the listView.
What I want is to have diffrent information in those tabs.... then I need to know which tab that is clicked. That I did with mViewPager.getCurrentItem() ....Is this right? How can i get the name of the tab instead?
I have also found onTabSelected...should I put my code here? In this case I think I know which tab that is selected but is it really wise to put the code here?
If I do this then listView1 becomes null...why?:
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
listView1 = (ListView)findViewById(R.id.listView2);
listAdapter2 = new ArrayAdapter<String>(context, R.layout.simplerow, testArray);
listAdapter2.notifyDataSetChanged();
listView1.setAdapter(listAdapter2);
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView2"
android:layout_centerHorizontal="true" />
If I understand it correctly, you want to load Fragment's ListView according to Tab's selected position.
If that's the case, you should not modify Fragment's ListView in Activity. Instead, pass Tab's selected position to Fragment as an argument from Activity and load ListView in Fragment according to that position.

Resources