私は2つのタブを持っています。別のフラグメントボタンをクリックしたときのRecyclerViewの更新方法
A - RecycleFragment /////// B - DiaryFragment
私はDiaryFragmentでたときにボタンのクリックRecycleFragmentを更新します。
public class RecycleFragment extends Fragment {
static DBHelper dbHelper;
public List<Contact> items = new ArrayList<Contact>();
MyAdapter adapter = new MyAdapter(items);
@Override
public void onCreate(Bundle savedInstanceState) {
dbHelper = new DBHelper(getActivity());
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.recyle_fragment, container, false);
RecyclerView rv = (RecyclerView) rootView.findViewById(R.id.rv_recycler_view);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
// MyAdapter adapter = new MyAdapter(items);
rv.setLayoutManager(llm);
rv.setAdapter(adapter);
adapter.addTestData();
adapter.notifyDataSetChanged();
return rv;
}
}
====================
public class MyAdapter extends RecyclerView.Adapter<DataManager> {
static List<Contact> items = new ArrayList<Contact>();
public MyAdapter(List<Contact> items){
this.items = items;
}
@Override
public DataManager onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.entries, viewGroup, false);
return new DataManager(v);
}
@Override
public void onBindViewHolder(DataManager holder, int position) {
Contact item = items.get(position);
holder.mTitle.setText(item.get1(Contact.Field.TITLE));
holder.mDate.setText(item.get1(Contact.Field.DATE));
holder.mMonth.setText(item.get1(Contact.Field.MONTH));
holder.mTime.setText(item.get1(Contact.Field.TIME));
holder.mDay.setText(item.get1(Contact.Field.DAY));
holder.mContent.setText(item.get1(Contact.Field.CONTENT));
holder.mMood.setImageResource(item.get2(Contact.Field.MOOD));
holder.mWeather.setImageResource(item.get2(Contact.Field.WEATHER));
}
@Override
public int getItemCount() {
return items.size();
}
public void setItem(List<Contact> items){
this.items = items;
notifyItemChanged (0, null);
}
public void addTestData(){
Cursor cursor = RecycleFragment.dbHelper.D_getAllData();
if(cursor != null) {
if (cursor.moveToFirst()) {
do{
add(new Contact(cursor.getString(0),
cursor.getString(1),
cursor.getString(2),
cursor.getString(3),
cursor.getString(5),
cursor.getString(5),
cursor.getString(6),
cursor.getInt(7),
cursor.getInt(8)));
}while (cursor.moveToNext());
}
}
}
public void add(Contact data){
items.add(data);
setItem(items);
}
}
================ボタンのクリックイベントで========================
DiaryFragment
case R.id.IV_diary_save:
if(EDT_diary_title.getText().toString().trim() != null && EDT_diary_title.getText().toString().trim().length() != 0){
if(EDT_diary_content.getText().toString().trim() != null && EDT_diary_content.getText().toString().trim().length() != 0){
dbHelper.D_insertData(EDT_diary_title.getText().toString(), EDT_diary_content.getText().toString(),
TV_diary_month.getText().toString(), TV_diary_date.getText().toString(), TV_diary_day.getText().toString(),
TV_diary_time.getText().toString(),
diaryInfoHelper.getMoodResourceId(SP_diary_weather.getSelectedItemPosition()),
diaryInfoHelper.getWeatherResourceId(SP_diary_mood.getSelectedItemPosition())
);
myAdapter.add(new Contact
(String.valueOf(dbHelper.getRow()),EDT_diary_title.getText().toString(), EDT_diary_content.getText().toString(),
TV_diary_month.getText().toString(), TV_diary_date.getText().toString(), TV_diary_day.getText().toString(),
TV_diary_time.getText().toString(),
diaryInfoHelper.getMoodResourceId(SP_diary_weather.getSelectedItemPosition()),
diaryInfoHelper.getWeatherResourceId(SP_diary_mood.getSelectedItemPosition())
));
Toast.makeText(v.getContext(), "Success to Insert", Toast.LENGTH_SHORT).show();
((TabActivity)getActivity()).getViewPager().setCurrentItem(0);
EDT_diary_title.setText("");
EDT_diary_content.setText("");
}else {
Toast.makeText(v.getContext(), "Content is Empty ", Toast.LENGTH_SHORT).show();
EDT_diary_content.setText("");
}
}else {
Toast.makeText(v.getContext(), "Title is Empty ", Toast.LENGTH_SHORT).show();
EDT_diary_title.setText("");
}
break;
========== =========================
DiaryFragmentでボタンクリックは、
すぐに新しい項目の挿入データを変更するタブRecycleFragmentと
RecycleFragmentのコンテンツを移動します。
今、ボタンをクリックするとDiaryFragment リサイクルフラグメントは変更されません。
変更リサイクルフラグメント。私はRecycleFragmentを終了し、RecycleFragmentを再開します。
は、次の手順を実行し
あなたのクラスのMainFragmentActivityのDiaryFragmentインテントコードをボタンでクリックしてみてください。 – Omi
これがあなたに役立つことを願っています。https://stackoverflow.com/questions/44284245/tab-fragment-button-click-send-data-to-more-than-one-tabfragment/44302221#44302221 –