ViewPager有四个片段。在第一个片段中,我使用RecyclerView显示prudacts列表。在第二部分中,我将数据显示在第一个片段中(例如,我可以删除一些数据或添加,它必须在第一个片段的Recycler视图中显示)。当我刷到第一个片段时,我必须看到变化。为此,我在OnResumeFragment方法中使用notifyDataSetChanged。但这会导致ViewPager滑动的奇怪行为。如果我删除notifyDataSetChanged ViewPager滑动正常工作。我该如何解决这个问题?任何帮助欣赏!
适配器
public class FavoriteAdapter extends RecyclerView.Adapter<FavoriteAdapter.PrudactViewHolder> {
List<PrudactModel> prudactsList;
public Context mContext;
public static EditPlayerAdapterCallback callback;
ArrayList<PrudactBusketModel> busketList = new ArrayList<>();
DBHelper dbHelper;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
public ArrayList<Integer> tableBasket = new ArrayList<Integer>();
public ArrayList<Integer> tableFavorite = new ArrayList<>();
String LOG_TAG = "myLogs";
public FavoriteAdapter(List<PrudactModel> persons, Context context){
this.prudactsList = persons;
this.mContext = context;
}
@Override
public PrudactViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_of_pruduct, parent, false);
PrudactViewHolder pvh = new PrudactViewHolder(v,mContext);
return pvh;
}
@Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
@Override
public void onBindViewHolder(final PrudactViewHolder prudactViewHolder, final int i) {
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
final PrudactModel p = prudactsList.get(i);
prudactViewHolder.catalogName.setText(p.name);
prudactViewHolder.catalogPrice.setText(Integer.toString(p.price)+" тг");
prudactViewHolder.catalogPriceOld.setText(Integer.toString(p.oldPrice)+" тг");
prudactViewHolder.thumbNail.setImageUrl(p.imgId,imageLoader);
//// TODO Cheking PRICE
if (p.oldPrice==0){
prudactViewHolder.catalogPriceOld.setVisibility(View.INVISIBLE);
prudactViewHolder.aksia.setVisibility(View.INVISIBLE);
prudactViewHolder.catalogPrice.setTextColor(ContextCompat.getColor(mContext, R.color.black));
}
else {
prudactViewHolder.catalogPrice.setTextColor(ContextCompat.getColor(mContext, R.color.colorPrimary));
prudactViewHolder.catalogPriceOld.setVisibility(View.VISIBLE);
prudactViewHolder.aksia.setVisibility(View.VISIBLE);
}
//// TODO Cheking DB
tableBasket = getTableBasket();
tableFavorite = getTableFavorite();
if (tableBasket.contains(p.id))
prudactViewHolder.btn_buy.setBackgroundResource(R.drawable.tool);
else
prudactViewHolder.btn_buy.setBackgroundResource(R.drawable.tool1);
if (tableFavorite.contains(p.id))
prudactViewHolder.btn_favorite.setBackgroundResource(R.drawable.favorite_d);
else
prudactViewHolder.btn_favorite.setBackgroundResource(R.drawable.favorite_c);
//// TODO: 02.05.2016 BUY BUTTON CLICK
prudactViewHolder.ll_buy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dbHelper = new DBHelper(mContext);
final ContentValues cv = new ContentValues();
final SQLiteDatabase db = dbHelper.getWritableDatabase();
if (prudactViewHolder.btn_buy.getBackground().getConstantState()== ContextCompat.getDrawable(mContext,R.drawable.tool1).getConstantState()){
updateBuyButton(prudactViewHolder, true);
Log.d(LOG_TAG, "--- Insert in mytable: ---");
cv.put("id_prudact", p.id);
cv.put("name",p.name);
cv.put("id_image",p.imgId);
cv.put("count",1);
cv.put("price",p.price);
cv.put("priceOld",p.oldPrice);
long rowID = db.insert("basket", null, cv);
Log.d(LOG_TAG, "row to basket inserted, ID = " + rowID);
callback.folderClicked(1);
}
else {
updateBuyButton2(prudactViewHolder, true);
callback.folderClicked(-1);
db.delete("basket","id_prudact=?",new String[]{String.valueOf(p.id)});
}
dbHelper.close();
}
});
prudactViewHolder.btn_buy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dbHelper = new DBHelper(mContext);
final ContentValues cv = new ContentValues();
final SQLiteDatabase db = dbHelper.getWritableDatabase();
if (prudactViewHolder.btn_buy.getBackground().getConstantState()== ContextCompat.getDrawable(mContext,R.drawable.tool1).getConstantState()){
updateBuyButton(prudactViewHolder, true);
Log.d(LOG_TAG, "--- Insert in mytable: ---");
cv.put("id_prudact", p.id);
cv.put("name",p.name);
cv.put("id_image",p.imgId);
cv.put("count",1);
cv.put("price",p.price);
cv.put("priceOld",p.oldPrice);
long rowID = db.insert("basket", null, cv);
Log.d(LOG_TAG, "row to basket inserted, ID = " + rowID);
callback.folderClicked(1);
}
else {
updateBuyButton2(prudactViewHolder, true);
callback.folderClicked(-1);
db.delete("basket","id_prudact=?",new String[]{String.valueOf(p.id)});
}
dbHelper.close();
}
});
//// TODO: 02.05.2016 FAVORITE BUTTON CLICK
prudactViewHolder.ll_favorite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dbHelper = new DBHelper(mContext);
final ContentValues cv2 = new ContentValues();
final SQLiteDatabase db = dbHelper.getWritableDatabase();
if (prudactViewHolder.btn_favorite.getBackground().getConstantState()== ContextCompat.getDrawable(mContext,R.drawable.favorite_c).getConstantState()){
cv2.put("id_prudact", p.id);
cv2.put("name",p.name);
cv2.put("id_image",p.imgId);
cv2.put("count",1);
cv2.put("price",p.price);
cv2.put("priceOld",p.oldPrice);
long rowID = db.insert("favorite", null, cv2);
Log.d(LOG_TAG, "row to favorite inserted, ID = " + rowID);
}
else {
updateHeartButton2(prudactViewHolder, true);
db.delete("favorite","id_prudact=?",new String[]{String.valueOf(p.id)});
delete(i);
}
}
});
prudactViewHolder.btn_favorite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dbHelper = new DBHelper(mContext);
final ContentValues cv2 = new ContentValues();
final SQLiteDatabase db = dbHelper.getWritableDatabase();
if (prudactViewHolder.btn_favorite.getBackground().getConstantState()== ContextCompat.getDrawable(mContext,R.drawable.favorite_c).getConstantState()){
cv2.put("id_prudact", p.id);
cv2.put("name",p.name);
cv2.put("id_image",p.imgId);
cv2.put("count",1);
cv2.put("price",p.price);
cv2.put("priceOld",p.oldPrice);
long rowID = db.insert("favorite", null, cv2);
Log.d(LOG_TAG, "row to favorite inserted, ID = " + rowID);
}
else {
updateHeartButton2(prudactViewHolder, true);
db.delete("favorite", "id_prudact=?", new String[]{String.valueOf(p.id)});
delete(i);
}
}
});
//// TODO: 02.05.2016 SHARE BUTTON CLICK
prudactViewHolder.ll_share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String shareBody = "Мне нравится "+p.name+" за "+p.price+" тенге";
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
mContext.startActivity(Intent.createChooser(sharingIntent,"Elaman" ));
}
});
prudactViewHolder.btn_share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String shareBody = "Мне нравится "+p.name+" за "+p.price+" тенге";
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
mContext.startActivity(Intent.createChooser(sharingIntent,"Elaman" ));
}
});
prudactViewHolder.thumbNail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext, ImageActivity.class);
intent.putExtra("image",p.imgId);
mContext.startActivity(intent);
}
});
if (p.oldPrice==0){
prudactViewHolder.catalogPriceOld.setVisibility(View.INVISIBLE);
prudactViewHolder.aksia.setVisibility(View.INVISIBLE);
}
dbHelper.close();
}
private static final AccelerateInterpolator ACCELERATE_INTERPOLATOR = new AccelerateInterpolator();
private static final OvershootInterpolator OVERSHOOT_INTERPOLATOR = new OvershootInterpolator(4);
private void updateHeartButton2(final PrudactViewHolder holder, boolean animated) {
if (animated) {
AnimatorSet animatorSet = new AnimatorSet();
ObjectAnimator rotationAnim = ObjectAnimator.ofFloat(holder.btn_favorite, "rotation", 0f, 360f);
rotationAnim.setDuration(300);
rotationAnim.setInterpolator(ACCELERATE_INTERPOLATOR);
ObjectAnimator bounceAnimX = ObjectAnimator.ofFloat(holder.btn_favorite, "scaleX", 0.2f, 1f);
bounceAnimX.setDuration(300);
bounceAnimX.setInterpolator(OVERSHOOT_INTERPOLATOR);
ObjectAnimator bounceAnimY = ObjectAnimator.ofFloat(holder.btn_favorite, "scaleY", 0.2f, 1f);
bounceAnimY.setDuration(300);
bounceAnimY.setInterpolator(OVERSHOOT_INTERPOLATOR);
bounceAnimY.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
holder.btn_favorite.setBackgroundResource(R.drawable.favorite_c);
}
});
animatorSet.play(rotationAnim);
animatorSet.play(bounceAnimX).with(bounceAnimY).after(rotationAnim);
animatorSet.start();
}
}
private void updateBuyButton(final PrudactViewHolder holder, boolean animated) {
if (animated) {
AnimatorSet animatorSet = new AnimatorSet();
ObjectAnimator rotationAnim = ObjectAnimator.ofFloat(holder.btn_buy, "rotation", 0f, 360f);
rotationAnim.setDuration(300);
rotationAnim.setInterpolator(ACCELERATE_INTERPOLATOR);
ObjectAnimator bounceAnimX = ObjectAnimator.ofFloat(holder.btn_buy, "scaleX", 0.2f, 1f);
bounceAnimX.setDuration(300);
bounceAnimX.setInterpolator(OVERSHOOT_INTERPOLATOR);
ObjectAnimator bounceAnimY = ObjectAnimator.ofFloat(holder.btn_buy, "scaleY", 0.2f, 1f);
bounceAnimY.setDuration(300);
bounceAnimY.setInterpolator(OVERSHOOT_INTERPOLATOR);
bounceAnimY.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
holder.btn_buy.setBackgroundResource(R.drawable.tool);
}
});
animatorSet.play(rotationAnim);
animatorSet.play(bounceAnimX).with(bounceAnimY).after(rotationAnim);
animatorSet.start();
}
}
private void updateBuyButton2(final PrudactViewHolder holder, boolean animated) {
if (animated) {
AnimatorSet animatorSet = new AnimatorSet();
ObjectAnimator rotationAnim = ObjectAnimator.ofFloat(holder.btn_buy, "rotation", 0f, 360f);
rotationAnim.setDuration(300);
rotationAnim.setInterpolator(ACCELERATE_INTERPOLATOR);
ObjectAnimator bounceAnimX = ObjectAnimator.ofFloat(holder.btn_buy, "scaleX", 0.2f, 1f);
bounceAnimX.setDuration(300);
bounceAnimX.setInterpolator(OVERSHOOT_INTERPOLATOR);
ObjectAnimator bounceAnimY = ObjectAnimator.ofFloat(holder.btn_buy, "scaleY", 0.2f, 1f);
bounceAnimY.setDuration(300);
bounceAnimY.setInterpolator(OVERSHOOT_INTERPOLATOR);
bounceAnimY.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
holder.btn_buy.setBackgroundResource(R.drawable.tool1);
}
});
animatorSet.play(rotationAnim);
animatorSet.play(bounceAnimX).with(bounceAnimY).after(rotationAnim);
animatorSet.start();
}
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public int getItemCount() {
return prudactsList.size();
}
public void setCallback(EditPlayerAdapterCallback callback2){
callback = callback2;
}
public static class PrudactViewHolder extends RecyclerView.ViewHolder{
CardView cv;
TextView catalogName,catalogPrice,catalogPriceOld;
NetworkImageView thumbNail;
ImageButton btn_favorite,btn_share, btn_buy;
LinearLayout ll_buy,aksia,ll_share,ll_favorite;
public PrudactViewHolder(View itemView,Context context) {
super(itemView);
cv = (CardView)itemView.findViewById(R.id.cv);
catalogName = (TextView)itemView.findViewById(R.id.name_catalog);
catalogPrice = (TextView)itemView.findViewById(R.id.price_catalog);
catalogPriceOld = (TextView)itemView.findViewById(R.id.old_price_catalog);
btn_buy = (ImageButton) itemView.findViewById(R.id.btn_buy);
btn_share = (ImageButton) itemView.findViewById(R.id.btn_share);
btn_favorite = (ImageButton) itemView.findViewById(R.id.izbrannyi);
thumbNail = (NetworkImageView)itemView.findViewById(R.id.image_catalog);
aksia = (LinearLayout) itemView.findViewById(R.id.aksia);
ll_buy = (LinearLayout) itemView.findViewById(R.id.ll_buy);
ll_share = (LinearLayout) itemView.findViewById(R.id.ll_share);
ll_favorite = (LinearLayout) itemView.findViewById(R.id.ll_favorite);
catalogPriceOld.setPaintFlags(catalogPriceOld.getPaintFlags()| Paint.STRIKE_THRU_TEXT_FLAG);
}
}
ArrayList<Integer> getTableBasket(){
ArrayList<Integer> id_list = new ArrayList<>();
dbHelper = new DBHelper(mContext);
SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor c = db.query("basket", null, null, null, null, null, null);
if (c.moveToFirst()) {
int id_prudactColIndex = c.getColumnIndex("id_prudact");
do {
id_list.add(c.getInt(id_prudactColIndex));
} while (c.moveToNext());
} else
Log.d(LOG_TAG, "In basket 0 rows");
c.close();
return id_list;
}
ArrayList<Integer> getTableFavorite(){
ArrayList<Integer> id_list = new ArrayList<>();
dbHelper = new DBHelper(mContext);
SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor c = db.query("favorite", null, null, null, null, null, null);
if (c.moveToFirst()) {
int id_prudactColIndex = c.getColumnIndex("id_prudact");
do {
id_list.add(c.getInt(id_prudactColIndex));
} while (c.moveToNext());
} else
Log.d(LOG_TAG, "In favorite 0 rows");
c.close();
return id_list;
}
public void delete(int position) { //removes the row
prudactsList.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, prudactsList.size());
}
}
片段
public class C_Fragment extends android.support.v4.app.Fragment implements FragmentLifecycle,EditPlayerAdapterCallback {
PrudactModel prudact = new PrudactModel();
RecyclerView rv;
PrudactAdapter adapter;
MainActivity mainActivity = new MainActivity();
DBHelper dbHelper;
public static BasketListener basketclick;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_c, null);
// mainActivity.setfragmentclick(this);
rv = (RecyclerView) view.findViewById(R.id.rv);
LinearLayoutManager llm = new LinearLayoutManager(rv.getContext());
rv.setLayoutManager(llm);
prudact.initializeData();
adapter = new PrudactAdapter(prudact.prudactList,getActivity());
adapter.setCallback(this);
rv.setAdapter(adapter);
setHasOptionsMenu(true);
return view;
}
@Override
public void onPauseFragment() {
}
@Override
public void onResumeFragment() {
((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("Акции и новинки");
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
MenuItem item = menu.findItem(R.id.ic_menu_basket);
LayerDrawable icon = (LayerDrawable) item.getIcon();
dbHelper = new DBHelper(getContext());
SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor cursor = db.rawQuery("select *from basket ", null);
int cnt = cursor.getCount();
cursor.close();
Utils.setBadgeCount(getActivity(), icon,cnt);
}
void setBasketclick(BasketListener bl){
basketclick = bl;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId()==android.R.id.home){
((MainActivity) getActivity()).setCurrentItem(0,true);
}
if (item.getItemId()==R.id.ic_menu_basket){
((MainActivity) getActivity()).setCurrentItem(3, true);
basketclick.basketClicked();
}
return super.onOptionsItemSelected(item);
}
@Override
public void onResume() {
super.onResume();
getView().setFocusableInTouchMode(true);
getView().requestFocus();
getView().setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) {
backAction();
return true;
}
return false;
}
});
}
public void backAction() {
if (getChildFragmentManager().getBackStackEntryCount() > 0) {
getChildFragmentManager().popBackStack();
}
else {
((MainActivity) getActivity()).setCurrentItem(0, true);
}
}
@Override
public void folderClicked(int position) {
getActivity().invalidateOptionsMenu();
}
}