I want to show a popup menu for each item of a RecyclerView when a button is clicked.
My Adapter:
public class SearchResultRecyclerViewAdapter extends RecyclerView.Adapter<SearchResultRecyclerViewAdapter.ViewHolder> {
public SearchResultRecyclerViewAdapter(ArrayList<BookModel> bookModels, Context context) {
this.bookModels = bookModels;
this.context = context;
}
ArrayList<BookModel> bookModels;
Context context;
#NonNull
#NotNull
#Override
public ViewHolder onCreateViewHolder(#NonNull #NotNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_book,parent,false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Log.d("aa", bookModels.get(position).getThumbnailLink());
Glide.with(context)
.asBitmap()
.load(bookModels.get(position).getThumbnailLink())
.into(holder.coverImage);
holder.name.setText(bookModels.get(position).getName());
holder.author.setText(bookModels.get(position).getAuthor());
holder.category.setText(bookModels.get(position).getCategories().toString());
holder.maturity.setText(bookModels.get(position).getMaturityRating());
holder.desc.setText(bookModels.get(position).getDesc());
holder.option.setOnClickListener(new View.OnClickListener() { ///////////// here
#Override
public void onClick(View v) {
//show popup
Log.d("ItemOption","Clicked");
}
});
if (position+1 == bookModels.size()){
holder.divider.setVisibility(View.GONE);
}
}
#Override
public int getItemCount() {
return bookModels.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
RoundedImageView coverImage;
TextView name;
TextView author;
TextView desc;
TextView category;
TextView maturity;
ImageView divider;
Button option;
RelativeLayout root;
public ViewHolder(#NonNull #NotNull View itemView) {
super(itemView);
coverImage = itemView.findViewById(R.id.cover_book_item_imageview);
name = itemView.findViewById(R.id.name_book_item_textview);
author = itemView.findViewById(R.id.author_book_item_textview);
desc = itemView.findViewById(R.id.desc_book_item_textview);
category = itemView.findViewById(R.id.category_book_item_textview);
maturity = itemView.findViewById(R.id.maturity_book_item_textview);
divider = itemView.findViewById(R.id.divider_book_item_imageview);
option = itemView.findViewById(R.id.option_book_item_button);
root = itemView.findViewById(R.id.root);
}
}
}
My item Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:id="#+id/cover_book_item_cardview"
android:layout_width="#dimen/_81sdp"
android:layout_height="#dimen/_124sdp"
android:layout_margin="#dimen/_15sdp"
app:cardCornerRadius="#dimen/_8sdp"
android:elevation="#dimen/_5sdp">
<com.makeramen.roundedimageview.RoundedImageView
android:id="#+id/cover_book_item_imageview"
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:src="#drawable/testcover"
android:scaleType="fitXY"
app:riv_corner_radius="#dimen/_5sdp"/>
</androidx.cardview.widget.CardView>
<TextView
android:id="#+id/name_book_item_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="name"
android:layout_marginTop="#dimen/_20sdp"
android:layout_alignStart="#id/cover_book_item_cardview"
android:layout_marginStart="#dimen/_90sdp"
android:fontFamily="#font/yanonekaffeesatz_medium"
android:layout_alignLeft="#id/cover_book_item_cardview"
android:layout_marginLeft="#dimen/_90sdp"
android:textSize="#dimen/_18sdp"
android:textColor="#color/light_black"
/>
<TextView
android:id="#+id/author_book_item_textview"
android:textSize="#dimen/_13sdp"
android:fontFamily="#font/yanonekaffeesatz_medium"
android:layout_marginTop="#dimen/_1sdp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/name_book_item_textview"
android:text="author"
android:layout_alignLeft="#id/cover_book_item_cardview"
android:layout_marginLeft="#dimen/_90sdp"
android:layout_alignStart="#id/cover_book_item_cardview"
android:layout_marginStart="#dimen/_90sdp"/>
<TextView
android:id="#+id/desc_book_item_textview"
android:layout_width="#dimen/_190sdp"
android:layout_height="#dimen/_40sdp"
android:layout_below="#id/name_book_item_textview"
android:width="#dimen/_60sdp"
android:layout_alignLeft="#id/cover_book_item_cardview"
android:layout_marginLeft="#dimen/_90sdp"
android:layout_alignStart="#id/cover_book_item_cardview"
android:layout_marginStart="#dimen/_90sdp"
android:layout_marginTop="#dimen/_30sdp"
android:textColor="#color/light_black"
android:text="desc"
android:fontFamily="#font/yanonekaffeesatz_medium"/>
<LinearLayout
android:id="#+id/holder_book_item_linearlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#id/name_book_item_textview"
android:layout_alignLeft="#id/cover_book_item_cardview"
android:layout_marginLeft="#dimen/_90sdp"
android:layout_alignStart="#id/cover_book_item_cardview"
android:layout_marginStart="#dimen/_90sdp">
<TextView
android:id="#+id/category_book_item_textview"
android:textColor="#color/white"
android:fontFamily="#font/yanonekaffeesatz_medium"
android:background="#drawable/rounded_rectangle_90"
android:backgroundTint="#color/yellow"
android:padding="#dimen/_3sdp"
android:layout_marginTop="#dimen/_72sdp"
android:text="category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/maturity_book_item_textview"
android:textColor="#color/white"
android:fontFamily="#font/yanonekaffeesatz_medium"
android:background="#drawable/rounded_rectangle_90"
android:backgroundTint="#color/light_black"
android:padding="#dimen/_3sdp"
android:layout_marginTop="#dimen/_72sdp"
android:text="maturity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/_5sdp"/>
</LinearLayout>
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/option_book_item_button"
android:layout_width="#dimen/_20sdp"
android:layout_height="#dimen/_20sdp"
android:background="#drawable/ic_more"
android:backgroundTint="#color/light_black"
android:layout_alignLeft="#id/cover_book_item_cardview"
android:layout_marginLeft="#dimen/_260sdp"
android:layout_alignStart="#id/cover_book_item_cardview"
android:layout_marginStart="#dimen/_260sdp"
android:layout_marginTop="#dimen/_23sdp"/>
<ImageView
android:id="#+id/divider_book_item_imageview"
android:layout_width="#dimen/_200sdp"
android:layout_height="1dp"
android:background="#DFDFDF"
android:layout_below="#id/holder_book_item_linearlayout"
android:layout_marginTop="#dimen/_15sdp"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
Initializing RecyclerView:
recyclerView = findViewById(R.id.search_result_recyclerview);
recyclerView.scheduleLayoutAnimation();
recyclerView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("CheckRecyclerView","clicked");
}
});
searchResultRecyclerViewAdapter = new SearchResultRecyclerViewAdapter(bookModels,SearchActivity.this);
recyclerView.setAdapter(searchResultRecyclerViewAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(SearchActivity.this));
}
XML Layout of Activity:
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawerlayout"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:layout_width="match_parent">
<RelativeLayout
android:layout_width="wrap_content"
android:gravity="center"
android:animateLayoutChanges="true"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--
Views
-->
<androidx.recyclerview.widget.RecyclerView
android:layout_centerHorizontal="true"
android:layout_below="#id/appbar_cardview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/search_result_recyclerview"
android:layoutAnimation="#anim/search_result_recyclerview_anim"
android:visibility="gone"/> <!-- will become visible later-->
<!--
Views
-->
</RelativeLayout>
<com.google.android.material.navigation.NavigationView
app:itemTextColor="#color/white"
app:itemIconTint="#color/white"
android:background="#color/black"
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header"
app:menu="#menu/menu_main" />
</androidx.drawerlayout.widget.DrawerLayout>
when the item's view is clicked nothing happens. I've done the same exact thing before with no problem I don't know what I'm missing here.
EDIT:
as #anemomylos mentioned this might be a click issue. in this layout there is a lot of Views that will be Gone in order to RecyclerView shows up. RecyclerViews don't register any clicks themselves so I'm not sure if it's being blocked by another view or not, is there any way to know what is blocking the click? I don't have any android:clickable="true" and I made sure that all Views before RecyclerView are Gone and not Invisible
Related
I'm trying to add a ripple effect for the entire row (like we see in listview) in my Recycler View when the row is clicked.
I have tried
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
But still, I couldn't get the desired behavior. Even I have tried
android:foreground="?android:attr/selectableItemBackground"
But still no improvement
My listrow.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true">
<RadioButton
android:id="#+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/heading"
android:text="Title"
android:textSize="20dp"
android:paddingTop="5dp"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_toLeftOf="#+id/subHeading"
android:id="#+id/size"
android:text="Sub Title"
android:paddingTop="3dp"
android:paddingLeft="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
And My layout with Recycler View
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<androidx.recyclerview.widget.RecyclerView
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:paddingTop="#dimen/list_item_spacing_half"
android:paddingBottom="#dimen/list_item_spacing_half"
android:layout_alignParentTop="true"
tools:context=".ListFragment"
tools:listitem="#layout/listrow" />
<Button
android:id="#+id/Save"
android:layout_width="match_parent"
android:text="Save"
android:layout_below="#+id/list"
android:textAllCaps="false"
android:layout_height="wrap_content"/>
</RelativeLayout>
Here is my adapter source code
private class listAdapter extends RecyclerView.Adapter<ViewHolder> {
private final int mItemCount;
final ArrayList<String> mTitles;
final ArrayList<String> msTitles;
ArrayList<RadioButton> radioButtons=new ArrayList<>();
listAdapter(int itemCount, ArrayList<String> titles, ArrayList<String> sTitles) {
mItemCount = itemCount;
mtitles=titles;
msTitles=sTitles;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()), parent);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.sTitle.setTag(position);
holder.radioButton.setTag(position);
holder.title.setTag(position);
holder.title.setText(mTitles.get(position));
holder.sTitle.setText(msTitles.get(position));
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
ItemCheckChanged(v);
}
};
holder.radioButton.setOnClickListener(listener);
holder.sTitle.setOnClickListener(listener);
holder.title.setOnClickListener(listener);
if(radioButtons.size()==0){
holder.radioButton.setChecked(true);
selectedItem=0;
}
radioButtons.add(holder.radioButton);
}
#Override
public int getItemCount() {
return mItemCount;
}
void ItemCheckChanged(View v){
selectedItem=(int)v.getTag();
for(RadioButton radioButton:radioButtons){
radioButton.setChecked(false);
}
radioButtons.get(selectedItem).setChecked(true);
notifyDataSetChanged();
}
public int getSelectedIndex(){
return selectedItem;
}
}
NOTE: I'm using these all things in BottomSheetDialog may it be reason?
You should set background to views that have set a click listener.
If you want to set the listener to the whole row you need to call only:
holder.itemView.setOnclickListener() and remove rest of them.
I am trying to create an activity where the user can create some recipe and input the ingredients. every time the user enters an ingredient, it should be added to a listview. The issue I am having here is that the first input is the only one that is added to the listview not the others.
Here is the layout script:
<?xml version="1.0" encoding="utf-8"?><ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activities.AddActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="226dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"
android:orientation="vertical">
<EditText
android:id="#+id/Name_ET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/recipe_name" />
<EditText
android:id="#+id/ServingsNbr_ET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/number_of_servings" />
<EditText
android:id="#+id/PrepTime_Et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/preparation_time" />
<EditText
android:id="#+id/Calories_ET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/calories" />
</LinearLayout>
<LinearLayout
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="36dp"
android:text="#string/ingredients"
android:textSize="26sp"
android:textAlignment="center"
android:textStyle="bold"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/new_ing"
android:layout_width="0dp"
android:layout_weight="9"
android:layout_height="36dp" />
<ImageView
android:id="#+id/add_ingredient_btn"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="36dp"
android:src="#drawable/round_add_circle_outline_black_36dp"/>
</LinearLayout>
<ListView
android:id="#+id/ingredient_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
</LinearLayout>
And the logic behind it :
public class AddActivity extends AppCompatActivity {
private ListView added_ing;
private IngredientAdapter adapter;
private ArrayList<String> ingredients = new ArrayList<String>();
private EditText new_ing;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
adapter = new IngredientAdapter(AddActivity.this,ingredients);
added_ing = findViewById(R.id.ingredient_list);
added_ing.setAdapter(adapter);
new_ing = findViewById(R.id.new_ing);
ImageView add_ingredien = findViewById(R.id.add_ingredient_btn);
add_ingredien.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ingredients.add(new_ing.getText().toString());
Toast.makeText(AddActivity.this, new_ing.getText().toString(), Toast.LENGTH_SHORT).show();
adapter.notifyDataSetChanged();
}
});
}
public class IngredientAdapter extends ArrayAdapter<String> {
ArrayList<String> mNewIngredients;
public IngredientAdapter(Context context, ArrayList<String> objects) {
super(context, 0, objects);
mNewIngredients = objects;
}
public String getItem(int position) {
return mNewIngredients.get(position).toString();
}
public View getView(int position, View convertView, ViewGroup parent) {
String item = getItem(position);
if(convertView==null)
convertView = LayoutInflater.from(getContext()).inflate(R.layout.ingredient,parent,false);
TextView recipeName= convertView.findViewById(R.id.new_added_ingredient);
recipeName.setText(item);
return convertView;
}
}
}
Try to scroll down your list view, maybe in your item's layout, you set the layout_height = match_parent so you do not see the other inputs.
You need to add android:fillViewport="true" to your ScrollView
Also change all child LinearLayout height to wrap_content instead of match_parent
Try this
<?xml version="1.0" encoding="utf-8"?><ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:fillViewport="true"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="226dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:orientation="vertical">
<EditText
android:id="#+id/Name_ET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/app_name" />
<EditText
android:id="#+id/ServingsNbr_ET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/app_name" />
<EditText
android:id="#+id/PrepTime_Et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/app_name" />
<EditText
android:id="#+id/Calories_ET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/app_name" />
</LinearLayout>
<LinearLayout
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="36dp"
android:text="#string/app_name"
android:textSize="26sp"
android:textAlignment="center"
android:textStyle="bold"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/new_ing"
android:layout_width="0dp"
android:layout_weight="9"
android:layout_height="36dp" />
<ImageView
android:id="#+id/add_ingredient_btn"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="36dp"
android:src="#drawable/ic_launcher_background"/>
</LinearLayout>
<ListView
android:id="#+id/ingredient_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
</LinearLayout>
</ScrollView>
In your implementation of IngredientAdapter, you are using your own ArrayList mNewIngredients for the data instead of the collection in the ArrayAdapter. When you add new data into ingredients, it is not added to the adapter.
To make use of the collection implemented in ArrayAdapter for simplicity. The following is an example, with a view holder so that views can be reused.
public class IngredientAdapter extends ArrayAdapter<String> {
public IngredientAdapter(Context context, ArrayList<String> objects) {
super(context, 0, objects);
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView==null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.ingredient, parent, false);
holder = new ViewHolder();
holder.recipeName = convertView.findViewById(R.id.new_added_ingredient);
convertView.Tag = holder;
} else {
holder = (ViewHolder)convertView.Tag;
}
String item = getItem(position);
holder.recipeName.setText(item);
return convertView;
}
}
public class ViewHolder {
public TextView recipeName;
}
The data in the adapter is now backed by the internal collection. And in the activity, you should add the new data into adapter directly.
public class AddActivity extends AppCompatActivity {
private ListView added_ing;
private IngredientAdapter adapter;
// keep this if you need it for something else
// private ArrayList<String> ingredients = new ArrayList<String>();
private EditText new_ing;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
adapter = new IngredientAdapter(AddActivity.this,ingredients);
added_ing = findViewById(R.id.ingredient_list);
added_ing.setAdapter(adapter);
new_ing = findViewById(R.id.new_ing);
ImageView add_ingredien = findViewById(R.id.add_ingredient_btn);
add_ingredien.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// keep this if you need it for something else
// ingredients.add(new_ing.getText().toString());
// to add adata into the adapter
adapter.Add(new_ing.getText().toString());
Toast.makeText(AddActivity.this, new_ing.getText().toString(), Toast.LENGTH_SHORT).show();
adapter.notifyDataSetChanged();
}
});
}
}
How can I delete the space when I hide a CardView? I doing an app with android and Firebase and I show the information from firebase in cardviews, but when the info isn't correct the cardview must disappear. The point is that the cardview disappear, but still uses a space in the layout, I've tried the answers in
setVisibility(GONE) view becomes invisible but still occupies space But it doesn't work for me.
#Override
protected void onStart() {
super.onStart();
firebaseAuth.addAuthStateListener(firebaseAuthListener);
final FirebaseRecyclerAdapter<CursosDB, MainActivity.CursosPViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<CursosDB, MainActivity.CursosPViewHolder>(
CursosDB.class,
R.layout.design_row_cursos,
MainActivity.CursosPViewHolder.class,
myRef)
{
#Override
protected void populateViewHolder(MainActivity.CursosPViewHolder viewHolder, final CursosDB Cmodel, int position)
{
String estado = Cmodel.getSTATUS().toString();
if (estado.equals("OK")){
viewHolder.setPhotoURL(getApplicationContext(), Cmodel.getURI());
viewHolder.setTitle(Cmodel.getTITLE());
viewHolder.setCiudad(Cmodel.getPLACE());
viewHolder.setLevel(Cmodel.getLEVEL());
viewHolder.setDur(Cmodel.getDURACION());
viewHolder.setPrice(Cmodel.getCOSTO());
}else{
RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) viewHolder.mView.getLayoutParams();
layoutParams.setMargins(0,0,0,0);
viewHolder.mView.setLayoutParams(layoutParams);
viewHolder.mView.setVisibility(View.INVISIBLE);
viewHolder.mView.setVisibility(View.GONE);
//viewHolder.mView.setVisibility(View.GONE);
//Toast.makeText(getApplicationContext(), "Ningún curso aprobado aún", Toast.LENGTH_SHORT).show();
}
viewHolder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Toast.makeText(getApplicationContext(), Cmodel.getUSERID(), Toast.LENGTH_LONG).show();
//Toast.makeText(getApplicationContext(), Cmodel.getTITLE(), Toast.LENGTH_SHORT).show();
//Toast.makeText(UsersList.this, user_key, Toast.LENGTH_LONG).show();
}
});
}
};
mCourses.setAdapter(firebaseRecyclerAdapter);
}
This is my layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/cardview_cursos"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:onClick="positionAction"
android:padding="4dp"
android:paddingBottom="4dp"
android:paddingEnd="4dp"
android:paddingStart="4dp"
android:paddingTop="4dp"
app:cardElevation="4dp">
<LinearLayout android:id="#+id/layout_to_hide"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/Cimg"
android:layout_width="match_parent"
android:layout_height="165dp"
android:scaleType="center"
app:srcCompat="#drawable/knowit_logo" />
<TextView
android:id="#+id/Ctitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/Cimg"
android:layout_marginLeft="14dp"
android:layout_marginStart="14dp"
android:paddingTop="8dp"
android:text="Titulo"
android:textStyle="bold" />
<TextView
android:id="#+id/Clugar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Ctitle"
android:layout_alignStart="#+id/Ctitle"
android:layout_below="#+id/Ctitle"
android:layout_marginTop="10dp"
android:text="Lugar" />
<TextView
android:id="#+id/textView29"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Clugar"
android:layout_alignStart="#+id/Clugar"
android:layout_below="#+id/Clugar"
android:layout_marginTop="10dp"
android:text="$" />
<TextView
android:id="#+id/Cprice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView29"
android:layout_marginBottom="4dp"
android:layout_toEndOf="#+id/textView29"
android:layout_toRightOf="#+id/textView29"
android:paddingLeft="5dp"
android:text="0.0" />
<TextView
android:id="#+id/Cdur"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/Clugar"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="126dp"
android:layout_marginRight="126dp"
android:text="Duración" />
<TextView
android:id="#+id/Cnivel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/Cprice"
android:layout_alignLeft="#+id/Cdur"
android:layout_alignStart="#+id/Cdur"
android:layout_marginBottom="0dp"
android:text="Nivel" />
</RelativeLayout>
</LinearLayout> </android.support.v7.widget.CardView>
This is the mView:
public static class CursosPViewHolder extends RecyclerView.ViewHolder {
View mView;
public CursosPViewHolder(final View itemView) {
super(itemView);
mView = itemView;
}
public void setTitle(String title){
TextView post_title = (TextView)mView.findViewById(R.id.Ctitle);
post_title.setText(title);
}
public void setCiudad (String ciudad){
TextView post_city = (TextView)mView.findViewById(R.id.Clugar);
post_city.setText(ciudad);
}
public void setPhotoURL(Context ctx, String pgotouserurl) {
ImageView post_image =(ImageView)mView.findViewById(R.id.Cimg);
Picasso.with(ctx).load(pgotouserurl).into(post_image);
} }
I hope you can help me.
First the XML code you've posted ils wrong closed,(must finish with a CardView tag)
On addition be sure you're pointing the CardView layout with mView field ans Only use GONE or INVISIBLE,
INVISIBLE change your Panel ( Root view ) structure but GONE don't
I want to develop List like this picture
I had used to RecylerView ItemDecorator for overlap. But it's overlapping without shadow. the screen & decorator code is below
public class OverlapDecoration extends RecyclerView.ItemDecoration {
private final static int vertOverlap = -50;
#Override
public void getItemOffsets (Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
outRect.set(0, 0, 0, vertOverlap);
}
}
card_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:tag="cards main container">
<android.support.v7.widget.CardView
android:id="#+id/card_view"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardBackgroundColor="#color/color_white"
card_view:cardElevation="5dp"
card_view:cardUseCompatPadding="true">
<TextView
android:id="#+id/textViewName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp"
android:text="Android Name"
android:textAppearance="?android:attr/textAppearanceLarge"/>
</android.support.v7.widget.CardView>
</LinearLayout>
try this code
card_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:tag="cards main container">
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:clipToPadding="false"
android:elevation="0dp"
card_view:cardUseCompatPadding="false"
card_view:paddingEnd="0dp"
card_view:paddingStart="0dp">
<TextView
android:id="#+id/textViewName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:layout_marginTop="30dp"
android:gravity="center"
android:text="Android Name"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_above="#id/textViewName"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#drawable/card_shadow" />
</android.support.v7.widget.CardView>
</RelativeLayout>
for card shadow
card_shadow
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="90"
android:endColor="#77111111"
android:startColor="#android:color/transparent" />
</shape>
This is example of your problem solution for static CardView , here you every time decrease your card_view:cardElevationby 2dp or 5dp. But if you done this with recyclerview and want to dynamic CardView then you have to add elevation in your RecyclerView Adapterdynamically. And its decrease with its position increase. Set card elevation holder.cardView.setCardElevation()
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:tag="cards main container">
<android.support.v7.widget.CardView
android:id="#+id/card_view"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="20dp"
card_view:cardUseCompatPadding="false">
<TextView
android:id="#+id/textViewName1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp"
android:text="Android Name"
android:textAppearance="?android:attr/textAppearanceLarge"/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="#+id/card_view2"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="18dp"
card_view:cardUseCompatPadding="false">
<TextView
android:id="#+id/textViewName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp"
android:text="Android Name"
android:textAppearance="?android:attr/textAppearanceLarge"/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="#+id/card_view3"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardUseCompatPadding="false">
<TextView
android:id="#+id/textViewName3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp"
android:text="Android Name"
android:textAppearance="?android:attr/textAppearanceLarge"/>
</android.support.v7.widget.CardView>
</LinearLayout>
Hope it will help you. Thanks.
Try this.
In RecyclerView Adapter- on OnBindViewHolder method add the following code:
CardView cardView = ((MyViewHolder) holder).cardView;
float elevation = cardView.getCardElevation();
float value = elevation-Float.parseFloat((position*0.5)+"");
cardView.setCardElevation(value);
See the output here:
Note: depend on your code modify the above code and use it in right place.
For reference here is my sample adaptor code.
public class CustomAdapter extends RecyclerView.Adapter {
public static final String Name = "Name";
public static final String Author = "Author";
public static final String Description = "Description";
private List<Book> dataSet;
private Context context;
public CustomAdapter(List<Book> data, Context context) {
this.dataSet = data;
this.context = context;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
ImageView profile;
TextView name, author,description;
CardView cardView;
public MyViewHolder(View itemView) {
super(itemView);
profile = (ImageView) itemView.findViewById(R.id.profile_image);
name = (TextView) itemView.findViewById(R.id.name);
author = (TextView) itemView.findViewById(R.id.author);
description = (TextView) itemView.findViewById(R.id.description);
cardView = (CardView) itemView.findViewById(R.id.cardview);
}
}
#Override
public int getItemCount() {
return dataSet.size();
}
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder vh;
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.book_layout_sample, parent, false);
vh = new MyViewHolder(v);
return vh;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof MyViewHolder) {
Book book = dataSet.get(position);
((MyViewHolder) holder).profile.setImageDrawable(getResources().getDrawable(R.drawable.blank_user,this.context.getTheme()));
((MyViewHolder) holder).name.setText(book.title);
((MyViewHolder) holder).author.setText(book.author);
((MyViewHolder) holder).description.setText(book.description);
CardView cardView = ((MyViewHolder) holder).cardView;
float ele = cardView.getCardElevation();
float val = ele-Float.parseFloat((position*0.5)+"");
cardView.setCardElevation(val);
if(position==0)
{
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams)cardView.getLayoutParams();
params.setMargins(0,0,0,0);
cardView.setLayoutParams(params);
}
}
}
}
I'm using 4 cardviews in XML, and they are only loading a picture and a textview each. Not using a RecyclerView or anything dynamic.
My Navigation drawer opens with so much lag and the entire app lags after adding the cardviews. Is there a better way to implement these cardviews?
These are simply nested within a Linearlayout.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/app_bar_main"
android:orientation="vertical"
tools:context="com.example.jesse.apbiologystudyappv2.MainActivity">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view_1_natural_selection"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
card_view:cardCornerRadius="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="96dp"
android:scaleType="centerCrop"
android:src="#drawable/apple_tree" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Natural Selection and Evolution"
android:typeface="normal"
android:layout_centerInParent="true"
android:textAlignment="center"
android:textColor="#color/colorTextIcons"
android:textSize="24dp"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view_2_descent_and_ancestry"
android:layout_gravity="center"
android:layout_marginBottom="16dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="96dp"
android:scaleType="centerCrop"
android:src="#drawable/gravestones" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Descent and Ancestry"
android:typeface="normal"
android:layout_centerInParent="true"
android:textAlignment="center"
android:textColor="#color/colorTextIcons"
android:textSize="24dp"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view_3_speciation"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_marginBottom="16dp"
android:layout_height="wrap_content"
card_view:cardCornerRadius="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="96dp"
android:scaleType="centerCrop"
android:src="#drawable/dinosaur" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Speciation and Extinction"
android:typeface="normal"
android:layout_centerInParent="true"
android:textAlignment="center"
android:textColor="#color/colorTextIcons"
android:textSize="24dp"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view_4_origin_of_life"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
card_view:cardCornerRadius="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="96dp"
android:scaleType="centerCrop"
android:src="#drawable/bacteria" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Origin of Life"
android:typeface="normal"
android:layout_centerInParent="true"
android:textAlignment="center"
android:textColor="#color/colorTextIcons"
android:textSize="24dp"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
You should use RecyclerView to manage memory as well. To do this you need only one CardView.
activity_main.xml :
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/app_bar_main"
android:orientation="vertical"
tools:context="com.example.jesse.apbiologystudyappv2.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/lstActions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="1dp"
android:padding="0dp"/>
</LinearLayout>
cardview_item.xml:
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view_1_natural_selection"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
card_view:cardCornerRadius="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="96dp"
android:scaleType="centerCrop"
android:id="#+id/imgActoun" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txtActoun"
android:typeface="normal"
android:layout_centerInParent="true"
android:textAlignment="center"
android:textColor="#color/colorTextIcons"
android:textSize="24dp"/>
</RelativeLayout>
Create a class ActionItem.java like this:
public class ActionItem
{
private final int mTitleResID;
private final int mIconResID;
public ActionItem(int titleResID, int iconResID)
{
this.mTitleResID = titleResID;
this.mIconResID = iconResID;
}
public int getTitleResID()
{
return mTitleResID;
}
public int getIconResID()
{
return mIconResID;
}
}
onCreateView in MainActivity must be like this :
public View onCreateView(String name, Context context, AttributeSet attrs)
{
List<ActionItem> actionItems = new ArrayList<>();
actionItems.add(new ActionItem(R.string.NaturalSelectionAndEvolution, R.drawable.apple_tree));
actionItems.add(new ActionItem(R.string.DescentAndAncestry, R.drawable.gravestones));
RecyclerView lstActions = (RecyclerView) findViewById(R.id.lstActions);
ActionAdapter actionAdapter = new ActionAdapter(actionItems);
lstActions.setAdapter(actionAdapter);
return super.onCreateView(name, context, attrs);
}
Add ActionAdapter Like this:
public class ActionAdapter extends RecyclerView.Adapter<ActionAdapter.ActionHolder>
{
private final List<ActionItem> mActionItems;
public ActionAdapter(List<ActionItem> actionItems)
{
this.mActionItems = actionItems;
}
#Override
public ActionHolder onCreateViewHolder(ViewGroup viewGroup, int i)
{
LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
View itemView = inflater.inflate(R.layout.cardview_item, viewGroup, false);
return new ActionHolder(itemView);
}
#Override
public void onBindViewHolder(ActionHolder actionHolder, int position)
{
ActionItem actionItem = mActionItems.get(position);
actionHolder.mTxtTitle.setText(actionItem.getTitleResID());
actionHolder.mImgIcon.setImageResource(actionItem.getIconResID());
}
#Override
public int getItemCount()
{
return this.mActionItems.size();
}
public class ActionHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
protected final TextView mTxtTitle;
protected final ImageView mImgIcon;
public ActionHolder(View view)
{
super(view);
mImgIcon = (ImageView) view.findViewById(R.id.imgAction);
mTxtTitle = (TextView) view.findViewById(R.id.txtAction);
}
}
}
I implemented Fred's answer with a few changes. Instead of putting this
public View onCreateView(String name, Context context, AttributeSet attrs){
List<ActionItem> actionItems = new ArrayList<>();
actionItems.add(new ActionItem(R.string.NaturalSelectionAndEvolution, R.drawable.apple_tree));
actionItems.add(new ActionItem(R.string.DescentAndAncestry, R.drawable.gravestones));
RecyclerView lstActions = (RecyclerView) findViewById(R.id.lstActions);
ActionAdapter actionAdapter = new ActionAdapter(actionItems);
lstActions.setAdapter(actionAdapter);
return super.onCreateView(name, context, attrs);
}
in onCreateView, I implemented it all in onCreate of MainActivity and put some declerations at the top of the class:
private RecyclerView.LayoutManager mLayoutManager;
private List<ActionItem> actionItems = new ArrayList<>();
private RecyclerView listOfCards;
private ActionAdapter actionAdapter;
I was still getting a lot of lag, however, and it was due to XML loading the images. So, I used the Glide dependency available on Github instead. In ActionAdapter, I added a context requirement and then changed onBindViewHolder. The context requirement is there to satisfy Glide's method which requires a context.
public class ActionAdapter extends RecyclerView.Adapter<ActionAdapter.ActionHolder> {
private final List<ActionItem> mActionItems;
private Context mContext;
public ActionAdapter(List<ActionItem> actionItems, Context context) {
this.mActionItems = actionItems;
this.mContext = context;
}
#Override
public ActionHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
View itemView = inflater.inflate(R.layout.cardview_item, viewGroup, false);
return new ActionHolder(itemView);
}
#Override
public void onBindViewHolder(ActionHolder actionHolder, int position) {
ActionItem actionItem = mActionItems.get(position);
actionHolder.mTxtTitle.setText(actionItem.getTitleResID());
Glide.with(mContext).load(actionItem.getIconResID()).into(actionHolder.mImgIcon);
}
#Override
public int getItemCount() {
return this.mActionItems.size();
}
public class ActionHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
protected final TextView mTxtTitle;
protected final ImageView mImgIcon;
public ActionHolder(View view) {
super(view);
mImgIcon = (ImageView) view.findViewById(R.id.imgAction);
mTxtTitle = (TextView) view.findViewById(R.id.txtAction);
}
#Override
public void onClick(View v) {
//do something
}
}
}
As you can see, i removed actionHolder.mImgIcon .setText.... from onBindViewHolder and replaced it with the Glide command. This makes the application skip no frames when launching and produce no more occurences of lag when opening the navigation drawer. Turns out the lag was produced by a combination of redundant RelativeLayouts, XML insertion of the picture (rather than the more efficient glide) and not using a RecyclerView.