TabLayout fragments are instantiated as the same when debugger is not connected - java

I have a TabLayout that uses a custom adaptor (TabPagerAdapter.java), it's initialised with this function (I previously had the problem that things didn't update when replaced so it removes everything before initializing):
public boolean setPagerViewContents(int mode, ArrayList<String> titles) {
try {
mTabLayout.removeAllTabs();
mViewPager.setAdapter(null);
mViewPager.removeAllViews();
for (int i = 0; i < titles.size(); i++) {
mTabLayout.addTab(mTabLayout.newTab().setText(titles.get(i)));
}
mAdapter = new TabPagerAdapter(getSupportFragmentManager(), titles.size(), mode);
mViewPager.setAdapter(mAdapter);
mViewPager.invalidate();
return true;
} catch (Exception e) {
mErrorReporter.reportError(e);
return false;
}
}
Custom adapter (TabPageAdapter.java):
public class TabPagerAdapter extends FragmentStatePagerAdapter {
int mTabCount;
int mLayoutType;
public TabPagerAdapter(FragmentManager fm, int numberOfTabs, int layoutType) {
super(fm);
this.mTabCount = numberOfTabs;
this.mLayoutType = layoutType;
}
#Override
public Fragment getItem(int position) {
switch (mLayoutType) {
case 0:
switch (position) {
case 0:
return new Fragment15();
case 1:
return new Fragment1();
default:
return null;
}
case 1:
switch (position) {
case 0:
return new Fragment3();
case 1:
return new Fragment2();
default:
return null;
}
default:
return null;
}
}
#Override
public int getCount() {
return mTabCount;
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
When the app starts the function setPagerViewContents (with the right mode and titles) is called, after that mViewPager.setCurrentItem(number) is called to display the right tab. But now the fragment displayed in mViewPager is correct but when clicking on a tab title the fragment is the same displayed at start (the one with the index number, not the one that should have been displayed), when tapping the first (number) tab again and then again to some other tab (not number) the shown tab is correct.
The most annoying thing here is that it's NOT consistent, it sometimes happens, sometimes doesn't and it doesn't happen when the debugger is attached so I can't debug it properly. If some other code is needed please do tell, I'll update this post as quick as possible because I'd really love to see this solved, for my own sanity and for the happiness of my few users.

Change your following method:
public boolean setPagerViewContents(int mode, ArrayList<String> titles) {
try {
mAdapter = new TabPagerAdapter(getSupportFragmentManager(), titles.size(), mode);
mViewPager.setAdapter(mAdapter);
mTabLayout.setupWithViewPager(mViewPager);
return true;
} catch (Exception e) {
mErrorReporter.reportError(e);
return false;
}
}
and set text on tabs in TabPagerAdapter by overriding following method:
#Override
public CharSequence getPageTitle(int position) {
return context.getString(tabTitles[position]);
}

Related

A strange number appears to me in FragmentPagerAdapter when I call a translation R.string

I'm trying to put a text translation into a FragmentPagerAdapter
With all attempts and use
return (R.string.Chats) + "";
A strange number appeared to me in three tab ,
photo : https://i.stack.imgur.com/znIB2.png
public class SectionPagerAdapter extends FragmentPagerAdapter {
private Context context;
public SectionPagerAdapter(#NonNull FragmentManager fm) {
super(fm);
}
#NonNull
#Override
public Fragment getItem(int position) {
if(position==0){
ChatsFragment chatsFragment =new ChatsFragment();
return chatsFragment;
}
else if(position==1){
FriendsFragment firendsFragment =new FriendsFragment();
return firendsFragment;
}
else if(position==2){
RequestsFragment requestsFragment =new RequestsFragment();
return requestsFragment;
}
else return null;
}
#Override
public int getCount() {
return 3;
}
public CharSequence getPageTitle(int position){
if(position==0){
return (R.string.Chats) + "";
}
else if(position==1){
return (R.string.Friends) + "";
}
else if(position==2){
return (R.string.Requests) + "";
}
else return null;
}
}
The problem in particular here is that I want to, in short, add the text through R.string !
public CharSequence getPageTitle(int position){
if(position==0){
return (R.string.Chats) + "";
}
else if(position==1){
return (R.string.Friends) + "";
}
else if(position==2){
return (R.string.Requests) + "";
}
else return null;
}
R.string.something return the String resource id that string have. You can pass the context to constructor of view and initialize the property context that you have in your adapter class.
Then you can get strings like this:
context.getString(R.string.something)
R.string.Chats - it's not a String it's resource identifier(has an integer type). If you want to get String from resources you should use:
context.getString(R.string.Chats) instead of R.string.Chats. The same for another page titles.
just re-write you getPageTitle method and it will be ok

Master-Detail View using ViewPager

I'd like to change the master-detail implementation of my Android phone app. Currently, users can select items from a ListView, opening a new activity. To select a different activity, the user must return to the list. Instead of this pogo-sticking, I'd like the user to swipe left and right to page through the documents using a ViewPager. There can be many documents, so I'd like to load at most 3 pages at a time - the current page, the previous, and the next. Paging back and forth should then add and remove pages left and right. I've created an adapter implementing FragmentStatePagerAdapter that handles static content (e.g. TextViews) nicely. Also deleting pages seems to work OK (not included here). But when I add e.g. an EditText content is copied over from one page to the next when paging.
Below is the code for the adapter and for the activity. There are two questions I have:
What is wrong with my adapter that causes the undesired copying of EditText from one fragment to the next?
This is my first shot at this, and it's probably far from an optimal implementation. But I find this to be such a common use case that I almost feel like there would be a ready made framework for it. Could this be achieved much easier?
Pager Adapter:
public class DetailPagerAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> mFragments;
private final static String TAG = "DetailPagerAdapter";
public DetailPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
mFragments = fragments;
}
#Override
public int getCount() {
return mFragments.size();
}
#Override
public int getItemPosition(Object object) {
return PagerAdapter.POSITION_NONE;
}
#Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
public void addItem(Fragment fragment) {
mFragments.add(fragment);
notifyDataSetChanged();
}
public void removeItem(int position) {
mFragments.remove(position);
notifyDataSetChanged();
}
public void insertItem(int position, Fragment fragment) {
mFragments.add(position, fragment);
notifyDataSetChanged();
}
}
PagingActivity Base Class:
public abstract class PagingActivity
extends AppCompatActivity
implements ViewPager.OnPageChangeListener {
protected ViewPager mViewPager;
DetailPagerAdapter mViewPagerAdapter;
protected ArrayList<String> mAllItemIds;
private String mPreviousItemId;
private String mCurrentItemId;
private String mNextItemId;
private boolean mMuteOnPageSelected = false;
protected abstract Fragment getNewPageFragment(String id);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<Fragment> initialFragments = new ArrayList<>();
int currentItemIndex = mAllItemIds.indexOf(mCurrentItemId);
int pageSelection = 1;
// Add previous view.
if (currentItemIndex > 0) {
mPreviousItemId = mAllItemIds.get(mAllItemIds.indexOf(mCurrentItemId) - 1);
initialFragments.add(getNewPageFragment(mPreviousItemId));
} else {
pageSelection = 0;
mPreviousItemId = null;
}
// Add current view.
initialFragments.add(getNewPageFragment(mCurrentItemId));
// Add next view.
if (currentItemIndex < mAllItemIds.size() - 1) {
mNextItemId = mAllItemIds.get(mAllItemIds.indexOf(mCurrentItemId) + 1);
initialFragments.add(getNewPageFragment(mNextItemId));
} else {
mNextItemId = null;
}
mViewPagerAdapter = new DetailPagerAdapter(getSupportFragmentManager(), initialFragments);
mViewPager.setAdapter(mViewPagerAdapter);
mViewPager.setCurrentItem(pageSelection);
mViewPager.addOnPageChangeListener(this);
}
#Override
public void onPageSelected(int position) {
if (!mMuteOnPageSelected) {
mCurrentItemId = ((PagingFragment) (mViewPagerAdapter.getItem(mViewPager.getCurrentItem()))).getItemId();
int currentItemIndex = mAllItemIds.indexOf(mCurrentItemId);
// Navigated to the right.
if (position == mViewPagerAdapter.getCount() - 1) {
// Add next if not already pointing at the last available item.
if (currentItemIndex < mAllItemIds.size() - 1) {
mNextItemId = mAllItemIds.get(mAllItemIds.indexOf(mCurrentItemId) + 1);
mViewPagerAdapter.addItem(getNewPageFragment(mNextItemId));
} else {
mNextItemId = null;
}
// If it succeeds remove first item.
int itemCount = mViewPagerAdapter.getCount();
if ((itemCount > 3) || ((itemCount == 3) && (currentItemIndex == mAllItemIds.size() - 1))) {
mMuteOnPageSelected = true;
mViewPagerAdapter.removeItem(0);
mViewPager.setCurrentItem(1);
mMuteOnPageSelected = false;
}
}
// Navigated to the left.
else if (position == 0) {
// Add item on the left if not already pointing at the first available item.
if (currentItemIndex > 0) {
mPreviousItemId = mAllItemIds.get(mAllItemIds.indexOf(mCurrentItemId) - 1);
mViewPagerAdapter.insertItem(0, getNewPageFragment(mPreviousItemId));
} else {
mPreviousItemId = null;
}
// Check if last item needs to be removed and selection updated.
int itemCount = mViewPagerAdapter.getCount();
if (itemCount == 3) {
if (currentItemIndex == 0) {
// Points to the first of two items.
// -> do not change selection
// -> remove rightmost item.
mViewPagerAdapter.removeItem(itemCount - 1);
} else if (currentItemIndex == mAllItemIds.size() - 2) {
// Will point to the middle of 3 items.
// -> nothing to remove
// -> select middle page.
mMuteOnPageSelected = true;
mViewPager.setCurrentItem(1);
mMuteOnPageSelected = false;
}
} else if (itemCount > 3) {
// Pager contains 4 items, first item selected.
// -> remove rightmost item
// -> select middle page.
mMuteOnPageSelected = true;
mViewPagerAdapter.removeItem(itemCount - 1);
mViewPager.setCurrentItem(1);
mMuteOnPageSelected = false;
}
}
mViewPagerAdapter.notifyDataSetChanged();
}
}
}
The second question was the key: Yes, at least the current state can be achieved much easier by letting the adapter handle the full array of items. FragmentStatePagerAdapter only loads as many fragments at a time as needed, so it can handle all the manual work I had done in the activity.
Pager Adapter
public class MyPagerAdapter extends FragmentStatePagerAdapter {
private List<String> mAllItemIds;
public MyPagerAdapter(Context context, FragmentManager fm) {
super(fm);
mAllItemIds = ...
}
#Override
public int getCount() {
return mAllItemIds.size();
}
#Override
public int getItemPosition(Object object) {
return PagerAdapter.POSITION_NONE;
}
#Override
public Fragment getItem(int position) {
return MyFragment.newInstance(mAllItemIds.get(position));
}
public void removeItem(int position) {
// add needed code here to remove item also from source
// ...
mAllItemIds.remove(position);
notifyDataSetChanged();
}
}
Activity
public abstract class PagingActivity extends AppCompatActivity {
protected ViewPager mViewPager;
MyPagerAdapter mViewPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mViewPager = (ViewPager)findViewById(R.id.viewPager);
mViewPagerAdapter = new MyPagerAdapter(this, getSupportFragmentManager());
mViewPager.setAdapter(mViewPagerAdapter);
}
private void deleteItem() {
mViewPagerAdapter.removeItem(mViewPager.getCurrentItem());
}
}

Android drag and drop on dynamically created views

could really do with some help.
I have a random number of buttons being created on the fly and each button is setOnTouchListener, which looks to be working fine. The buttons are added to Linearlayout (Top) and there is another empty Linearlayout (Bottom).
I want to be able to do the following:
Drag & Drop buttons between Linearlayout (Top) & Linearlayout (Bottom)
public class GetString extends Activity{
myDragEventListener mDragListen = new myDragEventListener();
public void getString(Context context, String[] temp, int no, LinearLayout words1, LinearLayout words2) {
Button bt;
int i = 0;
for (i = 0; i < no; i++) {
bt = new Button(context);
bt.setId(i);
bt.setText(temp[i]);
bt.setOnTouchListener(new MyTouchListener());
words1.addView(bt);
bt.setOnDragListener(mDragListen);
}
}
protected class myDragEventListener implements View.OnDragListener {
public boolean onDrag(View v, DragEvent event) {
// Defines a variable to store the action type for the incoming event
final int action = event.getAction();
// Handles each of the expected events
switch(action) {
case DragEvent.ACTION_DRAG_STARTED:
return false;
case DragEvent.ACTION_DRAG_ENTERED:
v.invalidate();
return true;
case DragEvent.ACTION_DRAG_LOCATION:
return true;
case DragEvent.ACTION_DRAG_EXITED:
v.invalidate();
return true;
case DragEvent.ACTION_DROP:
return true;
case DragEvent.ACTION_DRAG_ENDED:
return true;
// An unknown action type was received.
default:
Log.e("DragDrop Example", "Unknown action type received by OnDragListener.");
break;
}
return false;
}
}
public class MyTouchListener implements View.OnTouchListener {
#Override
public boolean onTouch(View v, MotionEvent arg1) {
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadow = new View.DragShadowBuilder(v);
v.startDrag(data, shadow, null, 0);
return false;
}
}
}

Java Android - Dynamic load fragment (ViewPager)

In settngs existed checkbox, if it is checked, then a specific fragment must not be loaded. I have just 4 fragment and I use FragmentStatePagerAdapter to show them.
public class TabPagerAdapter extends FragmentStatePagerAdapter {
public TabPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
switch (i) {
case 0:
return new Fragment_One();
case 1:
return new Fragment_Two();
case 2:
return new Fragment_Three();
case 3:
return new Fragment_Four();
}
return null;
}
#Override
public int getCount() {
return 4;
}
}
As show fragments, which are not only check in the settings? I get value (true of false (Check or Uncheck) fragment, but how do not show this fragment, i dont know.
You have to adapt your getItem() method as well as the getCount() method.
Let's assume you have a method shouldShowFragment(int fragmentNumber) that tells me for a given fragment number from 0 to 3, whether it should be shown or not (depending on the settings).
Now, implement getCount() like so to return the number of fragments that should be shown:
public int getCount() {
int cnt = 0;
for (int i = 0; i < 4; i++) {
if (shouldShowFragment(i)) cnt++;
}
return cnt;
}
And implement getItem() like so to take the not showing fragments into account:
public Fragment getItem(int position) {
int cnt = -1;
for (int i = 0; i < 4; i++) {
if (shouldShowFragment(i)) cnt++;
if (cnt == position) {
switch(i) {
case 0 : return new Fragment_One();
case 1 : return new Fragment_Two();
case 2 : return new Fragment_Three();
case 3 : return new Fragment_Four();
}
}
}
return null;
}
First of all Save the all check buttons state globally(i.e. in shared preferences like btn1.setChecked == true/false whatever) and in above code do like below:-
#Override
public Fragment getItem(int i) {
switch (i) {
case 0:
if(btn0.isChecked == true)
return new Fragment_One();
case 1:
if(btn1.isChecked == true)
return new Fragment_Two();
case 2:
if(btn2.isChecked == true)
return new Fragment_Three();
case 3:
if(btn3.isChecked == true)
return new Fragment_Four();
}
return null;
}

Switch statement just returning the last case

Switch statment fix:
The switch statement is only returning the last case i.e case 4, "#0R0dfdf0FF". how can i fix this so the text view shows the the one clicked in the dialogue box?
I'm a total newbie so yes help would really be appreciated.
public class NoteEdit extends Activity {
public EditText mTitleText;
public EditText mBodyText;
public EditText mColor;
private NotesDbAdapter mDbHelper;
private static final int DIALOG_ALERT = 10;
Long mRowId;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
mDbHelper = new NotesDbAdapter(this);
mDbHelper.open();
setContentView(R.layout.note_edit);
setTitle(R.string.done);
mTitleText = (EditText) findViewById(R.id.editTitle);
mBodyText = (EditText) findViewById(R.id.editNote);
mColor = (EditText) findViewById(R.id.editColor);
mRowId = (savedInstanceState == null) ? null :
(Long) savedInstanceState.getSerializable(NotesDbAdapter.KEY_ROWID);
if (mRowId == null) {
Bundle extras = getIntent().getExtras();
mRowId = extras != null ? extras.getLong(NotesDbAdapter.KEY_ROWID)
: null;
}
populateFields();
setupActionBar();
}
private void setupActionBar() {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
setResult(RESULT_OK);
finish();
}
return super.onOptionsItemSelected(item);
}
private void populateFields() {
if (mRowId != null) {
Cursor note = mDbHelper.fetchNote(mRowId);
startManagingCursor(note);
mTitleText.setText(note.getString(
note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)));
mBodyText.setText(note.getString(
note.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY)));
mColor.setText(note.getString(
note.getColumnIndexOrThrow(NotesDbAdapter.KEY_COLOR)));
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
saveState();
outState.putSerializable(NotesDbAdapter.KEY_ROWID, mRowId);
}
#Override
protected void onPause() {
super.onPause();
saveState();
}
#Override
protected void onResume() {
super.onResume();
populateFields();
}
private void saveState() {
String title = mTitleText.getText().toString();
String body = mBodyText.getText().toString();
String color = mColor.getText().toString();
if (mRowId == null) {
long id = mDbHelper.createNote(title, body, color);
if (id > 0) {
mRowId = id;
}
} else {
mDbHelper.updateNote(mRowId, title, body, color);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch(item.getItemId()) {
case R.id.add:
showDialog(DIALOG_ALERT);
return true;
}
return super.onMenuItemSelected(featureId, item);
}
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_ALERT:
// Create out AlterDialog
android.app.AlertDialog.Builder builder = new AlertDialog.Builder(this);
final String[] colors = {"Blue", "Green", "Yellow", "Red", "Purple"};
builder.setTitle(R.string.body);
builder.setItems(colors, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// The 'which' argument contains the index position
// of the selected item
switch (which){
case 0:
mColor.setText("#000000");
case 1:
mColor.setText("#0000FF");
case 2:
mColor.setText("#0R00FF");
case 3:
mColor.setText("#0R00dsdFF");
case 4:
mColor.setText("#0R0dfdf0FF");
default:
break;
}
} });
AlertDialog dialog = builder.create();
dialog.show();
}
return super.onCreateDialog(id);
}
}
You are missing break; at the end of the switch branches.
Fall Through.
You have to add the break.
case 0:
mColor.setText("#000000");
break;
You can find that in docs
The break statements are necessary because without them, statements in switch blocks fall through: All statements after the matching case label are executed in sequence, regardless of the expression of subsequent case labels, until a break statement is encountered.
You need a break when you don't have a return otherwise it causes fall through
You need the break; after all cases except for the last one or else it'll fall through case by case
switch (which){
case 0:
mColor.setText("#000000");
break;
case 1:
mColor.setText("#0000FF");
break;
case 2:
mColor.setText("#0R00FF");
break;
case 3:
mColor.setText("#0R00dsdFF");
break;
case 4:
mColor.setText("#0R0dfdf0FF");
default:
break;
}

Categories