【问题标题】:Dynamic amount of navigation tabs with ListView in them动态数量的导航选项卡,其中包含 ListView
【发布时间】:2019-10-10 13:40:19
【问题描述】:

我想提高我的代码效率,这就是我试图简化事情的原因(请随时在 KOTLIN 中帮助我)

这是关于一个餐厅预订系统,我有 2 个大房间,每个房间都有一定数量的桌子(=我的配置中的硬编码数字)。 我正在尝试为每个表获取一个选项卡,其中每个选项卡都包含一个 ListView,它通过 SQLite DB 获取他的信息。

我目前正在做的是,我为每个房间设置了 2 个单独的登录页面,并且为每张桌子编写了一个标签。这当然不是这样做的方法,但是,是的,这就是我在这里寻求您帮助的原因:

主页使用 SectionsPageAdapter 进行片段处理,如您所见,我已经实现了包含“Room 1”或“Room 2”的“currentLocation”字符串。

  private SectionsPageAdapter mSectionsPageAdapter;
    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_landing_one);

        Intent mIntent = getIntent();
        Bundle extras = mIntent.getExtras();
        final String currentLocation = extras.getString("currentLocation");


        mSectionsPageAdapter = new SectionsPageAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter
        mViewPager = (ViewPager) findViewById(R.id.container);
        setupViewPager(mViewPager);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);

    }



    private void setupViewPager (ViewPager viewPager) {
        SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());


        if(currentLocation.equals(config.roomone) {
        // for loop?
        adapter.addFragment(new table_new_main(),"INFO");
        adapter.addFragment(new table_one(),"TABLE 1"); 
        adapter.addFragment(new table_two(),"TABLE 2");
        adapter.addFragment(new table_three(),"TABLE 3");
} else { ... }


        viewPager.setAdapter(adapter);

    }
}

问题 1

if 循环完成了他的工作,现在我如何根据给定的表数设置动态的 Fragment 数量?使用 for 循环?

现在是片段(这是我卡住的棘手部分):

公共类 table_one 扩展片段 {

private ListView roomlist;
ListAdapter JSON_List;
ArrayList<String> Array_dates;
ArrayList<String> Array_table,Array_dayid,Array_location;
String currentLocation;



@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.table, container, false);


    roomlist = (ListView) view.findViewById(R.id.list_table);


    String id = config.ID_table3; // I hardcoded each table number, how can I pass that through my landing page to get it here?
    AppDatabase db = Room.databaseBuilder(getContext(),
            AppDatabase.class, config.DB_TABLES).allowMainThreadQueries().build();
    Array_dates = new ArrayList<String>();
    Array_table = new ArrayList<String>();
    Array_dayid = new ArrayList<String>();
    Array_location = new ArrayList<>();
    for(int i=0;i<value.size();i++) {
        Array_dates.add(value.get(i).getDaysDate());
        Array_table.add(value.get(i).getDaysPRX());
        Array_dayid.add(value.get(i).getDayid());
        Array_location.add(value.get(i).getLocation_id());
    }


    JSON_List = new RoomListAdapter(getContext(),id,Array_dates,null,null,Array_table,null,Array_dayid,Array_location,currentLocation);

    roomlist.setAdapter(JSON_List);
    return view;



}

问题 2

所以我需要传递给每个片段:currentLocation(房间 1/2)和它自己的 table_id,以了解从我的数据库中获取数据的位置。 我该怎么做?

【问题讨论】:

  • 你应该考虑使用 recyclerview 而不是列表视图
  • 嗯,你认为这对我解决这个问题有帮助吗?我的意思是,这如何使我更容易获得解决方案:)?
  • 你的问题对我来说有点宽泛,所以我只是快速查看了一下,看看发生了什么,总的来说,我建议人们从列表视图切换到回收站视图,因为它是谷歌出于各种原因推荐:) 我会看看我现在是否可以就如何让这个更好地为你提供一些一般性建议
  • 好的,更新了我的答案,试一试:D

标签: java android android-fragments kotlin fragment


【解决方案1】:

这绝不是对您问题的全面回答,但您绝对可以对此进行优化:

 private void setupViewPager (ViewPager viewPager) {
        SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());


        if(currentLocation.equals(config.roomone) {
        // for loop?
        adapter.addFragment(new table_new_main(),"INFO");
        adapter.addFragment(new table_one(),"TABLE 1"); 
        adapter.addFragment(new table_two(),"TABLE 2");
        adapter.addFragment(new table_three(),"TABLE 3");
    } else { ... }


        viewPager.setAdapter(adapter);

    }

有一个片段数组,大致如下:

    ArrayList<Fragment> listOfTables = new ArrayList<>();

    listOfTables.add(new example1()); 
    listOfTables.add(new example2()); 
    ...

所以在这里,您可以提前将所有表格片段填充到一个列表中,然后根据您的配置(您的if 声明)

您可以循环浏览现有的片段列表,然后根据需要将它们添加到适配器:

    for (int i = 0; i < SomeValueHere; i++) { //you might even try reading this value from your config
        yourAdapter.add(listOfFragment.get(i));
    }

对于您的第二个问题,您是说您想将一些值传递给片段。

嗯,你总是可以尝试这样的:

    for (int i = 0; i < SomeValueHere; i++) { //you should try reading this value from your config if can , perhaps
     Fragment table = listOfFragment.get(i);

     Bundle tableInfo = new Bundle();//add a bundle to the fragment with some extra details
     tableInfo.putInt("room_config", SomeIndicatorHereToShowWhichConfigIsChosen);
     tableInfo.putInt("table_id", i);

     table.setArguments(tableInfo);
        yourAdapter.add(table);
    }

然后,从您的片段中,您可以执行以下操作:

       @Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    Bundle extras = getArguments();

    if (extras == null) {
   //maybe throw an exception, as this should never happen
    } else {
      Integer roomConfig = extras.getInt("room_config");
      Integer roomId = extras.getInt("room_id");
    }

}

【讨论】:

  • 随时@LosKayos,乐于提供帮助:D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-22
  • 1970-01-01
  • 2019-07-20
  • 1970-01-01
相关资源
最近更新 更多