Recycler View
package lodore.com.lodore.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import java.util.ArrayList;
import java.util.List;
import lodore.com.lodore.CheckOutActivity;
import lodore.com.lodore.Pojo.CartDTO;
import lodore.com.lodore.R;
import lodore.com.lodore.adapter.CartAdapter;
public class CartFragment extends Fragment {
EditText editTextMain;
Button btnHome,btnSendGift;
RecyclerView recyclerView;
CartAdapter adapter;
Toolbar toolbar;
public CartFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_cart, container, false);
editTextMain = (EditText) view.findViewById(R.id.edit_main);
recyclerView = (RecyclerView) view.findViewById(R.id.cart_recycler_view);
btnHome = (Button) view.findViewById(R.id.btn_home);
btnSendGift = (Button) view.findViewById(R.id.btn_send_gift);
adapter = new CartAdapter(getActivity(), getData());
recyclerView.setAdapter(adapter);
recyclerView.setHasFixedSize(true);
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
btnHome.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), CheckOutActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
});
btnSendGift.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GiftFragment1 giftFragment = new GiftFragment1();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, giftFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
return view;
}
public static List<CartDTO> getData() {
List<CartDTO> cartDTOList = new ArrayList<>();
String[] title = {"الآسيوية", "الآسيوية"};
String[] price = {"السع", "السعر"};
int[] image = {R.drawable.perfume2, R.drawable.perfume2};
for (int i = 0; i < title.length; i++) {
CartDTO cartDTO = new CartDTO();
cartDTO.titleCart = title[i];
cartDTO.priceCart = price[i];
cartDTO.imageCart = image[i];
cartDTOList.add(cartDTO);
}
return cartDTOList;
}
}
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width=dm.widthPixels;
int height=dm.heightPixels;
int dens=dm.densityDpi;
double wi=(double)width/(double)dens;
double hi=(double)height/(double)dens;
double x = Math.pow(wi,2);
double y = Math.pow(hi,2);
double screenInches = Math.sqrt(x+y);
Log.v("width",""+width);
Log.v("height",""+height);
halfOfScreenWidth=width/2.15;
ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) holder.linearLayout.getLayoutParams();
params.width = (int)halfOfScreenWidth;
holder.linearLayout.setLayoutParams(params);
public class CartDTO {
public int imageCart;
public String titleCart;
public String priceCart;
}
package lodore.com.lodore.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.Collections;
import java.util.List;
import lodore.com.lodore.Pojo.CartDTO;
import lodore.com.lodore.R;
/**
* Created by w7 on 21-Mar-17.
*/
public class CartAdapter extends RecyclerView.Adapter<CartAdapter.MyViewHolder> {
LayoutInflater inflater;
List<CartDTO> cartDTOList = Collections.emptyList();
public CartAdapter(Context context, List<CartDTO> cartDTOList) {
inflater = LayoutInflater.from(context);
this.cartDTOList = cartDTOList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_cart_main,parent,false);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
CartDTO cartDTO = cartDTOList.get(position);
holder.imageItem.setImageResource(cartDTO.imageCart);
holder.textTitleCart.setText(cartDTO.titleCart);
holder.textPriceCart.setText(cartDTO.priceCart);
}
@Override
public int getItemCount() {
return cartDTOList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView textTitleCart,textQuansCart,textPriceCart;
ImageView imageItem;
CheckBox checkSelected;
public MyViewHolder(View itemView) {
super(itemView);
textTitleCart = (TextView) itemView.findViewById(R.id.row_text_title_cart);
textQuansCart = (TextView) itemView.findViewById(R.id.row_text_quan_cart);
textPriceCart = (TextView) itemView.findViewById(R.id.row_text_price_cart);
imageItem = (ImageView) itemView.findViewById(R.id.row_image_cart);
checkSelected = (CheckBox) itemView.findViewById(R.id.check_cart);
}
}
}