I'm having an issue trying to create an OnClickListener in my CategoriesAdapter.Java
A short summary of what i'm trying to do: I am trying to build a section of an app that shows the 'Categories' of certain business types i.e. Tech, Finance etc etc
I want to make sure the user is able to click on a category and take them to that desired category of choice basically; "User clicks on 'Finance' User goes to 'Finance' Category.
I'll paste my code here if you can help me that would be great.
CategoriesAdapter.Java
package com.example.tencil.HelperClasses.HomeAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.tencil.R;
import com.example.tencil.User.CategoriesHelperClass;
import java.util.ArrayList;
import static android.content.ContentValues.TAG;
public class CategoriesAdapter extends RecyclerView.Adapter<CategoriesAdapter.ViewHolder> {
ArrayList<CategoriesHelperClass> categories;
private ArrayList<CategoriesHelperClass> mTitle = new ArrayList<>();
private ArrayList<CategoriesHelperClass> mImages = new ArrayList<>();
private android.content.Context mcontext;
public CategoriesAdapter(ArrayList<CategoriesHelperClass> categories) {
this.categories = categories;
this.mTitle = mTitle;
this.mImages = mImages;
this.mcontext = mcontext;
}
//Returns Categories View Dynamically
public ViewHolder onCreateViewHolder( ViewGroup parent, int viewType) {
View view = LayoutInflater.from ( parent.getContext () ).inflate ( R.layout.categories_card_design, parent, false );
ViewHolder holder = new ViewHolder ( view );
return new ViewHolder ( view );
}
public void onBindViewHolder( ViewHolder holder, int position) {
Log.d (TAG, "OnBindViewHolder: Called.");
holder.image.setImageResource ( com.example.tencil.HelperClasses.HomeAdapter.CategoriesHelperClass.getImage () );
holder.title.setText ( com.example.tencil.HelperClasses.HomeAdapter.CategoriesHelperClass.getTitle () );
holder.parentLayout.setOnClickListener ( new View.OnClickListener () {
public void onClick(View v) {
Log.d (TAG, "OnClick: Clicked on" + mImages.get ( position ));
Toast.makeText ( mcontext, (CharSequence) mTitle.get ( position ), Toast.LENGTH_SHORT ).show ();
}
} );
}
public int getItemCount() {
return categories.size ();
}
//HOLDS VIEWS
public static class ViewHolder extends RecyclerView.ViewHolder {
ImageView image;
TextView title;
RelativeLayout parentLayout;
public ViewHolder(View itemView) {
super ( itemView );
//HOOKS
image = itemView.findViewById ( R.id.categories_image );
title = itemView.findViewById ( R.id.categories_title );
parentLayout = itemView.findViewById ( R.id.parentLayout );
}
}
}
UserDashboard.XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:context=".User.UserDashboard">
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#fff"
android:theme="#style/navigationTheme"
app:headerLayout="#layout/menu_header"
app:menu="#menu/main_menu" />
<LinearLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/lightWhite"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#color/colorPrimary"
android:padding="20dp">
<ImageView
android:id="#+id/menu_icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="#drawable/menu_icon" />
</RelativeLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:fontFamily="#font/raleway_bold"
android:paddingTop="8dp"
android:text="#string/tencil"
android:textColor="#color/colorAccent"
android:textSize="28sp" />
</RelativeLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal"
android:paddingTop="20dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/card_1"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="10dp"
android:background="#drawable/card_3"
android:elevation="8dp"
android:onClick="cardClicked1">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_centerInParent="true"
android:src="#drawable/tencilw" />
</RelativeLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="#string/socialmedis"
android:textAlignment="center" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/card_2"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="10dp"
android:background="#drawable/card_1"
android:elevation="8dp"
android:onClick="cardClicked2">>
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_centerInParent="true"
android:src="#drawable/piggy" />
</RelativeLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="#string/finance"
android:textAlignment="center" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/card_3"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="10dp"
android:background="#drawable/card_2"
android:elevation="8dp"
android:onClick="cardClicked3">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:src="#drawable/pws" />
</RelativeLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="#string/tech"
android:textAlignment="center" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/card_4"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="10dp"
android:background="#drawable/card_4"
android:elevation="8dp"
android:onClick="cardClicked4">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_centerInParent="true"
android:src="#drawable/analysisw" />
</RelativeLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="#string/fintech"
android:textAlignment="center" />
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:background="#drawable/banner_background">
<LinearLayout
android:id="#+id/featured_background"
android:layout_width="170dp"
android:layout_height="280dp"
android:layout_margin="10dp"
android:background="#drawable/banner_background"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="50dp"
android:text="#string/featured_companies"
android:textAllCaps="true"
android:textColor="#000"
android:textSize="25dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="#string/featured_companies_description" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/featured_recycler"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginStart="10dp"
android:layout_toEndOf="#+id/featured_background"
android:background="#fff" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="vertical"
android:padding="20dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/raleway_bold"
android:text="#string/categories"
android:textAllCaps="true"
android:textColor="#000"
android:textSize="20sp" />
<TextView
android:id="#+id/view_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:onClick="viewAllClicked"
android:text="#string/view_all" />
</RelativeLayout>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:id="#+id/parentLayout"
android:layout_height="150dp" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
</androidx.drawerlayout.widget.DrawerLayout>
CategoriesHelperClass.java
package com.example.tencil.HelperClasses.HomeAdapter;
public class CategoriesHelperClass {
static int image;
static String title;
//SETTER
public CategoriesHelperClass(int image, String title) {
this.image = image;
this.title = title;
}
//GETTER
public static int getImage() {
return image;
}
public static String getTitle() {
return title;
}
}
CONSOLE ERROR MESSAGE
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.tencil, PID: 5226
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.tencil.HelperClasses.HomeAdapter.CategoriesAdapter.onBindViewHolder(CategoriesAdapter.java:55)
at com.example.tencil.HelperClasses.HomeAdapter.CategoriesAdapter.onBindViewHolder(CategoriesAdapter.java:25)
Any help would be appreciated this has been bugging me for hours.
this is how you would solve this problem from kotlin using callbacks, in java you need to use listeners
in your Activity/Fragment have a function ready to handle the navigation like this
Note that the code below is like a pseudo code and you will have to adjust accordingly
private fun navigateTo(title: String) {
//handle navigation with the title normally here
val intent = Intent(this, Category::class.java)
intent.putExtra("Title", title)
startActivity(intent)
}
in your Activity's onCreate
val categoriesAdapter = CategoriesAdapter({ title ->
navigateTo(title)
})
here's the Adapter and ViewHolder classes
private class CategoriesAdapter(val callBack:(String)-> Unit){
override fun onCreateViewHolder(): CategoriesViewHolder{
return CategoriesViewHolder(callBack)
}
}
private class CategoriesViewHolder(val callBack: (String) -> Unit){
fun bind(){
item.setOnClickListener{
callBack(title)
}
}
}
what this does is when you click a certain item, you will get the title and any detail you want from inside the view holder's bind method which will contain only 1 single item inside at any given time, and that will run the method named callBack and sends the title for it back to the adapter, and the adapter does nothing but call the same method again and sends title for it in the Activity, then your Activity will run navigateTo method and do the action required.
in java, you will create a Listener inside your Activity/Fragment and you will use it directly from your ViewHolder if i remember correctly and this will do the same effect
In java, you must cast each element of view to its class, findViewById() returns member of T class, so you will get normal View instead ImageView, TextView etc.
public static class ViewHolder extends RecyclerView.ViewHolder {
ImageView image;
TextView title;
RelativeLayout parentLayout;
public ViewHolder(View itemView) {
super ( itemView );
//HOOKS
image = (ImageView) itemView.findViewById ( R.id.categories_image );
title = (TextView) itemView.findViewById ( R.id.categories_title );
parentLayout = (RelativeLayout) itemView.findViewById ( R.id.parentLayout );
}
}
In addition, if you want to click whole row, you should use holder.itemView.setOnClickListener(). Calling click listener on RelativeLayout will work only, when you touch a free space in your row view, so it may not work each time.
get data from list and set to view (in onBindViewHolder method)
CategoriesHelperClass item = categories.get(position);
holder.image.setImageResource ( item.getImage () );
holder.title.setText ( item.getTitle () );
holder.itemView.setOnClickListener ( new View.OnClickListener () {
public void onClick(View v) {
Log.d (TAG, "OnClick: Clicked on" + mImages.get ( position ));
Toast.makeText ( mcontext, item.getTitle (), Toast.LENGTH_SHORT ).show ();
}
} );
there is no need for this two list, you can handle image and image with categories list
private ArrayList<CategoriesHelperClass> mTitle = new ArrayList<>();
private ArrayList<CategoriesHelperClass> mImages = new ArrayList<>();
Related
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
Here in my activity_main.xml|
in my android RecyclerView show only half of the screen, I'm not able to find out the mistake. Help me guys. I was used different layout like relativelayout, constraintlayout, linearlayout. but I was getting only half of the screen RecyclerView with size.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.SearchView
android:id="#+id/search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimaryDark"
android:focusable="false"
android:visibility="gone"
app:iconifiedByDefault="false"
app:queryHint="Search Distributors..." />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_below="#+id/search"
android:layout_centerInParent="true"
android:layout_height="match_parent" />
<Button
android:id="#+id/btnGet"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_alignParentBottom="true"
android:text="get" />
</RelativeLayout>
Here I attach my MainActivity.java
package com.example.admin.recyclerview;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.view.View;
import android.widget.Button;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<Model> models;
RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
public SearchView searchView;
private Button button;
private RecyclerViewAdapter recyclerViewAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
searchView = findViewById(R.id.search);
button = findViewById(R.id.btnGet);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
List<Model> models1 = recyclerViewAdapter.getList();
System.out.println("final order list "+new Gson().toJson(models1));
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
if (recyclerViewAdapter != null) {
recyclerViewAdapter.getFilter().filter(newText);
}
return true;
}
});
//recyclerView.setHasFixedSize(true);
models = new ArrayList<>();
models.add(new Model("rajesh"));
models.add(new Model("deva"));
models.add(new Model("merlin"));
models.add(new Model("antony"));
models.add(new Model("giri"));
models.add(new Model("guru"));
System.out.println("get the position " +
models.get(0).getName());
System.out.println("json view "+new Gson().toJson(models));
recyclerViewAdapter = new
RecyclerViewAdapter(getApplicationContext(), models);
recyclerView.setAdapter(recyclerViewAdapter);
}
}
Here i bellow attach my adapter class
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> implements Filterable {
public Context context;
private List<Model> modelList;
private List<Model> myfilterList;
public RecyclerViewAdapter(Context context, List<Model> modelList) {
this.context = context;
this.modelList = modelList;
this.myfilterList = modelList;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
View view = layoutInflater.inflate(R.layout.recyclerview_items, null);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder myViewHolder, int i) {
Model model = myfilterList.get(i);
myViewHolder.llMain.setTag(i);
myViewHolder.textView.setText(model.getName());
}
#Override
public int getItemCount() {
return myfilterList.size();
}
#Override
public Filter getFilter() {
return new Filter() {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
String text = constraint.toString();
if (text.isEmpty()) {
myfilterList = modelList;
} else {
ArrayList<Model> filterable = new ArrayList<>();
for (Model model : modelList) {
System.out.println("check equal " + model.getName()
+ " " + text);
if (model.getName().toLowerCase().contains(text)) {
filterable.add(model);
}
}
myfilterList = filterable;
}
FilterResults filterResults = new FilterResults();
filterResults.values = myfilterList;
return filterResults;
}
#Override
protected void publishResults(CharSequence constraint, FilterResults results) {
myfilterList = (ArrayList<Model>) results.values;
notifyDataSetChanged();
}
};
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView textView;
Button btnAdd, btnMinus;
LinearLayout llMain;
MyViewHolder(#NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.tvName);
btnAdd = itemView.findViewById(R.id.btnAdd);
btnMinus = itemView.findViewById(R.id.btnMinus);
llMain = itemView.findViewById(R.id.llMain);
btnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int pos = (Integer) llMain.getTag();
System.out.println("linear layout position details
"+pos);
myfilterList.add(pos+1, new Model("dummy"));
notifyItemChanged(pos+1);
notifyDataSetChanged();
}
});
}
}
public List<Model> getList()
{
return myfilterList;
}
}
Here I bellow attach my item xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/llMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/tvName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textSize="16sp"
android:layout_margin="5dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/btnAdd"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Add Items"/>
<Button
android:id="#+id/btnMinus"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Add Items"/>
</LinearLayout>
</LinearLayout>
I don't have privileges to add a comment, hence posting it here. Sorry in advance.
Your question is not quite clear, do you want your RecyclerView to occupy the whole screen(i.e. parent). If that's the case, With LinearLayout I guess if you add an attribute android:layout_weight in your RecyclerView will solve the problem.
Here's what the discription for the attribute on developer.android site:
LinearLayout also supports assigning a weight to individual children with the android:layout_weight attribute. This attribute assigns an "importance" value to a view in terms of how much space it should occupy on the screen. A larger weight value allows it to expand to fill any remaining space in the parent view. Child views can specify a weight value, and then any remaining space in the view group is assigned to children in the proportion of their declared weight. Default weight is zero.
Hope this link helps https://developer.android.com/guide/topics/ui/layout/linear
Also there's this question already asked by me : What is the connection between setVisibility and layout_weight in Linear Layout
Correct me if I'm wrong.
Try changing the RecyclerView element inside your activity_main.xml file with the following, remove center_in_parent and change android:layout_height="wrap_content"
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.SearchView
android:id="#+id/search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimaryDark"
android:focusable="false"
android:visibility="gone"
app:iconifiedByDefault="false"
app:queryHint="Search Distributors..." />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_below="#+id/search"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btnGet"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_alignParentBottom="true"
android:text="get" />
</RelativeLayout>
Edit 1 - Using ConstraintLayout, since after the RecyclerView you'd also like to display the button, you will need the ConstraintLayout inside the NestedScrollView
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.SearchView
android:id="#+id/search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
android:background="#color/colorPrimaryDark"
android:focusable="false"
app:iconifiedByDefault="false"
app:queryHint="Search Distributors..." />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
app:layout_constraintTop_toBottomOf="#+id/search"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btnGet"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_alignParentBottom="true"
android:text="get" />
</android.support.constraint.ConstraintLayout>
</android.support.v4.widget.NestedScrollView>
In your adapter where you are inflating the item in onCreateViewHolder, use this code block:
View rootView = LayoutInflater.from(context).inflate(R.layout.itemLayout, null, false);
RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rootView.setLayoutParams(lp);
return new RecyclerViewHolder(rootView);
items.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/llMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/tvName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textSize="16sp"
android:text="fdfsfsdfsd"
android:layout_margin="5dp"/>
<Button
android:id="#+id/btnAdd"
android:layout_width="wrap_content"
android:layout_below="#+id/tvName"
android:layout_height="wrap_content"
android:text="Add Items"/>
<Button
android:layout_below="#+id/tvName"
android:id="#+id/btnMinus"
android:layout_alignParentEnd="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Items"/>
</RelativeLayout>
activity_main.xml
<android.support.v7.widget.SearchView
android:id="#+id/search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimaryDark"
android:focusable="false"
android:visibility="visible"
app:iconifiedByDefault="false"
app:queryHint="Search Distributors..." />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/search" />
<Button
android:id="#+id/btnGet"
android:visibility="visible"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_alignParentBottom="true"
android:text="get" />
</android.support.constraint.ConstraintLayout>
package com.example.android.popularmovies;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.ViewHolder> {
private ArrayList<String> mTrailers;
private Context mContext;
// Provide a suitable constructor (depends on the kind of dataset)
public MovieAdapter(Context context, ArrayList<String> trailers) {
mTrailers = trailers;
mContext = context;
}
// Create new views (invoked by the layout manager)
#Override
public MovieAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
LinearLayout v = (LinearLayout)LayoutInflater.from(parent.getContext())
.inflate(R.layout.trailer_view, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
public void watchYoutubeVideo(String id) {
Intent appIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:" + id));
Intent webIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.youtube.com/watch?v=" + id));
try {
mContext.startActivity(appIntent);
} catch (ActivityNotFoundException ex) {
mContext.startActivity(webIntent);
}
}
// Replace the contents of a view (invoked by the layout manager)
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
holder.mImageView.setImageResource(R.drawable.ic_play_arrow_black_24dp);
holder.mTextView.setText("Trailer " + (position + 1));
holder.mTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
watchYoutubeVideo(mTrailers.get(holder.getAdapterPosition()));
}
});
}
// Return the size of your dataset (invoked by the layout manager)
#Override
public int getItemCount() {
return mTrailers.size();
}
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView mTextView;
public ImageView mImageView;
public ViewHolder(View v) {
super(v);
mImageView = v.findViewById(R.id.play_button_image_view);
mTextView = v.findViewById(R.id.movie_text_view);
}
}
}
I want my UI to be like this
With my code however the app looks like this:
Here is my xml resource:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/play_button_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:maxHeight="10dp"
android:maxWidth="10dp"
android:scaleType="centerInside"
android:src="#drawable/ic_play_arrow_black_24dp"
/>
<TextView
android:id="#+id/movie_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:gravity="center_horizontal"
android:padding="10dp"
android:textSize="16sp" />
</LinearLayout>
Is there a way to implement this without using different viewholders? Can it be done with a single ViewHolder?
I have seen ways to implement different View types with a RecyclerView, but I would prefer to keep my code as simple as possible.
change the height of LinearLayot to "wrap_content". and add gravity as "center_vertical"
change you item's layout like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="#+id/play_button_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:maxHeight="10dp"
android:maxWidth="10dp"
android:scaleType="centerInside"
android:src="#drawable/ic_play_arrow_black_24dp"
/>
<TextView
android:id="#+id/movie_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:gravity="center_horizontal"
android:padding="10dp"
android:textSize="16sp"/>
</LinearLayout>
Try android:layout_height="wrap_content", instead of match_parent in LinearLayout.
1) You need to change the height of the layout: android:layout_height="wrap_content"
2) For the ImageView set:
android:layout_width="10dp"
android:layout_height="10dp"
(very small)
You can also use ConstraintLayout like this
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginLeft="32dp"
android:layout_marginTop="32dp"
android:layout_marginBottom="32dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="#drawable/ic_play_arrow_black_24dp" />
<TextView
android:id="#+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="Trailer 1"
app:layout_constraintBottom_toBottomOf="#+id/imageView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/imageView"
app:layout_constraintTop_toTopOf="#+id/imageView" />
</androidx.constraintlayout.widget.ConstraintLayout>
That being said the easiest solution would be simply using "wrap_content" on your parent layout.
try this
<?xml version="1.0" encoding="utf-8"?><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="#+id/play_button_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:src="#drawable/ic_play_arrow_black_24dp" />
<TextView
android:id="#+id/movie_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Trailer 1"
android:layout_margin="5dp"
android:padding="10dp"
android:textSize="16sp" />
</LinearLayout>
Output
Just check out main activity .xml
what i am doing is displaying recycler view beneath a button and edit text ..
the value from edit text is taken made into a card but the problem is the button is not working i dont know why plz help
I am new to Coordinator views so plz pardon me if i doing some noob mistake
AdapterEx.java
import android.support.design.widget.Snackbar;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
public class AdapterEx extends RecyclerView.Adapter<AdapterEx.ViewHolder>{
private List<String> nos;
public AdapterEx(List<String> nos){
this.nos = nos;
}
class ViewHolder extends RecyclerView.ViewHolder{
public TextView number;
public ViewHolder(View itemView) {
super(itemView);
number =
(TextView)itemView.findViewById(R.id.nos);
itemView.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
int position = getAdapterPosition();
Snackbar.make(v, "Click detected on item " + position,
Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.card, viewGroup, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
viewHolder.number.setText(nos.get(i));
}
#Override
public int getItemCount() {
return nos.size();
}
}
initializing recycler view
recycler = (RecyclerView) findViewById(R.id.rv);
LayoutManager manager = new LinearLayoutManager(this);
recycler.setLayoutManager(manager);
nos = new ArrayList<>();
nos.add("sadasd");
nos.add("sdada");
adapter = new AdapterEx(nos);
recycler.setAdapter(adapter);
recycler.setHasFixedSize(true);
recycler.setItemAnimator(new DefaultItemAnimator());
main activity . xml
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.dev99.mathbuddy.prime"
android:id="#+id/prime"
android:background="#drawable/prime">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:inputType="number|numberDecimal|numberSigned"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FACTORISE IT!"
android:id="#+id/button5"
android:onClick="checker"
android:nestedScrollingEnabled="false"
android:layout_below="#+id/editText"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/editText2"
android:layout_below="#+id/button5"
android:textAlignment="center"
android:textSize="30dp"
android:textColor="#000000"
android:layout_alignParentStart="false"
android:layout_alignParentLeft="false" />
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/rv">
</android.support.v7.widget.RecyclerView>
</android.support.design.widget.CoordinatorLayout>
card view.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/card_view"
android:layout_gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_margin="10dp"
android:layout_height="wrap_content"
card_view:cardCornerRadius="4dp"
card_view:cardBackgroundColor="#color/bluemat"
card_view:cardElevation="10dp"
card_view:contentPadding="4dp"
android:clickable="true">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/nos"
android:textSize="40dp"
android:textStyle="bold"
android:textAlignment="center"
android:layout_gravity="center"
android:textColor="#color/red"/>
</android.support.v7.widget.CardView>
The problem is RecyclerView with wrap_content.
For some reason, a RecyclerView does not display the last item or the last few items well. The most common problem I experienced is that the last item got cut off.
I'm using recycler view to show items with multiple views. whenever I launch the app some white space is being showed. which is exactly equal to my list items.
I provided the screenshot please go through it.
I think there is no problem with my code.
following is my code
recyclerview
<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".activities.GroupView">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view_group_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" >
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
adapter code
import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
import java.util.Objects;
import pdfshare.hemanthreddy.com.pdfshare.R;
import pdfshare.hemanthreddy.com.pdfshare.pojo.PdfItemHolder;
import pdfshare.hemanthreddy.com.pdfshare.pojo.RequestPojo;
public class GroupContentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
final int PDF = 0,REQUEST = 1;
List<Object> list;
OnClickListener onClickListener;
Context context;
public GroupContentAdapter(List<Object> list, OnClickListener onClickListener, Context context) {
this.list = list;
this.onClickListener = onClickListener;
this.context = context;
}
public class PdfHolder extends RecyclerView.ViewHolder {
TextView pdfName,pdfDescription,Helped,uploadedBy;
Button downloadPdf,viewPdf;
View container;
CardView cardView;
public PdfHolder(View itemView) {
super(itemView);
container = itemView;
uploadedBy = (TextView) itemView.findViewById(R.id.pdf_info_uplodedby);
pdfName = (TextView) itemView.findViewById(R.id.pdf_name);
pdfDescription = (TextView) itemView.findViewById(R.id.pdf_description);
Helped = (TextView) itemView.findViewById(R.id.helped);
downloadPdf = (Button) itemView.findViewById(R.id.download_pdf);
viewPdf = (Button) itemView.findViewById(R.id.view_pdf);
cardView = (CardView) itemView.findViewById(R.id.card_view_pdf_row);
}
}
public class RequestHolder extends RecyclerView.ViewHolder {
View container;
TextView name,request;
public RequestHolder(View itemView) {
super(itemView);
container = itemView;
name = (TextView) itemView.findViewById(R.id.requested_name);
request = (TextView) itemView.findViewById(R.id.request);
}
}
public interface OnClickListener{
public void OnItemClick(View view,int position);
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder;
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
switch (viewType) {
case PDF:
View v1 = inflater.inflate(R.layout.pdf_row_view,parent,false);
viewHolder = new PdfHolder(v1);
break;
case REQUEST:
View v2 = inflater.inflate(R.layout.group_request_row_view,parent,false);
viewHolder = new RequestHolder(v2);
break;
default:
View v3 = inflater.inflate(R.layout.pdf_row_view,parent,false);
viewHolder = new PdfHolder(v3);
}
return viewHolder;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
if(holder.getItemViewType() == PDF)
{
PdfItemHolder pdfItemHolder = (PdfItemHolder) list.get(position);
PdfHolder pdfHolder = (PdfHolder) holder;
if(!TextUtils.isEmpty(pdfItemHolder.getPdfName())) {
pdfHolder.pdfName.setText(pdfItemHolder.getPdfName());
pdfHolder.pdfDescription.setText(pdfItemHolder.getPdfDescription());
pdfHolder.Helped.setText(pdfItemHolder.getHelped());
pdfHolder.uploadedBy.setText(pdfItemHolder.getUplodedBy());
pdfHolder.downloadPdf.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "downloading pdf bitch", Toast.LENGTH_LONG).show();
}
});
pdfHolder.container.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onClickListener.OnItemClick(v, position);
}
});
}
else
{
Toast.makeText(context,"empty",Toast.LENGTH_LONG).show();
Log.e("empty","empty");
pdfHolder.container.setVisibility(View.GONE);
pdfHolder.pdfName.setVisibility(View.GONE);
pdfHolder.pdfDescription.setVisibility(View.GONE);
pdfHolder.Helped.setVisibility(View.GONE);
pdfHolder.downloadPdf.setVisibility(View.GONE);
pdfHolder.uploadedBy.setVisibility(View.GONE);
pdfHolder.viewPdf.setVisibility(View.GONE);
}
}
else
{
RequestHolder requestHolder = (RequestHolder) holder;
RequestPojo requestPojo = (RequestPojo) list.get(position);
requestHolder.name.setText(requestPojo.getName());
requestHolder.request.setText(requestPojo.getRequest());
}
}
#Override
public int getItemCount() {
return list.size();
}
#Override
public int getItemViewType(int position)
{
if(list.get(position) instanceof PdfItemHolder)
return PDF;
else if(list.get(position) instanceof RequestPojo)
return REQUEST
;
return -1;
}
}
also I checked stack overflow to remove spaces and I added some code to adapter to check wheather the data is empty or not, it didn't worked well.
following Is code of row views`group_row_view code
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Requested By : "
android:id="#+id/textView7" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Name"
android:id="#+id/requested_name"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/textView7"
android:layout_toEndOf="#+id/textView7" />
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#c0c0c0"
android:id="#+id/view1"
android:layout_below="#+id/textView7"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/request"
android:lines="2"
android:layout_below="#+id/view1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
code of Pdf_row_view
</LinearLayout>`
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:card_view="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.CardView
android:id="#+id/card_view_pdf_row"
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
card_view:cardCornerRadius="3dp"
card_view:contentPadding="2dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="250dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="pdfName"
android:id="#+id/pdf_name"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="25dp"
android:lines="1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="pdf descriptionl"
android:id="#+id/pdf_description"
android:layout_below="#+id/pdf_name"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:lines="2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View Pdf"
android:id="#+id/view_pdf"
android:layout_alignTop="#+id/download_pdf"
android:layout_toRightOf="#+id/pdf_info_uplodedby"
android:layout_toEndOf="#+id/pdf_info_uplodedby" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Download Pdf"
android:id="#+id/download_pdf"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_below="#+id/pdf_description" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="helped"
android:id="#+id/helped"
android:textSize="15dp"
android:layout_alignBottom="#+id/view_pdf"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#c0c0c0"
android:layout_alignTop="#+id/view_pdf"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/view" />
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#c0c0c0"
android:id="#+id/view1"
android:layout_below="#+id/view_pdf"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Upladed by : "
android:id="#+id/pdf_info_uplodedby"
android:layout_below="#+id/view_pdf"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
please help me.. thank you
In all of your list item layout files:
pdf_row_view.xml, group_request_row_view.xml, pdf_row_view.xml
set layout_height="wrap_content" of their root layout
Remove the match_parent and set wrap_content as the layout_height instead in the root of your layout of your list items.
For example your pdf_row_view.xml should look like this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:card_view="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.CardView
android:id="#+id/card_view_pdf_row"
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
card_view:cardCornerRadius="3dp"
card_view:contentPadding="2dp">
<!-- Other elements in your layout -->
</android.support.v7.widget.CardView>
</RelativeLayout>
See, I've set the android:layout_height="wrap_content" in the root RelativeLayout of your pdf_row_view list item.
Do the same for the other list item layouts.
Just set the height of the rootView of specific viewHolder to 0 like this -
RecyclerView.LayoutParams layoutParams = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.height = 0;
rootView.setLayoutParams(layoutParams);
where rootView is same as this -
ViewHolder(View view) {
super(view);
rootView = view;
}
Let me know if you want more clarity on this.