如何在两个布局之间设置可见性



这是2个布局的XML代码,我需要设置您的购物车的可见性是空的,并且在列表的长度为0时现在购买。否则,我应该仅显示RecyClerview。我已经编写了片段的代码,但是当我们加载片段和列表的大小不是零时,它也显示您的购物车是空的,现在在Recycler View正在加载后,现在以秒为单位购买按钮...但是我想显示这个空文字和现在购买按钮,只有在我们的购物车中没有任何东西时。

<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代码公共班级碎片扩展了碎片{

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

公共视图持有人(查看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());
            }}}}

只需检查购物车是否为空,然后使用setVisibility((

使您的视图不可见

您可以在XML Android中使用:可见性=" gone" 以隐藏布局或按钮在活动或片段 layout.setvisibal(view.gone(; for Hide或 layout.setvisibility; setVisibility(view.visible(; 可见,只需使您的逻辑以列表计数

最新更新