Android: Localizador de vista horizontal no en la pantalla principal
Frecuentes
Visto 1,199 veces
0
I recently have started programming again for the android and am currently working on a app that I would like to implement horizontal view pagers so the user in one instance can swipe between two pages to enter/update information and in a later instance swipe between four pages to view information that they have entered/updated. I am basically making an electronic character sheet for roleplaying games.
I have been working off of these tutorials for horizontal view pagers: http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-horizontal-view-paging/
http://manishkpr.webheavens.com/android-viewpager-example/
My question is of all the tutorials I have seen, the h. view pager is used off of the main activity screen, is there a way to implement the horizontal view pager off of a subsequent screen? Every time I have tried to implement the code to work off of a page other than a main screen it has crashed as soon as I got to that page.
So, long story short, has anyone successfully implemented horizontal view pagers on a non main page and if so, how?
I hope that I have made sense, but if you have any further questions please let me know!
08-24 01:44:34.310: I/ActivityManager(144): START {cmp=com.echaractersheet/.CharacterStats1} from pid 15115 08-24 01:44:34.360: D/AndroidRuntime(15115): Shutting down VM 08-24 01:44:34.360: W/dalvikvm(15115): threadid=1: thread exiting with uncaught exception (group=0x40a581f8) 08-24 01:44:34.370: E/AndroidRuntime(15115): FATAL EXCEPTION: main 08-24 01:44:34.370: E/AndroidRuntime(15115): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.echaractersheet/com.echaractersheet.CharacterStats1}: java.lang.NullPointerException
characterstats.xml:
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/characterstatspager" />
characterstats1.xml and characterstats2.xml are the two pages I want to swipe between
CharacterStatsPagerAdapter.java: ...
public class CharacterStatsPagerAdapter extends PagerAdapter {
public int getCount() {
return 2;
}
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId =0;
switch (position) {
case 0:
resId = R.layout.characterstats1;
break;
case 1:
resId = R.layout.characterstats2;
break;
}
View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
}
CharacterStats1.java:
CharacterStatsPagerAdapter adapter = new CharacterStatsPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.characterstatspager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
1 Respuestas
0
well the pager will work with the data in the adapter, so as long as you keep the objects in the adapter, you are good to go. so maybe before going to the next screen you should save the objects in the adapter (maybe to a static field in another class, just for a moment) and when you get to the new page you can retrieve the objects and put them in the adapter of the new viewpager and add the new pages. That is what i will do in your case.
ACTUALIZACIÓN:
this is just an example, this needs more lines of code, but is a good start.
public class Fields {
private String name;
private String lastName;
private int age;
private boolean male;
public Fields(){
this.name = "";
this.lastName = "";
}
public Fields(String name, String lastName, int age, boolean male) {
this.name = name;
this.lastName = lastName;
this.age = age;
this.male = male;
}
public View getRepresentation(Context mContext){
/*Create the view that ViewPager will display*/
LinearLayout layout = new LinearLayout(mContext);
layout.addView(new TextView(mContext)); //Name
layout.addView(new TextView(mContext)); //LastName
layout.addView(new TextView(mContext)); //Age
layout.addView(new CheckBox(mContext)); //Male/Female
return layout;
}
}
The View Pager Adapter
public class ViewPagerAdapter extends PagerAdapter {
private List<Fields> pages;
private Context mContext;
public ViewPagerAdapter( Context mContext )
{
this.mContext = mContext;
}
public void setPages(List<Fields> pages){
this.pages = pages;
}
public List<Fields> getPages(){
return this.pages;
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
@Override
public int getCount()
{
return pages.size();
}
@Override
public Object instantiateItem( View pager, int position )
{
View view = pages.get(position).getRepresentation(mContext);
view.setId(position);
return view;
}
@Override
public void destroyItem( View pager, int position, Object view )
{
((ViewPager)pager).removeViewInLayout( (View) view );
}
@Override
public boolean isViewFromObject( View view, Object object )
{
return view.equals( object );
}
}
Clase principal
public class MainActivity extends Activity {
private Context mContext;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.mContext = getApplicationContext();
setContentView(R.layout.viewpager_layout);
final List<Fields> fields = new ArrayList<Fields>();
fields.add(new Fields());
ViewPager mPager = (ViewPager) this.findViewById(R.id.viewpager);
Button nextActivity = (Button) this.findViewById(R.id.nextAct);
ViewPagerAdapter mAdapter = new ViewPagerAdapter(mContext);
mAdapter.setPages(fields);
mPager.setAdapter(mAdapter);
nextActivity.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SecondActivity.pages = fields;
Intent intent = new Intent(mContext, SecondActivity.class);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Segunda clase
public class SecondActivity extends Activity {
public static List<Fields> pages;
private Context mContext;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.mContext = getApplicationContext();
setContentView(R.layout.viewpager_layout);
final List<Fields> fields = pages;
pages = null;
fields.add(new Fields()); //Add the new Pages
ViewPager mPager = (ViewPager) this.findViewById(R.id.viewpager);
Button nextActivity = (Button) this.findViewById(R.id.nextAct);
ViewPagerAdapter mAdapter = new ViewPagerAdapter(mContext);
mAdapter.setPages(fields);
mPager.setAdapter(mAdapter);
nextActivity.setVisibility(View.GONE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
As i said, this is JUST AN EXAMPLE, the Fields Class should be changed for your Objects, the ViewPagerAdapter is where you keep the list of pages that you will pass to the next activity. And the static field on the secondactivity is just to be use as a bridge between activities and should not be overuse, thats is why after fetching the data you need to set to NULL to know that you already grabbed the value. Hope it helps.
NOTA: THIS CODE WILL NOT WORK AS IT IS, need more code.
Respondido 27 ago 12, 17:08
Thank you, I am curious, how would you go about implementing what you have described above? - Malo
Thank you very much, it is much more than I expected and will be a very valuable resource! - Malo
your welcome, if this helped you to solve your initial problem, please mark this as an answer for other to see it. have a nice day. - Jorge Aguilar
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas android view android-viewpager pager or haz tu propia pregunta.
What are you calling "main page". Can you give us your exact error ? - Aerilys
I have my activity_main.java and activity_main.xml. I didn't have logcat running at the time of crash, so I am unsure of the error. But from the activity_main.java the user is brought to characterstats.java/characterstats.xml at which point I want the user to be able to swipe to a second page. Sorry I don't have the code to make it any clearer, I have built it up and destroyed it so many times. - Malo
It's difficult to provide you a solution without the code, but for my part I never had any difficulty to implement the viewpager in the way you describe. - Aerilys
Okay, thank you, at least I know its possible. I will start reworking what I have and will post some code as soon as I can. Thank you again. - Malo
Mira este ejemplo github.com/amitkot/view-pager-sample - raman