gpt4 book ai didi

java - 在不同的 fragment 中实现两个 RecyclerViews,但在同一个 Activity 中

转载 作者:行者123 更新时间:2023-11-30 01:53:19 24 4
gpt4 key购买 nike

我在 MainActivity.java 中有一个有效的 RecyclerView 和常规事件的 fragment “概述”,但我想在单独的 fragment (“提醒”)中实现第二个。在设置第一个 fragment 的 MainActivity 的 OnCreate 中,我面临无法使用 setContentView 两次的问题。

这是我的 MainActivity.java:

public class MainActivity extends AppCompatActivity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {

/**
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;

/**
* Used to store the last screen title. For use in {@link #restoreActionBar()}.
*/
private CharSequence mTitle;

private RecyclerView mRecyclerView;
private RecyclerView.LayoutManager mLayoutManager;
OverviewAdapter mAdapter;

private RecyclerView reminderRecyclerView;
private RecyclerView.LayoutManager reminderLayoutManager;
RemindersAdapter reminderAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();

// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));

mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);


mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);

events();
mAdapter = new OverviewAdapter(events);
mRecyclerView.setAdapter(mAdapter);

}

@Override
public void onNavigationDrawerItemSelected(int element) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
Fragment fragment = new Fragment();
element++;
// int frame = R.id.container;

String position = "Overview";
switch(element) {

case 1:
position = "Overview";
mTitle = getString(R.string.title_section1);
// frame = R.id.container;
break;

case 2:
position = "Reminders";
mTitle = getString(R.string.title_section2);
fragment = new Reminders();

setContentView(R.layout.reminder_recycler);
// REMINDERS RECYCLERVIEW SETUP BELOW
setContentView(R.layout.reminder_recycler);
reminderRecyclerView = (RecyclerView) findViewById(R.id.my_reminder_recycler_view);

// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
reminderRecyclerView.setHasFixedSize(true);

reminderEvents();
// use a linear layout manager
reminderLayoutManager = new LinearLayoutManager(this);
reminderRecyclerView.setLayoutManager(reminderLayoutManager);


reminderAdapter = new RemindersAdapter(reminderEventsList);
reminderRecyclerView.setAdapter(reminderAdapter);

break;

case 3:
position = "Categories";
mTitle = getString(R.string.title_section3);
fragment = new Categories();
// frame = R.id.categories;
break;

case 4:
position = "Smart Suggestions";
mTitle = getString(R.string.title_section4);
fragment = new SmartSuggestions();
// frame = R.id.smart_suggestions;
break;

}
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();

}
}

我在 Reminders 类中遇到空指针异常,如下所示:

public class Reminders extends Fragment {

private RecyclerView reminderRecyclerView;
private RecyclerView.LayoutManager reminderLayoutManager;
RemindersAdapter reminderAdapter;
Context context;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.reminders, container, false);

// Inflate the layout for this fragment
return rootView;



}

public List<ReminderEvents> reminderEventsList = new ArrayList<>();
public void reminderEvents() {

reminderEventsList.add(new ReminderEvents("Example reminder", "Wed 12 July"));
reminderEventsList.add(new ReminderEvents("Example reminder1", "Wed 12 July"));
reminderEventsList.add(new ReminderEvents("Example reminder2", "Wed 12 July"));
reminderEventsList.add(new ReminderEvents("Example reminder3", "Wed 12 July"));

}

}

最佳答案

setContentView() 只应在您的 Activity 中调用一次。

在 switch 语句中,情况 2 应该看起来更像这样

case 2:
position = "Reminders";
mTitle = getString(R.string.title_section2);
fragment = new Reminders();

break;

然后在您的 fragment 的 onCreateView 中,进行充气

reminderRecyclerView = (RecyclerView) findViewById(R.id.my_reminder_recycler_view);

// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
reminderRecyclerView.setHasFixedSize(true);

reminderEvents();
// use a linear layout manager
reminderLayoutManager = new LinearLayoutManager(this);
reminderRecyclerView.setLayoutManager(reminderLayoutManager);


reminderAdapter = new RemindersAdapter(reminderEventsList);
reminderRecyclerView.setAdapter(reminderAdapter);

我希望这对您有所帮助。

关于java - 在不同的 fragment 中实现两个 RecyclerViews,但在同一个 Activity 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32676015/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com