所以基本上,我在ViewPager
和TabLayout
中有兩個選項卡,用於在選項卡視圖中存儲兩個片段。事實上,我在recycler view
中使用card view
,而card view
就像Like Button
。基本上,當用戶點擊按鈕時,它應該出現在下一個選項卡上 - 但它不會。只有當用戶離開整個頁面並返回到該頁面並選擇該選項卡時,纔會顯示所選的card view
。我檢查了我的數據庫,它正在做它應該是,但它是不更新的recycler view
。爲什麼當我進入下一個選項卡時,我的recyclerview不會更新?
這是在card view
我的liking
內容代碼:
IN CUSTOM ADAPTER CLASS
holder.likebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Database database = new Database(fragment.getContext());
database.likeContent(userID, contentID, name);
}
});
DATABASE CLASS
public void likeContent(int userID, int contentID, String name){
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(USER_ID, userID);
contentValues.put(CONTENT_ID, contentID);
contentValues.put(NAME, name);
sqLiteDatabase.insert(DB_TABLE_NAME, null, contentValues);
}
BACKGROUND CLASS
public void showFavourites(final ArrayList<Integer> likes){
theURL = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
requestQueue = Volley.newRequestQueue(fragment.getActivity());
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest
(theURL, new Response.Listener<JSONArray>(){
@Override
public void onResponse(JSONArray jsonArray) {
try {
for (int i = 0; i < jsonArray.length(); i++) {
...
adapter.notifyDataSetChanged();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError volleyError) {
}
}
);
requestQueue.add(jsonArrayRequest);
}
SECOND TAB CLASS
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.likes, container, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.rlikes);
recyclerView.setHasFixedSize(true);
list = new ArrayList<UserInfo>();
adapter = new CAdapter(list, SecondTab.this);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(adapter);
conditionNames = new ArrayList<>();
Background = new Background(SecondTab.this, list, adapter);
inputs = new ArrayList<>();
populate();
return rootView;
}
public void populate(){
background.showFavourites(inputs);
}
所以後,我點擊它在database
更新Custom Adapter Class
的like
按鈕,但它不會在第二個選項卡更新recyclerview
。
編輯:我的尋呼機適配器:
public class TabAdapter extends FragmentPagerAdapter{
String tabNames[] = new String[] {"Current", "Likes"};
Context context;
public PodcastGUITabAdapter(FragmentManager fragmentManager, Context context){
super(fragmentManager);
this.context = context;
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new CurrentTab();
case 1:
return new LikesTab();
default:
return null;
}
}
@Override
public int getCount() {
return tabNames.length;
}
@Override
public CharSequence getPageTitle(int position) {
return tabNames[position];
}
}
我缺少什麼?
後尋呼機適配器代碼。很可能你需要在getItem函數中返回一個新的fragment實例。 –
@vipulmittal,我編輯了我的問題以包含'getItem()'函數 –
將與從onCreate填充列表相關的代碼放入onResume。此外,如果這不工作檢查http://stackoverflow.com/questions/10024739/how-to-determine-when-fragment-becomes-visible-in-viewpager –