I’m new fairly new to Android programming, so go easy on me. 🙂
I have a list of items, when the user clicks on an item it takes them to a screen with that item details. User can swipe right and left to view the details of other items on the list.
I have some ActionBar icons like share, increase font size …etc
My problem is: If I choose item1 on the list and go to screen with the details of that item, if I swipe to item2 then click share, the contents of item1 are shared instead of item2.
Same thing happens to increasing or decreasing the font. If I choose item2 then swipe to item3 and increase font size the font size of item2 increases instead of item3.
I tried a lot of things like using SimpleOnPageChangeListener but no use, I don’t know how to implement it correctly.
Here is my code:
Main activity which contains the listview:
public class Main extends SherlockListActivity implements OnItemClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView mylist = (ListView) findViewById(android.R.id.list);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.simple_list_reda_1, R.id.list_content, getResources().getStringArray(R.array.items_list_array) );
mylist.setAdapter(adapter);
mylist.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0,View arg1, int position, long arg3)
{
Intent n = null;
switch (position){
case 0:
n = new Intent(getApplicationContext(), ViewPagerClass.class);
n.putExtra("POSITION_KEY", position);
break;
case 1:
n = new Intent(getApplicationContext(), ViewPagerClass.class);
n.putExtra("POSITION_KEY", position);
break;
case 2:
n = new Intent(getApplicationContext(), ViewPagerClass.class);
n.putExtra("POSITION_KEY", position);
break;
case 3:
n = new Intent(getApplicationContext(), ViewPagerClass.class);
n.putExtra("POSITION_KEY", position);
break;
}
if(null!=n)
startActivity(n);
}
});
}
}
ViewPagerClass:
public class ViewPagerClass extends SherlockFragmentActivity{
static final int NUM_ITEMS = 4;
MyAdapter mAdapter;
ViewPager mPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.viewpager_layout);
mAdapter = new MyAdapter(getSupportFragmentManager());
mPager = (ViewPager) findViewById(R.id.viewpager);
mPager.setAdapter(mAdapter);
//mPager.setCurrentItem(2);
final ActionBar ab = getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);
ab.setDisplayUseLogoEnabled(false);
ab.setDisplayShowHomeEnabled(false);
}
public boolean onCreateOptionsMenu(Menu menu) {
mainMenu = menu;
subMenu1 = menu.addSubMenu(0, 1, 7, "");
subMenu2 = menu.addSubMenu(0, 2, 6, "");
subMenu3 = menu.addSubMenu(0, 3, 5, "");
//some code here...
MenuItem share = menu.findItem(R.id.menu_share);
ShareActionProvider provider = (ShareActionProvider) share.getActionProvider();
provider.setShareHistoryFileName(null);
provider.setShareIntent(createShareIntent());
getSupportMenuInflater().inflate(R.menu.textsize_subm, subMenu3);
MenuItem subMenu3Item = subMenu3.getItem();
subMenu3Item.setIcon(R.drawable.ic_font_size);
subMenu3Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
private Intent createShareIntent() {
TextView tv = (TextView) findViewById(R.id.textView1);
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, tv.getText());
shareIntent.setType("text/plain");
return shareIntent;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
//some code here...
default:
return super.onOptionsItemSelected(item);
}
}
public static class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return NUM_ITEMS;
}
@Override
public Fragment getItem(int position) {
switch(position){
case 0: return FirstPageFragment.newInstance();
case 1: return SecondPageFragment.newInstance();
case 2: return ThirdPageFragment.newInstance();
case 3: return FourthPageFragment.newInstance();
}
return null;
}
}
public static class FirstPageFragment extends Fragment {
public static FirstPageFragment newInstance() {
FirstPageFragment f = new FirstPageFragment();
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.fragment1, container, false);
return V;
}
}
public static class SecondPageFragment extends Fragment {
public static SecondPageFragment newInstance() {
SecondPageFragment f = new SecondPageFragment();
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.fragment2, container, false);
return V;
}
}
public static class ThirdPageFragment extends Fragment {
public static ThirdPageFragment newInstance() {
ThirdPageFragment f = new ThirdPageFragment();
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.fragment3, container, false);
return V;
}
}
public static class FourthPageFragment extends Fragment {
public static FourthPageFragment newInstance() {
FourthPageFragment f = new FourthPageFragment();
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.fragment4, container, false);
return V;
}
}
So in short: The menu/actionbar icons won’t update to respond to the current page after swiping.
I hope my question is clear.
I figured it out, so I thought I’d post here in case anyone ran into the same problem.
The solution is quite easy actually, I don’t know why I didn’t figure it out right away.
I used ‘setOnPageChangeListener’ & put it in ‘onCreate’
Here is my code: