以下是2个布局的XML代码我需要设置购物车的可见性是空的,当列表的长度为0时立即购买按钮。否则我应该只显示RecyclerView。我已经在片段中编写代码,但是当我们加载我们的片段并且列表的大小不为零时,它也显示你的购物车是空的并且现在购买按钮,在循环器视图加载之后的几分之一秒......但我想要显示只有当我们的购物车中没有任何内容时,这个空文本和立即购买按钮。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.brinhitamobile.krishigaadi.CartFragment"
android:weightSum="1">
<!-- TODO: Update blank fragment layout -->
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler1"
android:layout_width="match_parent"
android:layout_height="490dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearEmpty"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="your cart is empty"
android:textAlignment="center"
android:visibility="gone"
android:textColor="@android:color/black"
android:textSize="20sp"
android:id="@+id/tvEmpty" />
<Button
android:id="@+id/buttonShop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="20dp"
android:background="#32cd32"
android:visibility="gone"
android:text="SHOP NOW"
android:textColor="#000"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="485dp"
android:orientation="horizontal">
<TextView
android:id="@+id/texttotPrice"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#fff"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:text=" "
android:textColor="#000"
android:textSize="20sp"
android:textStyle="bold"
/>
<Button
android:id="@+id/buttonChck"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/button"
android:text="checkout"
android:textColor="#000"
android:visibility="visible"
android:textSize="20sp" />
</LinearLayout>
</FrameLayout>
这是我的java代码 公共类CartFragment扩展了Fragment {
RecyclerView recyclerView;
Button checkout,shopnow;
ArrayList<Cart> list;
LinearLayoutManager linearLayoutManager;
MyAdapter myAdapter;
Mytask mytask;
TextView textViewTotalPrice,tv100;
public boolean checkinternet()
{
ConnectivityManager manager = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getActiveNetworkInfo();
if (info != null && info.isConnected() == true) {
return true;
}
return false;
}
public CartFragment() {
}
@Override
public void onStart() {
super.onStart();
myAdapter.grandTotal(list);
Log.d("price","Onstart" );
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_cart, container, false);
checkout = (Button) v.findViewById(R.id.buttonChck);
tv100= (TextView)v.findViewById(R.id.tvEmpty);
textViewTotalPrice = (TextView) v.findViewById(R.id.texttotPrice);
shopnow = (Button) v.findViewById(R.id.buttonShop);
recyclerView = (RecyclerView) v.findViewById(R.id.recycler1);
linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL,
false);
myAdapter = new MyAdapter();
list = new ArrayList<Cart>();
recyclerView.setAdapter(myAdapter); //adapter --to--> recyclerview
recyclerView.setLayoutManager(linearLayoutManager); //recyclerview--to--> layoutmanager
if (myAdapter.getItemCount()==0) {
Toast.makeText(getContext(),"emptycart",Toast.LENGTH_SHORT).show();
Log.d("size", "onCreateView2: "+list.size());
tv100.setVisibility(View.VISIBLE);
checkout.setVisibility(View.GONE);
textViewTotalPrice.setVisibility(View.GONE);
shopnow.setVisibility(View.VISIBLE);
}
if (checkinternet() == true) {
mytask = new Mytask();
String AddedBy = "gaurav";
} else {
Toast.makeText(getActivity(), "no internet", Toast.LENGTH_SHORT).show();
}
Double x = Store.grandPrice.getGrandprice();
Log.d("Price","totalprice"+x);
checkout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) { }
});
shopnow.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), MainActivity.class);
startActivity(intent);
}
});
return v;
}
public class Mytask extends AsyncTask<String, Void, String> {
URL myurl;
HttpURLConnection con;
InputStream is;
InputStreamReader reader;
BufferedReader br;
String s = null;
StringBuilder sb;
@Override
protected String doInBackground(String... strings) {
try {
myurl = new URL(strings[0]);
con = (HttpURLConnection) myurl.openConnection();
is = con.getInputStream();
reader = new InputStreamReader(is);
br = new BufferedReader(reader);
sb = new StringBuilder();
do {
s = br.readLine();
sb.append(s);
} while (s != null);
return sb.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
Log.d("b37", "doInBackground: malform " + e.getMessage());
} catch (IOException e) {
e.printStackTrace();
Log.d("b37", "doInBackground: io exception " + e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(String s)
{
try {
JSONArray array = new JSONArray(s);
for (int i = 0; i < array.length(); i++) {
JSONObject j = array.getJSONObject(i);
String cartid = j.getString("CartId");
String productid = j.getString("ProductId");
String image1 = j.getString("ProductImagePath");
String title = j.getString("ProductName");
// String subtitle = j.getString("subtitle");
String price = j.getString("Price");
String description = j.getString("ProductDescription");
String quantity = j.getString("Quantity");
String shippingcharges = j.getString("ShippingCharges");
String discount = j.getString("Discount");
String totalprice = j.getString("TotalPrice");
Cart r = new Cart(productid, image1, title, price, description, cartid, quantity, shippingcharges, discount, totalprice);
list.add(r);
cart.setProductid(productid);
cart.setImage1(image1);
cart.setTitle(title);
cart.setPrice(price);
cart.setDescription(description);
cart.setCartid(cartid);
cart.setQuantity(quantity);
cart.setShippingcharges(shippingcharges);
cart.setDiscount(discount);
cart.setTotalprice(totalprice);
myAdapter.notifyDataSetChanged();
}
if (s != null) {
}
} catch (JSONException e)
{
e.printStackTrace();
}
super.onPostExecute(s);
}
}
public class MyAdapter extends RecyclerView.Adapter<CartFragment.MyAdapter.ViewHolder>
{
@Override
public CartFragment.MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
View v = getActivity().getLayoutInflater().inflate(R.layout.cart, parent, false);
CartFragment.MyAdapter.ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Cart k = list.get(position);
holder.tv1.setText(k.getTitle());
holder.tv2.setText(k.getDescription());
holder.tv3.setText("Rs" + Double.parseDouble(k.getPrice()));
holder.tv4.setText(k.getQuantity());
holder.tv5.setText(k.getDiscount() + "% off");
holder.tv7.setText("RS " + k.getTotalprice());
String imagepath = k.getImage1();
Glide.with(CartFragment.this).load(imagepath).into(holder.iv1);
grandTotal(list);
}
@Override
public int getItemCount()
{
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView tv1, tv2, tv3, tv4, tv5, tv7;
public ImageView iv1, iv2, iv3;
public Button b1, b2;
public ImageButton ib1, ib2
public ViewHolder(查看itemView){
super(itemView);
tv1 = (TextView) itemView.findViewById(R.id.tvTitle);
tv7 = (TextView) itemView.findViewById(R.id.tvTotalPrice);
tv2 = (TextView) itemView.findViewById(R.id.tvSubtitle);
tv3 = (TextView) itemView.findViewById(R.id.tvPrice);
tv3.setPaintFlags(tv3.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
tv4 = (TextView) itemView.findViewById(R.id.tvQty);
itemView.findViewById(R.id.tvShippingCharge);
ib1 = (ImageButton) itemView.findViewById(R.id.qty_decrease);
ib2 = (ImageButton) itemView.findViewById(R.id.qty_increase);
b1 = (Button) itemView.findViewById(R.id.btnRemove);
b2 = (Button) itemView.findViewById(R.id.btnWishList);
iv2 = (ImageView) itemView.findViewById(R.id.imageDelete);
iv3 = (ImageView) itemView.findViewById(R.id.imageWish);
iv1 = (ImageView) itemView.findViewById(R.id.ivImage);//FOR DISPLAYING ACTOR NAME
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int pos = getAdapterPosition();
Cart selectedItem = list.get(pos);
String removeCartid = selectedItem.getCartid();
Store.deletecartitem.setDeletecartid(removeCartid);
list.remove(selectedItem.getCartid());
MyAdapter myAdapter = new MyAdapter();
myAdapter.notifyDataSetChanged();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(CartFragment.this).attach(CartFragment.this).commit();
Toast.makeText(CartFragment.this.getActivity(), " Item Removed", Toast.LENGTH_SHORT).show();
new Deletetask().execute();
}
});
ib2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
new Updatequantity().execute();
}
});
ib1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
String _stringVal;
int pos = getAdapterPosition();
String quantity = tv4.getText().toString();
final Cart selectedItem = list.get(pos);
final String productprice = selectedItem.getPrice();
final double productpriced = Double.parseDouble(productprice);
String productid = selectedItem.getProductid();
Store.updatequantityprameter.setPid(productid);
String productname = selectedItem.getTitle();
Store.updatequantityprameter.setPname(productname);
int j = Integer.parseInt(quantity);
Log.d("src", "Decreasing value...");
if (j > 1) {
_stringVal = String.valueOf(j);
tv4.setText(_stringVal);
double subtotalprice = --j * productpriced * (1 / ((double) (j + 1)));
Store.updatequantityprameter.setPprice(String.valueOf(subtotalprice));
Store.updatequantityprameter.setPquantity(String.valueOf(j));
} else
{
double subtotalprice = j * productpriced;
Store.updatequantityprameter.setPprice(String.valueOf(subtotalprice));
}
String imagepath = selectedItem.getImage1();
Store.updatequantityprameter.setPimagepath(imagepath);
String productdescription = selectedItem.getDescription();
Store.updatequantityprameter.setPdescription(productdescription);
String productshippingcharges = selectedItem.getShippingcharges();
Store.updatequantityprameter.setPshippingcharge(productshippingcharges);
String productdiscount = selectedItem.getDiscount();
Store.updatequantityprameter.setPdiscount(productdiscount);
String producttotalprice = selectedItem.getTotalprice();
Store.updatequantityprameter.setPtotalprice(producttotalprice);
MyAdapter myAdapter = new MyAdapter();
recyclerView.refreshDrawableState();
myAdapter.notifyDataSetChanged();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(CartFragment.this).attach(CartFragment.this).commit();
new Updatequantity().execute();
}});
if (myAdapter.getItemCount()!=0) {
Log.d("size", "onCreateView1: "+list.size());
tv100.setVisibility(View.GONE);
checkout.setVisibility(View.VISIBLE);
textViewTotalPrice.setVisibility(View.VISIBLE);
shopnow.setVisibility(View.GONE);
}}}
private void grandTotal(ArrayList<Cart> list) {
int tPrice = 0;
for (int i = 0; i < list.size(); i++) {
String x = list.get(i).getPrice();
double xr = Double.parseDouble(x);
tPrice += xr;
Store.grandPrice.setGrandprice(Double.valueOf(tPrice));
Log.d("Price", "pric" + Store.grandPrice.getGrandprice());
textViewTotalPrice.setText("Total Rs "+Store.grandPrice.getGrandprice());
}}}}
答案 0 :(得分:0)
只需检查购物车是否为空,然后使用setVisibility()
隐藏您的视图答案 1 :(得分:0)
您可以在xml中使用 android:visibility =“gone”来隐藏布局或按钮 在活动或片段 layout.setVisibility(View.GONE); 表示隐藏或 layout.setVisibility(View.VISIBLE); 表示可见,只需为列表计数制作逻辑< / p>