我已在我的应用中使用13 tab fragment(Category)
设置了ViewPager
。我在TabFragment
中遇到了很多问题。我试图在很多方面解决这个问题。但在我的案例中没有任何作用。所以请帮我解决这个问题。
问题是:
我的代码在这里:
适配器类:
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {
public static String CART_COUNT;
public static String CART_TOTAL;
public static int countCART;
public static int TotalCART;
// static List<Restaurant_Beam> restaurant_prducts;
static List<CartRes> RestaurantList;
Config config;
ArrayList<String> CATEGORYNAME = new ArrayList<>();
ArrayList<String> ITEMNAME = new ArrayList<>();
ArrayList<String> ITEM_PRICE = new ArrayList<>();
ArrayList<String> LIKE_COUNT = new ArrayList<>();
int clickedPos = -1;
String CART_PRODUCT_ID,
CART_PRICE,
CART_PDT_NAME,
CART_QUANTITY,
CART_CAT_ID,
CART_STATUS,
CART_CATEGORY;
CommonUtil commonUtil;
byte[] product_pic;
CartRes Restaurant = new CartRes();
private ImageLoader imageLoader;
private Context context;
public CardAdapter(List<CartRes> cartRestaurant, Context context) {
super();
this.RestaurantList = cartRestaurant;
this.context = context;
System.out.println("Filter Result" + cartRestaurant);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_list_item_layout, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
commonUtil.dbUtil = new DbUtil(context);
commonUtil.dbUtil.open();
return viewHolder;
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
commonUtil.pref = context.getSharedPreferences(CommonUtil.MyPREFERENCES, Context.MODE_PRIVATE);
commonUtil.dbUtil = new DbUtil(context);
commonUtil.dbUtil.open();
System.out.println("Filter Result" + "........1");
Restaurant = RestaurantList.get(position);
Typeface tf = Typeface.createFromAsset(context.getAssets(), "roboto.medium.ttf");
Typeface tf1 = Typeface.createFromAsset(context.getAssets(), "roboto.regular.ttf");
config.IMAGE_URL = "http://ec2-54-251-155-163.ap-southeast-1.compute.amazonaws.com/product_images/devdemopos1.jpg";
imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader();
imageLoader.get(config.IMAGE_URL, ImageLoader.getImageListener(holder.productImage, 0, 0));
//config.IMAGE_URL + restaurant_beam.getIG_IMAGEURL()
// String IMAGE_URL = config.IMAGE_URL + restaurant_beam.getIG_IMAGEURL();
String IMAGE_URL = config.IMAGE_URL;
String ITEM_NAME = Restaurant.JSON_PRODUCT_NAME;
String CATEGORY_NAME = Restaurant.JSON_CATEGORY_NAME;
int CAT_ID = Restaurant.JSON_CATEGORY_ID;
String PRICE = String.valueOf(Restaurant.JSON_SALES_PRICE);
int LIKECOUNT = (Restaurant.JSON_LIKECOUNT);
int DeliveryTime = Integer.parseInt((Restaurant.JSON_DELIVERY_TIME));
int COMMENTCOUNT = 200;
String VOUCHER_ID = Restaurant.JSON_VOUCHER_ID;
String ITEMID = String.valueOf(Restaurant.JSON_PRODUCT_ID);
final String PICTUREID = Restaurant.JSON_IMAGE_ID;
holder.productImage.setImageUrl(IMAGE_URL, imageLoader);
holder.txt_itemName.setText(ITEM_NAME);
holder.txt_itemName.setTypeface(tf);
holder.txt_categoryName.setText(CATEGORY_NAME);
holder.txt_itemName.setTypeface(tf1);
holder.txt_Price.setText(PRICE);
holder.txtFav.setText(String.valueOf(LIKECOUNT));
holder.txtItemID.setText(ITEMID);
holder.comment_count.setText(String.valueOf(COMMENTCOUNT));
holder.txtTime.setText(String.valueOf(DeliveryTime));
holder.txtCategoryID.setText(String.valueOf(CAT_ID));
holder.linearLayout.setVisibility(View.INVISIBLE);
ITEMNAME.add(ITEM_NAME);
CATEGORYNAME.add(CATEGORY_NAME);
ITEM_PRICE.add(PRICE);
LIKE_COUNT.add(String.valueOf(LIKECOUNT));
holder.productImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Config.adapterClicked = true;
SharedPreferences.Editor editor = commonUtil.pref.edit();
editor.putBoolean("FRAGMENT", Config.adapterClicked);
editor.commit();
holder.productImage.buildDrawingCache();
Bitmap bmap = holder.productImage.getDrawingCache();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
product_pic = stream.toByteArray();
Intent i = new Intent(view.getContext(), ProductActivity.class);
i.putExtra("itemName", holder.txt_itemName.getText().toString());
i.putExtra("categoryName", holder.txt_categoryName.getText().toString());
i.putExtra("Price", Double.parseDouble(holder.txt_Price.getText().toString()));
i.putExtra("like", Integer.parseInt(holder.txtFav.getText().toString()));
i.putExtra("itemID", holder.txtItemID.getText().toString());
i.putExtra("CATID", holder.txtCategoryID.getText().toString());
i.putExtra("picture", product_pic);
i.putExtra("pictureId", PICTUREID);
i.putExtra("comment", Integer.parseInt(holder.comment_count.getText().toString()));
view.getContext().startActivity(i);
}
});
if (clickedPos == position) {
holder.linearLayout.setVisibility(View.VISIBLE);
holder.txt_categoryName.setVisibility(View.INVISIBLE);
holder.txt_itemName.setVisibility(View.INVISIBLE);
holder.iv_settings.setVisibility(View.INVISIBLE);
} else {
holder.linearLayout.setVisibility(View.INVISIBLE);
holder.txt_categoryName.setVisibility(View.VISIBLE);
holder.txt_itemName.setVisibility(View.VISIBLE);
holder.iv_settings.setVisibility(View.VISIBLE);
}
holder.iv_settings.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickedPos = position;
notifyDataSetChanged();
}
});
holder.viewDetail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Config.adapterClicked = true;
SharedPreferences.Editor editor = commonUtil.pref.edit();
editor.putBoolean("FRAGMENT", Config.adapterClicked);
editor.commit();
holder.productImage.buildDrawingCache();
Bitmap bmap = holder.productImage.getDrawingCache();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
product_pic = stream.toByteArray();
Intent i = new Intent(view.getContext(), ProductActivity.class);
i.putExtra("itemName", holder.txt_itemName.getText().toString());
i.putExtra("categoryName", holder.txt_categoryName.getText().toString());
i.putExtra("Price", Double.parseDouble(holder.txt_Price.getText().toString()));
i.putExtra("like", Integer.parseInt(holder.txtFav.getText().toString()));
i.putExtra("itemID", holder.txtItemID.getText().toString());
i.putExtra("CATID", holder.txtCategoryID.getText().toString());
i.putExtra("picture", product_pic);
i.putExtra("pictureId", PICTUREID);
i.putExtra("comment", Integer.parseInt(holder.comment_count.getText().toString()));
view.getContext().startActivity(i);
}
});
holder.addToCart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
commonUtil.dbUtil.open();
CART_PDT_NAME = holder.txt_itemName.getText().toString();
CART_CATEGORY = holder.txt_categoryName.getText().toString();
CART_PRICE = holder.txt_Price.getText().toString();
Integer.parseInt(holder.txtFav.getText().toString());
CART_PRODUCT_ID = holder.txtItemID.getText().toString();
CART_CAT_ID = holder.txtCategoryID.getText().toString();
Integer.parseInt(holder.comment_count.getText().toString());
holder.productImage.buildDrawingCache();
Bitmap bmap = holder.productImage.getDrawingCache();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
product_pic = stream.toByteArray();
hadInBasket();
}
private void hadInBasket() {
Cursor CHECK = commonUtil.dbUtil.isInCart(CART_PRODUCT_ID);
if (CHECK != null && CHECK.moveToFirst()) {
Toast.makeText(context, "Already added into the cart", Toast.LENGTH_SHORT).show();
} else {
addtocart();
Toast.makeText(context, "Added into Cart", Toast.LENGTH_SHORT).show();
}
}
private void addtocart() {
if (MainActivity.mCounter != null) {
countCART++;
SharedPreferences.Editor editor = commonUtil.pref.edit();
editor.putString("cartCount", String.valueOf(countCART));
editor.commit();
CART_COUNT = commonUtil.pref.getString("cartCount", "00");
CART_TOTAL = commonUtil.pref.getString("cartTotal", "00");
MainActivity.mCounter.setText(CART_COUNT);
}
CART_QUANTITY = "1";
commonUtil.dbUtil.addtocart(CART_PRODUCT_ID, CART_PDT_NAME, CART_PRICE, CART_QUANTITY, CART_CATEGORY, CART_CAT_ID, CommonUtil.CART_PDT_STATUS, CART_TOTAL, product_pic);
}
});
holder.checkOut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CART_PDT_NAME = holder.txt_itemName.getText().toString();
CART_CATEGORY = holder.txt_categoryName.getText().toString();
CART_PRICE = holder.txt_Price.getText().toString();
Integer.parseInt(holder.txtFav.getText().toString());
CART_PRODUCT_ID = holder.txtItemID.getText().toString();
CART_CAT_ID = holder.txtCategoryID.getText().toString();
holder.productImage.buildDrawingCache();
Bitmap bmap = holder.productImage.getDrawingCache();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
product_pic = stream.toByteArray();
IsItInWishList();
}
private void IsItInWishList() {
Cursor CHECK = commonUtil.dbUtil.isInWishList(CART_PRODUCT_ID);
if (CHECK != null && CHECK.moveToFirst()) {
Toast.makeText(context, "Already in the wishlist", Toast.LENGTH_SHORT).show();
} else {
int priceInt = (int) Math.round(Double.parseDouble(CART_PRICE));
commonUtil.dbUtil.addTowish(CART_PRODUCT_ID, CART_PDT_NAME, CART_CATEGORY, priceInt, product_pic);
Toast.makeText(context, "Added into WishList", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public int getItemCount() {
return RestaurantList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public NetworkImageView productImage;
public TextView txt_categoryName, txtFav, txtItemID, txtCategoryID;
public TextView txt_itemName, txt_Price, comment_count, txtTime;
public CardView cardlist_item;
public ImageView iv_settings;
public LinearLayout linearLayout;
public Button viewDetail, addToCart, checkOut;
public ViewHolder(View itemView) {
super(itemView);
iv_settings = (ImageView) itemView.findViewById(R.id.iv_settings);
cardlist_item = (CardView) itemView.findViewById(R.id.cardlist_item);
txt_itemName = (TextView) itemView.findViewById(R.id.txt_itemName);
txt_Price = (TextView) itemView.findViewById(R.id.txt_Price);
txt_categoryName = (TextView) itemView.findViewById(R.id.txt_categoryName);
productImage = (NetworkImageView) itemView.findViewById(R.id.productImage);
txtFav = (TextView) itemView.findViewById(R.id.txtFav);
txtItemID = (TextView) itemView.findViewById(R.id.txtItemID);
linearLayout = (LinearLayout) itemView.findViewById(R.id.linearTransparent);
viewDetail = (Button) itemView.findViewById(R.id.butViewDetail);
addToCart = (Button) itemView.findViewById(R.id.but_addTCart);
checkOut = (Button) itemView.findViewById(R.id.butCheckOut);
comment_count = (TextView) itemView.findViewById(R.id.comment_count);
txtTime = (TextView) itemView.findViewById(R.id.txtTime);
txtCategoryID = (TextView) itemView.findViewById(R.id.cat_id_hidden);
}
}
}
TabFragment.class:
public class TabFragmentSearch extends Fragment {
public static int pastVisiblesItems, visibleItemCount, totalItemCount;
public static boolean loading = true;
public static SearchView edtSearch;
GridLayoutManager mLayoutManager;
Context context;
RecyclerView recyclerView;
boolean swipeCheck;
Config config;
TextView noData;
CommonUtil commonUtil;
List<CartRes> cartRestaurant;
private List<CartRes> RestaurantDataSet;
private RecyclerView.Adapter adapter;
public static TabFragmentSearch newInstance(int categoryId, String categoryName) {
Bundle args = new Bundle();
args.putInt(CommonUtil.CATEGORY_ID, categoryId);
args.putString(CommonUtil.CATEGORY_NAME, categoryName);
TabFragmentSearch fragment = new TabFragmentSearch();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
config = new Config(getActivity());
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); //SOFT_INPUT_STATE_HIDDEN
CommonUtil.pref = getActivity().getSharedPreferences(CommonUtil.MyPREFERENCES, Context.MODE_PRIVATE);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, container, false);
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
config = new Config(getActivity());
context = getActivity();
CommonUtil.pref = context.getSharedPreferences(CommonUtil.MyPREFERENCES, Context.MODE_PRIVATE);
commonUtil.dbUtil = new DbUtil(context);
commonUtil.dbUtil.open();
commonUtil.dbHelper = new DbHelper(context);
recyclerView = (RecyclerView) view.findViewById(R.id.fragment_list_rv);
edtSearch = (SearchView) view.findViewById(R.id.edtSearch);
noData = (TextView) view.findViewById(R.id.noData);
noData.setVisibility(View.GONE);
recyclerView.setVisibility(View.VISIBLE);
mLayoutManager = new GridLayoutManager(context, 2);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setHasFixedSize(true);
RestaurantDataSet = new ArrayList<>();
swipeCheck = false;
cartRestaurant = new ArrayList<>();
RestaurantDataSet = MainActivity.cartRestaurant;
int BeanCatId = getArguments().getInt(CommonUtil.CATEGORY_ID);
Config.startFilterClicked = false;
SharedPreferences.Editor editor1 = CommonUtil.pref.edit();
editor1.putBoolean("startFilter", Config.startFilterClicked);
editor1.commit();
Cursor curCAT_PDT = commonUtil.dbUtil.getCAT_PDT(String.valueOf(BeanCatId));
if (curCAT_PDT != null && curCAT_PDT.moveToFirst()) {
curCAT_PDT.moveToFirst();
while (curCAT_PDT.isAfterLast() == false) {
cartRestaurant.add(new CartRes(curCAT_PDT.getInt(curCAT_PDT.getColumnIndex(DbHelper.JSON_CATEGORY_ID)), curCAT_PDT.getString(curCAT_PDT.getColumnIndex(DbHelper.JSON_CATEGORY_NAME)),
curCAT_PDT.getInt(curCAT_PDT.getColumnIndex(DbHelper.JSON_PRODUCT_ID)),
curCAT_PDT.getString(curCAT_PDT.getColumnIndex(DbHelper.JSON_PRODUCT_NAME)),
curCAT_PDT.getInt(curCAT_PDT.getColumnIndex(DbHelper.JSON_SALES_PRICE)), curCAT_PDT.getString(curCAT_PDT.getColumnIndex(DbHelper.JSON_IMAGE_ID)),
curCAT_PDT.getString(curCAT_PDT.getColumnIndex(DbHelper.JSON_SHOP_ID)), curCAT_PDT.getString(curCAT_PDT.getColumnIndex(DbHelper.JSON_DELIVERY_TIME)),
curCAT_PDT.getInt(curCAT_PDT.getColumnIndex(DbHelper.JSON_LIKECOUNT)), curCAT_PDT.getString(curCAT_PDT.getColumnIndex(DbHelper.JSON_VOUCHER_ID)),
curCAT_PDT.getString(curCAT_PDT.getColumnIndex(DbHelper.JSON_VOUCHER_OFFER))));
curCAT_PDT.moveToNext();
}
adapter = new CardAdapter(cartRestaurant, context);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if (dy > 0) {
visibleItemCount = mLayoutManager.getChildCount();
totalItemCount = mLayoutManager.getItemCount();
pastVisiblesItems = mLayoutManager.findFirstVisibleItemPosition();
if (loading) {
if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) {
loading = false;
Log.v("...", "Last Item Wow !");
}
}
}
}
});
edtSearch.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
System.out.println("TabFragment Search" + " query" + query);
final List<CartRes> filteredModelList = filter(RestaurantDataSet, query);
if (filteredModelList.size() == 0) {
Toast.makeText(context, "No Search Data Found", Toast.LENGTH_SHORT).show();
} else {
noData.setVisibility(View.GONE);
}
View view = getActivity().getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
System.out.println("TabFragment Search" + "newText " + newText);
final List<CartRes> filteredModelList = filter(RestaurantDataSet, newText);
adapter = new CardAdapter(filteredModelList, context);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
recyclerView.scrollToPosition(0);
return true;
}
});
edtSearch.setOnCloseListener(new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
View view = getActivity().getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
edtSearch.clearFocus();
recyclerView.setVisibility(View.VISIBLE);
noData.setVisibility(View.GONE);
return false;
}
});
noData.setVisibility(View.GONE);
}
private List<CartRes> filter(List<CartRes> restaurantDataSet, String strSearch) {
final List<CartRes> filteredModelList = new ArrayList<>();
for (CartRes restaurant : restaurantDataSet) {
final String text = restaurant.JSON_PRODUCT_NAME.toLowerCase();
if (text.contains(strSearch)) {
filteredModelList.add(restaurant);
}
}
return filteredModelList;
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
menu.findItem(R.id.badge).setVisible(false);
menu.findItem(R.id.home).setVisible(false);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main, menu);
super.onCreateOptionsMenu(menu, inflater);
}
}
标签实例从MainActivity标签ViewPagerAdapter获取数据:
class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return TabFragmentSearch.newInstance(jSonCatId.get(position), jSonTab.get(position)); //newInstance
}
@Override
public int getCount() {
return jSonTab.size();
}
@Override
public CharSequence getPageTitle(int position) {
return jSonTab.get(position);
}
}