我的主要活动是处理导航抽屉中的点击。然后它调用一个不同的Java类来完成剩下的工作。保持主要代码分开。这是我的主要活动代码。
public class MainActivity extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListener {
ViewFlipper flip_it; //flip tussen screens = flip_it
DrawerLayout drawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,
drawer, toolbar, R.string.navigation_drawer_open,
R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
flip_it = findViewById(R.id.vf);
flip_it.animate();
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
然后是OnNavigationClickListeners,意图运行其他Java类
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_rec) {
flip_it.setDisplayedChild(2); //Recordings
drawer.closeDrawer(GravityCompat.START);
Intent intent=new Intent(MainActivity.this,list_recordings.class);
startActivity(intent);
}
现在在我的Java类中,我正在调用我想要更改主要活动上调用的布局上的内容,例如更改按钮文本。
TextView player_tema = findViewById(R.id.player_tema); //todo declare bo
player_tema.setText(recording.getTema());
Button playpause = findViewById(R.id.pauseplay);
playpause.setText(R.string.pause_button);
我将如何处理这个问题,因为布局在前台仍然有效。我只是需要有关如何与当前正在运行的类的当前布局进行通信的帮助。
由于
编辑:
public RecyclerView getRecyclerView(){
return recyclerView;
}
在我的主要活动中添加了getter,但现在我的RecyclerView抛出了NullPointerException。请看下面我在第二节课中如何使用我的吸气剂。
MainActivity main = new MainActivity();
main.getRecyclerView().addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
main.getRecyclerView().addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), main.getRecyclerView(), new ClickListener() {
完整代码:
Main_Activity:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
ViewFlipper flip_it;
DrawerLayout drawer;
RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
flip_it = findViewById(R.id.vf);
flip_it.animate();
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
}
if (flip_it.getDisplayedChild() == 3) {
flip_it.setDisplayedChild(2);
}
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_rec) {
flip_it.setDisplayedChild(2); //Recordings
recyclerView = findViewById(R.id.recycler_view);
drawer.closeDrawer(GravityCompat.START);
list_recordings lr = new list_recordings();
lr.maak_die_lys_vir_al_die_recordings();
}
if (id == R.id.nav_qanda) {
flip_it.setDisplayedChild(1);
drawer.closeDrawer(GravityCompat.START);
}
if (id == R.id.nav_news){
flip_it.setDisplayedChild(4);
drawer.closeDrawer(GravityCompat.START);
}
return true;
}
//some getters and setters
public RecyclerView getRecyclerView(){
return recyclerView;
}
}
和第二类的完整代码:
Second Class:
public class list_recordings extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
MyRecyclerViewAdapter mAdapter;
private List<Recording> recordings = new ArrayList<>();
ViewFlipper flip_it;
public void maak_die_lys_vir_al_die_recordings() {
mAdapter = new MyRecyclerViewAdapter(recordings);//todo declare bo
MainActivity main = new MainActivity();
main.getRecyclerView().addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL)); //
main.getRecyclerView().addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), main.getRecyclerView(), new ClickListener() {
@Override
public void onClick(View view, int position) {
Recording recording = recordings.get(position);
if (recording.getCaID() == 0) {
toasty(); //tasty om te load more todo load more code skryf
} else {
flip_it = findViewById(R.id.vf);
flip_it.setDisplayedChild(3);
TextView player_tema = findViewById(R.id.player_tema); /
player_tema.setText(recording.getTema());
Button playpause = findViewById(R.id.pauseplay);
playpause.setText(R.string.pause_button);
}
}
public void onLongClick(View view, int position) {
}
}));
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
main.getRecyclerView().setLayoutManager(mLayoutManager);
main.getRecyclerView().setAdapter(mAdapter);
recordings.clear();
add_item("Ds.Braam Smith", "Ons is hier Vandag", 2, 4, "1992.11.21");
Collections.reverse(recordings);
add_item("n/a", "0", 0, 0, "0");
}
public void toasty() {
Toast.makeText(this, "Loading more...", Toast.LENGTH_LONG).show();
}
public void add_item(String dominie, String tema, int CaID, int preeknommer, String date) {
Recording recording = new Recording(dominie, tema, CaID, preeknommer, date);
recordings.add(recording);
mAdapter.notifyDataSetChanged();
}
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
return true;
}
}
答案 0 :(得分:-1)
一个简单的解决方案是您只需在MainActivity.class中定义您的视图
即可static TextView textView
static RecyclerView recyclerview
这样您就可以像MainActivity.textView或MainActivity.recyclerview
那样访问它但是使用静态变量会导致内存泄漏。
所以你可以尝试另一种方法。 您可以为视图创建getter setter。
只需在MainActivity中定义
即可public getTextView(){
return this.textView
}
public getRecyclerView(){
return this.recyclerview
}
你的第二堂课后来:
MainActivity mainActivity = new MainActivity();
TextView textView = mainActivity.getTextView();