如何从片段1到片段2调用回收器视图?因此我可以在片段1的recyclerview中添加项目,并从片段1的视图而不是片段2的视图识别recyclerview。
当我单击fab时,onClickListener无法正常工作,并且没有任何动作。
我要做的是,当我单击fragment2中的晶圆厂时,将在片段1的回收站视图中添加一个项目。
这是片段1的JAVA代码
package com.example.admin.test2;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
/**
* A simple {@link Fragment} subclass.
*/
public class MainScreen extends Fragment {
private ArrayList<ExampleItem> mExampleList;
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
public MainScreen() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate (R.layout.fragment_main_screen, container, false);
mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
mExampleList = new ArrayList <>();
// mExampleList.add(new ExampleItem(R.drawable.ic_library_add_black_24dp, "", "", "ADD EXPENSES"));
mRecyclerView = view.findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getContext());
mAdapter = new ExampleAdapter(mExampleList);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
return view;
}
}
这是片段2的JAVA代码
package com.example.admin.test2;
import android.app.DatePickerDialog;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
public class Expense extends Fragment {
public Expense() {
// Required empty public constructor
}
//SPINNER
Spinner sp1;
CustomAdapter adapter;
String[] names = {"Food & Drink", "Shopping", "Transportation", "Home", "Bills & Fees", "Entertainment", "Healthcare", "Education", "Beauty", "Others"};
int[] images = {R.drawable.food, R.drawable.shopping, R.drawable.transportation, R.drawable.home, R.drawable.bills, R.drawable.entertainment, R.drawable.medical, R.drawable.education, R.drawable.beauty, R.drawable.others};
//DATE PICKER
private EditText mDisplayDate;
private DatePickerDialog.OnDateSetListener mDateSetListener;
//FOR THE ADD ITEM
FloatingActionButton fab;
private ArrayList<ExampleItem> mExampleList;
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
EditText amount;
EditText detail;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate (R.layout.fragment_expense, container, false);
//spinner
sp1 = (Spinner)view.findViewById(R.id.customSpinner);
adapter = new CustomAdapter(getActivity(), names, images);
sp1.setAdapter(adapter);
//fab onClick - sending values to the recycler view
fab = (FloatingActionButton) view.findViewById(R.id.fabs);
amount = (EditText) view.findViewById(R.id.textAmountt);
detail = (EditText) view.findViewById(R.id.textDetailss);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String amountValue = amount.getText().toString();
String detailValue = detail.getText().toString();
mExampleList.add(0, new ExampleItem(R.drawable.entertainment, "Entertainment", detailValue, "₱ " + amountValue));
mAdapter.notifyDataSetChanged();
}
});
View view2 = inflater.inflate (R.layout.fragment_main_screen, container, false);
mExampleList = new ArrayList <>();
// mExampleList.add(new ExampleItem(R.drawable.ic_library_add_black_24dp, "", "", "ADD EXPENSES"));
mRecyclerView = view2.findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getContext());
mAdapter = new ExampleAdapter(mExampleList);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
int pos = sp1.getSelectedItemPosition();
//date picker
mDisplayDate = (EditText) view.findViewById(R.id.datePick);
mDisplayDate.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(getActivity(),
android.R.style.Theme_Holo_Light_Dialog_MinWidth,
mDateSetListener,
year, month, day);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
}
});
mDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
//Calendar calendar = Calendar.getInstance();
//String currentDate = DateFormat.getDateInstance().format(calendar.getTime());
month = month + 1;
String date = month + "-" + day + "-" + year;
mDisplayDate.setText(date);
}
};
return view;
}
}
这是活动JAVA
package com.example.admin.test2;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
public class ScreenOne extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener{
BottomNavigationView bottomNavigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_screen);
bottomNavigationView = findViewById (R.id.Navbot);
bottomNavigationView.setOnNavigationItemSelectedListener (this);
bottomNavigationView.setSelectedItemId (R.id.home);
}
Income incomeFragment = new Income ();
Expense expenseFragment = new Expense ();
Savings savingsFragment = new Savings ();
MainScreen mainFragment = new MainScreen ();
Charts chartsFragment = new Charts ();
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId ()){
case R.id.home:
getSupportFragmentManager ().beginTransaction ().setCustomAnimations (R.anim.fade_in, R.anim.fade_out).replace (R.id.replaceLayout, mainFragment).commit ();
updateStatusBarColor("#77dd77");
return true;
case R.id.income:
getSupportFragmentManager ().beginTransaction ().setCustomAnimations (R.anim.fade_in, R.anim.fade_out).replace (R.id.replaceLayout, incomeFragment).commit ();
updateStatusBarColor("#779ecb");
return true;
case R.id.expense:
getSupportFragmentManager ().beginTransaction ().setCustomAnimations (R.anim.fade_in, R.anim.fade_out).replace (R.id.replaceLayout, expenseFragment).commit ();
updateStatusBarColor("#ff6961");
return true;
case R.id.savings:
getSupportFragmentManager ().beginTransaction ().setCustomAnimations (R.anim.fade_in, R.anim.fade_out).replace (R.id.replaceLayout, savingsFragment).commit ();
updateStatusBarColor("#77dd77");
return true;
case R.id.chart:
getSupportFragmentManager ().beginTransaction ().setCustomAnimations (R.anim.fade_in, R.anim.fade_out).replace (R.id.replaceLayout, chartsFragment).commit ();
updateStatusBarColor("#77dd77");
return true;
}
return false;
}
public void updateStatusBarColor(String color){// Color must be in hexadecimal fromat
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.parseColor(color));
}
}
}
答案 0 :(得分:0)
您可以使用EventBus来组织片段/活动之间的通信。
答案 1 :(得分:0)
在应用程序中创建一个接口。
public interface MyInterface {
void updateFragment(Example value);// you can pass your object also in the arguments
}
在“活动”中实现此界面。
public class ScreenOne extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener ,MyInterface {
@Override
public void updateFragmnt(Example value){// if you pass arguments then call the method of fragment which recycle view you have to update
// update your Fragment Recycle view here .
expenseFragment.updateData(value);
}
}
从onclick
添加项目时,在fragment
上执行这些操作。
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String amountValue = amount.getText().toString();
String detailValue = detail.getText().toString();
ExampleItem example = new ExampleItem(R.drawable.entertainment, "Entertainment", detailValue, "₱ " + amountValue);
((getActivity())MyInterface).updateFragmnt(example);
}
});
最终在Expense片段中添加方法...
public class Expense extends Fragment {
public void updateData(Example example){
// add this data in your arraylist
// notify your adapter
// and magic begin
}
}