Sliding menu in android,misbehaviour - android

I am using Sliding menu from library jfeinstein, i have two sliding menu 'menuLeft' 'menuRight' in my activity one from left side and one from right,i have toggle buttons for respective sliding menu,how ever if menuLeft is open and if i slide from right to left in order to close menuLeft ,menuRiht also gets opened,what can be the solution to avoid this misbehaviour
here's my activity which contains SlidingMenu's
public class ChatListActivity extends SherlockActivity {
private SlidingMenu menuLeft;
private SlidingMenu menuRight;
private Button btnSliderLeftToggle;
private Button btnSliderRightToggle;
private ListView lvSliderLeft;
private ListView lvSliderRight;
private int width;
private int height;
private DBContacts db;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.chatlist_layout);
db = new DBContacts(this);
WindowManager wmanager = (WindowManager) getSystemService(WINDOW_SERVICE);
Display display = wmanager.getDefaultDisplay();
width = display.getWidth();
height = display.getHeight();
menuLeft = new SlidingMenu(this);
menuRight = new SlidingMenu(this);
initLeftSlider();
initRightSlider();
btnSliderLeftToggle = (Button) findViewById(R.id.mnuSlidingleftToggle);
btnSliderLeftToggle.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
ChatListActivity.this.menuLeft.toggle();
}
});
btnSliderRightToggle = (Button) findViewById(R.id.mnuSlidingRightToggle);
btnSliderRightToggle.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
ChatListActivity.this.menuRight.toggle();
}
});
menuLeft.setOnOpenedListener(new OnOpenedListener()
{
#Override
public void onOpened()
{
lvSliderLeft = (ListView) findViewById(R.id.lvSlidingmenuLeft);
MySLidingMenuLeftAdapter adapter = new MySLidingMenuLeftAdapter(ChatListActivity.this,
R.layout.crow_listview_lvslidingleft_chatlist);
lvSliderLeft.setAdapter(adapter);
}
});
menuRight.setOnOpenedListener(new OnOpenedListener()
{
#Override
public void onOpened()
{
lvSliderRight = (ListView) findViewById(R.id.lvSlidingmenuRight);
String column[] = new String[] { DBContacts.USERNAME};
int[] viewId = { R.id.txtContactName};
Cursor dataBaseCursor = db.getAllContacts();
MySLidingMenuRightAdapter customContactListAdapter = new MySLidingMenuRightAdapter(
ChatListActivity.this, R.layout.crow_lvslidingmenu_right_chatlist, dataBaseCursor, column,
viewId, 0);
lvSliderRight.setAdapter(customContactListAdapter);
}
});
}
private void initRightSlider()
{
menuRight.setMode(SlidingMenu.RIGHT);
menuRight.setBehindOffsetRes(R.dimen.slidingmenu_offset);
menuRight.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menuRight.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
menuRight.setMenu(R.layout.sliding_menu_chatlist_right);
menuRight.setFadeDegree(0.35f);
}
private void initLeftSlider()
{
menuLeft.setMode(SlidingMenu.LEFT);
menuLeft.setBehindOffsetRes(R.dimen.slidingmenu_offset);
menuLeft.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menuLeft.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
menuLeft.setMenu(R.layout.sliding_menu_chatlist_left);
menuLeft.setFadeDegree(0.35f);
}
#Override
public void onBackPressed()
{
super.onBackPressed();
System.exit(0);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add("Refresh");
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
new SendNativeContacts(this).execute();
return true;
}
}

I have never faced this issue when i used both Left and Right SlidingMenu but for your problem you can have look at SlidingMenu Issues and you can try this solution for your problem. It may help you.
EDIT :
try this
You will need a patch which calls onOpened and onClosed methods for right menu it may have been included in latest code. Write logs to check method calls.
slidingMenuLeft.setOnOpenedListener(new OnOpenedListener() {
#Override
public void onOpened(int pos) {
slidingMenuRight.setSlidingEnabled(false);
}
});
slidingMenuLeft.setOnClosedListener(new OnClosedListener() {
#Override
public void onClosed() {
slidingMenuRight.setSlidingEnabled(true);
}
});
slidingMenuRight.setOnOpenedListener(new OnOpenedListener() {
#Override
public void onOpened(int pos) {
slidingMenuLeft.setSlidingEnabled(false);
}
});
slidingMenuRight.setOnClosedListener(new OnClosedListener() {
#Override
public void onClosed() {
slidingMenuLeft.setSlidingEnabled(true);
}
});

Related

How can I have scrolling to last page working properly in a paginated scroll in android ListView

Attention! This question is not about dynamic loading of items into a very long ListView.
This is about adding PageUP and PageDown buttons to a ListView so that user can touch the button and scroll ListView page by page. Page means all fully and partially visible items on the screen.
I have partially implemented this in the following code, but my problem is that when I have lets say 10 items of approximately same height in the listview and 7 of them fit into the first page, then when I press PgDown button, user expects that item 8 to be on top of the screen (next page), but because there are only 10 items, ListView scrolls to the bottom of the list and because there is no extra scroll space I have item number 4 on top.
What is the best solution in this situation?
Should I add one item to the end of the list which will make the last page the height of the screen or there are any better options?
Here is my code:
public class cPaginatedListViewHelper {
Activity m_parentActivity;
private ListView mList;
//controls
private LinearLayout m_PagingLL;
//buttons
private ImageButton m_btnPrevPage;
private ImageButton m_btnNextPage;
private ImageButton m_btnExitPaginatedMode;
public cPaginatedListViewHelper(ListActivity mParent) {
this.m_parentActivity = mParent;
m_btnPrevPage=(ImageButton) mParent.findViewById(R.id.btnPrevPage);
m_btnNextPage=(ImageButton) mParent.findViewById(R.id.btnNextPage);
m_btnExitPaginatedMode =(ImageButton) mParent.findViewById(R.id.btnClosePage);
if(m_btnPrevPage!=null) {
m_btnPrevPage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showSiblingPage(-1);
}
});
m_btnPrevPage.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
mList.smoothScrollToPosition(0);
return true;
}
}
);
}
if(m_btnNextPage!=null) {
m_btnNextPage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showSiblingPage(1);
}
});
m_btnNextPage.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
mList.smoothScrollToPosition(mList.getCount());
return true;
}
}
);
}
m_btnExitPaginatedMode.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setEnabled(false);
m_PagingLL.setVisibility(View.GONE);
}
});
mList=mParent.getListView();
m_PagingLL = (LinearLayout) mParent.findViewById(R.id.pageControls);
}
public void updateControlsVisibility()
{
ViewTreeObserver observer = mList.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if (willMyListScroll()) {
boolean psm = isEnabled();
//enable or disable
m_PagingLL.setVisibility( psm ? View.VISIBLE : View.GONE);
((View)mList).setVerticalScrollbarPosition(psm ? View.SCROLLBAR_POSITION_LEFT: View.SCROLLBAR_POSITION_RIGHT);
}
else
{
m_PagingLL.setVisibility(View.GONE);
((View)mList).setVerticalScrollbarPosition(View.SCROLLBAR_POSITION_RIGHT);
}
}
});
}
private boolean willMyListScroll() {
try {
int pos = mList.getLastVisiblePosition();
if (mList.getChildAt(pos).getBottom() > mList.getHeight()) {
return true;
} else {
return false;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
private void showSiblingPage(int shift)
{
if(mList!=null) {
int iScrollPageHeight = mList.getHeight();
mList.scrollListBy(iScrollPageHeight * shift);
}
}
public void setEnabled(boolean psm) {
MyApp.Pref.edit().putBoolean("PSModeEnabled", psm).commit();
}
public boolean isEnabled(){
return MyApp.Pref.getBoolean("PSModeEnabled", false);
}
public void pagedScrollEnableDisable() {
boolean pagingEnabled = isEnabled();
pagingEnabled=!pagingEnabled;
setEnabled(pagingEnabled);
m_PagingLL.setVisibility( pagingEnabled ? View.VISIBLE : View.GONE);
updateControlsVisibility();
}
}
I finished up with using ListView's footer with variable height as shown in the following code:
LayoutInflater inflater = m_parentActivity.getLayoutInflater();
m_footerView = inflater.inflate(R.layout.listview_paged_overscroll, mList, false );
ViewGroup.LayoutParams lp =m_footerView.getLayoutParams();
if(m_tvPageNum!=null) recalcPagination();
if(lp!=null) lp.height = m_extraScrollFooterHeight;
int iFooters = mList.getFooterViewsCount();
if(iFooters==0) mList.addFooterView(m_footerView);

RecyclerView index not updating while removing item using FirebaseRecycleradapter

I have implemented FirebaseRecyclerAdapter for a dynamic recyclerView and while I am trying to delete an item from the list, initially items at wrong indexes are getting deleted and after more items deletion, i am getting IndexOutOfBoundsException. I have already tried all solutions I found which can be seen as commented inside populateViewHolder() method. Please, if you have to downvote, at least give a proper answer and downvote it as I have already tried all solutions possible. Below I am posting my code. Please have a look.
Check the delete.setOnClickListener():
adapter = new FirebaseRecyclerAdapter<Slc, viewHolder>(
Slc.class,
R.layout.single_slc_row,
viewHolder.class,
FirebaseDatabase.getInstance().getReference().child("DCU")) {
#Override
protected void populateViewHolder(final viewHolder viewHolder, final Slc model, final int position)
{
//expand collapse
viewHolder.vis.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
// ViewAnimationsUtils.expand(viewHolder.inv);
viewHolder.collapse.setVisibility(View.VISIBLE);
viewHolder.inv.setVisibility(View.VISIBLE);
}
});
viewHolder.collapse.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
// ViewAnimationsUtils.collapse(inv);
viewHolder.inv.setVisibility(View.GONE);
viewHolder.collapse.setVisibility(View.GONE);
}
});
String s=adapter.getRef(position).getKey();
viewHolder.name.setText(s);
//dimming get
viewHolder.dimming.setProgress(model.getPercentage());
//on off get
if(model.getON_OFF()==1)
{
viewHolder.onOff.setChecked(true);
}
else if(model.getON_OFF()==0)
{
viewHolder.onOff.setChecked(false);
}
//dimming set
//viewHolder.dimming.incrementProgressBy(10);
viewHolder.showProgress.setText( viewHolder.dimming.getProgress()+"%");
viewHolder.dimming.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
if(position!=RecyclerView.NO_POSITION)
{
if(seekBar.isShown() && fromUser)
{
progress= (progress/10)*10;
adapter.getRef(position).child("Percentage").setValue(progress);
viewHolder.showProgress.setText(progress+"%");
}
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
viewHolder.onOff.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if(buttonView.isPressed() &&isChecked){
adapter.getRef(position).child("ON_OFF").setValue(1);
}else{
adapter.getRef(position).child("ON_OFF").setValue(0);
}
}
});
viewHolder.delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
final Dialog dialog = new Dialog(myContext);
dialog.setTitle("Delete Slc?");
dialog.setContentView(R.layout.delete_slc);
dialog.setCancelable(false);
Window window = dialog.getWindow();
if(window == null) return;
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
lp.gravity = Gravity.CENTER;
dialog.getWindow().setAttributes(lp);
TextView delete = (TextView)dialog.findViewById(R.id.deleteSlc);
final String key= adapter.getRef(position).getKey();
delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
// int pos = viewHolder.getAdapterPosition();
ref.child(key).setValue(null);
// int selectedItems = position;
// for (int i = selectedItems; i >= selectedItems; i--) {
// adapter.getRef(i).removeValue();
adapter.notifyItemRemoved(position);
// }
// adapter.getRef(position).removeValue();
//
// adapter.notifyItemRemoved(position);
// adapter.notifyDataSetChanged();
dialog.dismiss();
}
});
TextView dialogButton = (TextView) dialog.findViewById(R.id.canceld);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
dialog.dismiss();
}
});
dialog.show();
}
});
}
};
slcList.setAdapter(adapter);
The exception I am getting after deleting some items:
java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at com.firebase.ui.database.FirebaseArray.getItem(FirebaseArray.java:52)
at com.firebase.ui.database.FirebaseRecyclerAdapter.getRef(FirebaseRecyclerAdapter.java:150)
The error is caused by usage of position object inside of function not directly under populateViewHolder. Like in this one: (but not limited to)
protected void populateViewHolder(final viewHolder viewHolder, final Slc model, final int position) {
// here position object is valid
...
viewHolder.dimming.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged( ... ) {
// but here, position object is likely to be incorrect
// especially when you add/remove item of list
}
...
}
And the solution is simply to replace all the position objects that are not directly under populateViewHolder into viewHolder.getAdapterPosition().
Note: I copy it from my comment in case others seek for answer and miss the comment

ListView not getting updated after insertion of entries in SqliteDatabase

I have read a many posts on this topic but none is helping.I have a listview and I have a dialog.The dialog contains three Edittext fields one for name,description and date.I have two Buttons on the dialog save and cancel. On clicking the save button , insert the data in database but listview doesn't get updated.
This is the method where I specify the function of the save button :
save_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
selectedModel=new Model();
selectedModel.setTask_title(task_Title.getText().toString());
selectedModel.setTask_description(task_Description.getText().toString());
selectedModel.setTask_date(task_Date.getText().toString());
db.insertTask(selectedModel);
//Toast.makeText(getApplicationContext(),"model ="+selectedModel.getTask_title(),Toast.LENGTH_LONG).show();
dialog.dismiss();
//myAdapter.notifyDataSetChanged();
}
});
I have tried using .notifyDataSetChanged() but it didn't work.
This is my main code where I specify all the functions :
public class SecondScreen extends ListActivity implements AppCompatCallback,AdapterView.OnItemClickListener {
private Toolbar bar;
private AppCompatDelegate delegate;
List<Model> list;
ArrayAdapter<String> myAdapter;
final Context context=this;
Model selectedModel;
AGSQLiteHelper db;
private EditText task_Title,task_Description,task_Date;
#Override
protected void onCreate(Bundle savedInstanceState) {
delegate=AppCompatDelegate.create(this,this);
super.onCreate(savedInstanceState);
delegate.setContentView(R.layout.activity_second_screen);
bar=(Toolbar)findViewById(R.id.toolbarID);
delegate.setSupportActionBar(bar);
ActionBar ab=delegate.getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);
db=new AGSQLiteHelper(this);
db.onUpgrade(db.getWritableDatabase(),1,2);
db.createTask(new Model("abcde","description abcde","19/11/12"));
db.createTask(new Model("bcdese","description bcdese","18/12/12"));
db.createTask(new Model("mnbv","description mnbv","2/11/10"));
db.createTask(new Model("poiuy","description poiuy","1/5/6"));
list=db.getAllTasks();
List<String> listTitle=new ArrayList<String>();
for(int i=0;i<list.size();i++){
listTitle.add(i,list.get(i).getTask_title());
}
myAdapter=new ArrayAdapter<String>(this,R.layout.row,R.id.tasktitleTextID,listTitle);
getListView().setOnItemClickListener(this);
setListAdapter(myAdapter);
}
#Override
public void onSupportActionModeStarted(ActionMode mode) {
}
#Override
public void onSupportActionModeFinished(ActionMode mode) {
}
#Nullable
#Override
public ActionMode onWindowStartingSupportActionMode(ActionMode.Callback callback) {
return null;
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { }
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==R.id.add){
Toast.makeText(getApplicationContext(),"Add was clicked",Toast.LENGTH_LONG).show();
final Dialog dialog=new Dialog(context);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Dialog");
Button save_button=(Button)dialog.findViewById(R.id.saveButtonID);
Button cancel_button=(Button)dialog.findViewById(R.id.cancelButtonID);
task_Title=(EditText)dialog.findViewById(R.id.titleEditTextID);
task_Description=(EditText)dialog.findViewById(R.id.descriptionEditTextID);
task_Date=(EditText)dialog.findViewById(R.id.dateEditTextID);
dialog.show();
save_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
selectedModel=new Model();
selectedModel.setTask_title(task_Title.getText().toString());
selectedModel.setTask_description(task_Description.getText().toString());
selectedModel.setTask_date(task_Date.getText().toString());
db.insertTask(selectedModel);
//Toast.makeText(getApplicationContext(),"model ="+selectedModel.getTask_title(),Toast.LENGTH_LONG).show();
dialog.dismiss();
//myAdapter.notifyDataSetChanged();
}
});
cancel_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
if(item.getItemId()==R.id.done){
Toast.makeText(getApplicationContext(),"Done was clicked",Toast.LENGTH_LONG).show();
}
return true;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu,menu);
return true;
}
}
Any suggestions?
you need to add the new entry into the listTitle used by the adapter
db.insertTask(selectedModel);
dialog.dismiss();
listTitle.add(selectedModel.getTask_title());
myAdapter.notifyDataSetChanged();
so make your listTitle global to the class so that you can use it inside other functions
public class SecondScreen extends ListActivity...{
private EditText task_Title;
private List<String> listTitle;
// declare list like this as global to class
... onCreate(..){
listTitle=new ArrayList<String>();
}
}

Changing a textview above viewpager depending on what page?

I have an activity that has 2 buttons (Previous, Next) and a text view in between the two. Below that is a viewpager.
I would like to change the textview to the title of whatever page they are on.
Here is the class where it all happens:
public class MyWorkout extends BaseActivity {
NonSwipeableViewPager vp;
TextView txtViewTitle;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_workout);
mToolBar = activateToolbar();
setUpNavigationDrawer();
getSupportActionBar().setTitle("My Workout");
Intent intent = getIntent();
String workouttitle = intent.getStringExtra("workout");
switch(workouttitle) {
case "w29" : {
getSupportActionBar().setTitle("My Workout - Workout 29");
break;
}
case "w30" : {
getSupportActionBar().setTitle("My Workout - Workout 30");
break;
}
}
Button btnPrev = (Button) findViewById(R.id.btnPrevious);
Button btnNext = (Button) findViewById(R.id.btnNext);
vp = (NonSwipeableViewPager) findViewById(R.id.view_pager);
txtViewTitle = (TextView) findViewById(R.id.txtViewTitle);
this.addPages(vp);
//THIS IS WHAT I HAVE TRIED
if (vp.getCurrentItem() == 0) {
txtViewTitle.setText("Week One, Monday");
}
if (vp.getCurrentItem() == 1) {
txtViewTitle.setText("Week One, Wednesday");
}
btnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MoveNext();
}
});
btnPrev.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MovePrevious();
}
});
}
public void MoveNext() {
//it doesn't matter if you're already in the last item
vp.setCurrentItem(vp.getCurrentItem() + 1);
}
public void MovePrevious() {
//it doesn't matter if you're already in the first item
vp.setCurrentItem(vp.getCurrentItem() - 1);
}
private void addPages(NonSwipeableViewPager pager) {
MyFragPagerAdapter adapter = new MyFragPagerAdapter(getSupportFragmentManager());
Intent intent = getIntent();
String workout = intent.getStringExtra("workout");
Bundle args = new Bundle();
args.putString("workout", workout);
Week1Mon week1MonFragment = new Week1Mon();
week1MonFragment.setArguments(args);
adapter.addPage(week1MonFragment);
Week1Wed week1WedFragment = new Week1Wed();
week1WedFragment.setArguments(args);
adapter.addPage(week1WedFragment);
Week1Fri week1Fri = new Week1Fri();
week1Fri.setArguments(args);
adapter.addPage(week1Fri);
Week2Mon week2MonFragment = new Week2Mon();
week2MonFragment.setArguments(args);
adapter.addPage(week2MonFragment);
Week2Wed week2WedFragment = new Week2Wed();
week2WedFragment.setArguments(args);
adapter.addPage(week2WedFragment);
Week2Fri week2Fri = new Week2Fri();
week2Fri.setArguments(args);
adapter.addPage(week2Fri);
pager.setAdapter(adapter);
}
}
The part I have tried:
if (vp.getCurrentItem() == 0) {
txtViewTitle.setText("Week One, Monday");
}
if (vp.getCurrentItem() == 1) {
txtViewTitle.setText("Week One, Wednesday");
}
This sets the textview to Week One, Monday. Unfortunately it doesn't change when the user hits the Next button. I'm also hoping theres a better way, as I don't want to "assume" that the current item is that day of the week, because every week is different. I also need to figure out how to set the current page when the app is opened, because it saves the position is shared pref and I have to load it in the class above, and somehow set the page to a current page (preferably by the title)
Here is MyFragPagerAdapter:
public class MyFragPagerAdapter extends FragmentPagerAdapter {
ArrayList<Fragment> pages = new ArrayList<>();
public MyFragPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return pages.get(position);
}
#Override
public int getCount() {
return pages.size();
}
public void addPage(Fragment f) {
pages.add(f);
}
#Override
public CharSequence getPageTitle(int position) {
return pages.get(position).toString();
}
}
Thanks for any help, let me know if you need to see more code!
You can use,
viewPager.addOnPageChangeListener(new OnPageChangeListener() {
public void onPageScrollStateChanged(int state) {}
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
public void onPageSelected(int position) {
// Check if this is the page you want.
}});

Android Viewflow go to view with a button

I am trying to make a nice horizontal viewgroup with the ViewFlow class.
I want to combine it with buttons. For example, when I click button 2 I go to view 2 of the viewgroup. Is that possible? I currently got this:
public class HousingActivity extends BaseActivity implements OnClickListener {
ViewFlow viewFlow;
ListView listView;
HousingViewflowAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.housing_activity);
init();
}
private void init() {
viewFlow = (ViewFlow) findViewById(R.id.viewflow);
setOnClickListner(this, this, new int[] { R.id.housing_button1,
R.id.housing_button2, R.id.housing_button3,
R.id.housing_button4 });
adapter = new HousingViewflowAdapter(this);
viewFlow.setAdapter(adapter);
viewFlow.setOnViewSwitchListener(new ViewSwitchListener() {
#Override
public void onSwitched(View view, int position) {
}
});
listView = (ListView) findViewById(R.id.housing_view1_listview);
ArrayList<Community> communityList = new ArrayList<Community>();
// Some dummy data
for (int i = 0; i < 10; i++) {
communityList.add(new Community("Place " + new Random().nextInt(),
new Random().nextInt(), 000000000, ""));
}
listView.setAdapter(new MainListViewAdapter(this, 0, communityList));
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// viewFlow.onConfigurationChanged(newConfig);
}
#Override
public void onClick(View v) {
super.onClick(v);
switch (v.getId()) {
case R.id.housing_button1:
break;
case R.id.housing_button2:
break;
case R.id.housing_button3:
break;
case R.id.housing_button4:
break;
}
}
}
Thanks in advance.
You can look into:
viewFlow.setSelection(int position)
Ah ! I found it !
viewFlow.setAdapter(adapter,number);
The number being the page you want to show

Categories

Resources