Reputation: 1
I am developping a simple "liste" application. A liste is made of Elements (name, price, date, and some other data) I have one, two, or more different listes, which are all based with Elements. In my main_activity I have some buttons to import, export data or add Element, modify color of the list, etc .. And also a spinner to choose another list, or create a new one. In the middle I have the content of one list (Elements) in a scrollable fragment
I would like to switch between my different lists on scrolling horizontally on the list view.
I have tried with androidx.viewpager and androidx.viewpager2, but this is very hard for me and not working. My need was very simple, and I am surprised not to find a simple solution
Here is some part of my code, scrolling between listes is not working
public class Element {
public double montant;
public String dateAchat;
public String objet;
public String magasin;
public String dateRecue;
}
public class Liste {
public String name;
public int colorText;
public int colorBack;
}
public class Variables {
//static ArrayList<Element> elements = new ArrayList<>();
static ArrayList<Liste> listes = new ArrayList<>();
static int numListeEnCours = -1;
}
public class MainActivity extends AppCompatActivity {
public MainActivity() {
super(R.layout.activity_main);
}
@Override
public void onCreate(Bundle savedInstanceState) {
Log.i(TAG,"--------------debut oncreate");
super.onCreate(savedInstanceState);
appContext = this;
myView = new View(this);
Variables.listes = Fic.loadFileListes(appContext);
int numPos = Variables.numListeEnCours;
setContentView(R.layout.activity_main);
viewPager = findViewById(R.id.viewpager);
adapter = new FragmentPagerAdapter(new FragmentActivity());
viewPager.setAdapter(adapter);
initSpinner(numPos);
}
public class FragmentPagerAdapter extends FragmentStateAdapter {
private static String TAG = "dudu adapter";
public FragmentPagerAdapter(@NonNull FragmentActivity fragmentActivity) {
super(fragmentActivity);
}
//@Override
public Fragment getItem(int i) {
Log.i(TAG,"GetItem appelé");
Fragment fragment = new FragmentListe();
return fragment;
}
@NonNull
@Override
public Fragment createFragment(int position) {
return null;
}
@Override
public int getItemCount() {
return Variables.listes.size()-1;
}
}
I am working on fragments and pageadapter since 2 days, but I don't understand how it work, this seem very complicated for my simple need
adapter fragments FragmentStateAdapter
Upvotes: 0
Views: 14