Android Stackoverflow Errror: ViewGroup.jumpDrawablesToCurrentState - android

I have a problem. I got an Stackoverflow Error. I tried reduced the Layout and so on. But nothing worked.
I have a view pager and in the fragments are recyclerviews 4 times. (one in each). The inflater Code
View root = inflater.inflate(R.layout.content_main, container, false);
One Layout in Fragment for example:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
android:id="#+id/list_view"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_margin="10dp"
android:layout_marginTop="15dp"
android:layout_height="wrap_content" />
And this is the Layout for the ReclerView Item
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
android:id="#+id/cv"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_margin="20dp"
android:clickable="true"
android:background="?attr/selectableItemBackground"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center|left"
android:padding="10dp"
android:clickable="true"
android:background="?attr/selectableItemBackground"
>
<ImageView
android:id="#+id/album_cover"
android:layout_width="33dp"
android:layout_height="50dp"
android:layout_marginRight="16dp"
android:src="#drawable/ic_supervisor_account_black_24dp" />
<TextView
android:id="#+id/album_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Artistname"
android:textColor="#000000"
android:textSize="15sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
One Layout has a Relative Layout but it works before I made some changes:
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/cv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:background="?attr/selectableItemBackground"
android:layout_margin="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:clickable="true"
android:longClickable="true"
android:background="?attr/selectableItemBackground"
>
<ImageView
android:id="#+id/album_cover"
android:layout_width="66dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="16dp"
android:src="#drawable/spring_276014_640" />
<TextView
android:id="#+id/album_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/album_cover"
android:text="Albumtitel"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="#+id/album_artist"
android:layout_width="wrap_content"
android:layout_marginTop="5dp"
android:layout_height="wrap_content"
android:layout_below="#+id/album_name"
android:layout_toRightOf="#+id/album_cover"
android:text="von Artist xy" />
</RelativeLayout>
</android.support.v7.widget.CardView>
Captureing View Table is not possible because app is going down before it changes the layout.
Bitmaps are scaled.
Here is my Adapter:
public class DefaultAdapter extends RecyclerView.Adapter<DefaultAdapter.SongViewHolder> {
private Context con;
private List<String> names;
boolean useplaylist;
// 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 SongViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView title;
SongViewHolder(View itemView) {
super(itemView);
cv = (CardView)itemView.findViewById(R.id.cv);
title = (TextView)itemView.findViewById(R.id.album_name);
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public DefaultAdapter(Context context,List<String> names,boolean playlist) {
con = context;
this.names = names;
useplaylist = playlist;
}
// Create new views (invoked by the layout manager)
#Override
public SongViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.playlist_item, viewGroup, false);
SongViewHolder pvh = new SongViewHolder(v);
return pvh;
}
#Override
public void onBindViewHolder(SongViewHolder personViewHolder, int i) {
personViewHolder.title.setText(names.get(i));
}
// Return the size of your dataset (invoked by the layout manager)
#Override
public int getItemCount() {
return names.size();
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
}
Log:
java.lang.StackOverflowError: stack size 8MB\n
at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:6108)
at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:6112)
at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:6112)
at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:6112)
at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:6112)
at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:6112)
Can somebody help me?

Problem solved.
I don't know where the Error was but maby it was an empty list or the ImageAssets wich I replaced with vector assets and activate the Libary in the Gradle.
For thoos who have the same problem in a viewpage: Deactivate all fragments and enable them single, to test where the error is.

Related

Ripple Effect in Android Recycler View is not working

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.

Why Custom ArrayAdapter not showing TextView in a ListView

I have made a custom ArrayAdapter and tried to add the another TextView "Breed" of the animals, but when i execute the program , its not showing the Breed. Where am i doing wrong ? I am scratching my head since long. please help where is the mistake ?
MainActivity.Java
public class MainActivity extends AppCompatActivity {
ListView simpleList;
ArrayList<Item> animalList=new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
simpleList = (ListView) findViewById(R.id.simpleListView);
animalList.add(new Item("Lion",R.drawable.lion,"Khatarnak"));
animalList.add(new Item("Tiger",R.drawable.tiger,"Fudu"));
animalList.add(new Item("Monkey",R.drawable.monkey,"Lallu"));
animalList.add(new Item("Elephant",R.drawable.elephant,"Jabardast"));
animalList.add(new Item("Dog",R.drawable.dog,"ItemDog"));
animalList.add(new Item("Cat",R.drawable.cat,"MeeMee"));
MyAdapter myAdapter=new MyAdapter(this,R.layout.list_view_items,animalList);
simpleList.setAdapter(myAdapter);
}
}
MyAdapter.Java
public class MyAdapter extends ArrayAdapter<Item> {
ArrayList<Item> animalList = new ArrayList<>();
public MyAdapter(Context context, int textViewResourceId, ArrayList<Item> objects) {
super(context, textViewResourceId, objects);
animalList = objects;
}
#Override
public int getCount() {
return super.getCount();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_view_items, null);
TextView textView = (TextView) v.findViewById(R.id.textView);
ImageView imageView = (ImageView) v.findViewById(R.id.imageView);
TextView breedView = (TextView)v.findViewById(R.id.breed);
textView.setText(animalList.get(position).getAnimalName());
imageView.setImageResource(animalList.get(position).getAnimalImage());
breedView.setText(animalList.get(position).getBreed());
return v;
}
}
Item.Java
public class Item {
String animalName;
int animalImage;
String breedName;
public String getBreed() {
return breedName;
}
public void setBreed(String breed) {
this.breedName = breedName;
}
public Item(String animalName,int animalImage,String breedName)
{
this.animalImage=animalImage;
this.animalName=animalName;
this.breedName = breedName;
}
public String getAnimalName()
{
return animalName;
}
public int getAnimalImage()
{
return animalImage;
}
}
activity_main.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="match_parent"
tools:context=".MainActivity">
<ListView
android:id="#+id/simpleListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#000"
android:dividerHeight="2dp"/>
</RelativeLayout>
list_view_items.xml
<?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:orientation="horizontal">
<ImageView
android:id="#+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="5dp" />
<TextView
android:id="#+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Demo"
android:textColor="#000" />
<TextView
android:id="#+id/breed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Breed"
android:textColor="#000" />
</LinearLayout>
This is most likely a problem with the linear layout for each item. The orientation is horizontal, which lays each view one after the other horizontally. The problem is that the text view for the animal type has a width of fill_parent leaving no space for the breed view. If you change it to wrap_content it'll probably work for some of the cases, but might not work with everything.
In any case I think this is a problem with the views' positions and sizes. Try and move them around. Perhaps even a simple change would be placing them vertically:
<?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:orientation="horizontal">
<ImageView
android:id="#+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="5dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Demo"
android:textColor="#000" />
<TextView
android:id="#+id/breed"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Breed"
android:textColor="#000" />
</LinearLayout>
</LinearLayout>
There's perhaps a more efficient way of doing this, but this is just an example. The inner linear layout places one text view on to of the other.
PS: your getCount method is not really doing anything. You can remove it.
replace your layout with my code your issue will resolve.
<?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:orientation="horizontal">
<ImageView
android:id="#+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="5dp" />
<TextView
android:id="#+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="Demo"
android:textColor="#000" />
<TextView
android:id="#+id/breed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Breed"
android:textColor="#000" />
</LinearLayout>

Separating different Views in Android Studio using RecyclerView and CardView

I am using FirebaseRecyclerViewAdapter with CardView in my application and want to load video on one card and images on another, but I dont know how to separate them. Everytime I add a videoView in CardView layout it gets added on the top of ImageView in same card and also gets repeated in all the RecyclerView. Its like same thing is coming again and again with number of posts I have. I want that VideoView should play video on different post and ImageView shows images on different, I have searched many options but did not find anything useful, please help.
I am using Android Studio
CardView xml file:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/CardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.google.android.youtube.player.YouTubePlayerView
android:id="#+id/youtube"
android:layout_width="match_parent"
android:layout_height="200dp"/>
<ImageView
android:id="#+id/po_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerCrop" />
<TextView
android:id="#+id/ti_cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="18sp"
android:textStyle="bold"/>
<TextView
android:id="#+id/de_cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp" />
<TextView
android:id="#+id/so_cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#color/SoColor"/>
</LinearLayout>
</android.support.v7.widget.CardView>
FirebaseRecyclerView Adapter class:
FirebaseRecyclerAdapter <post, postViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<post, postViewHolder>(
post.class,
R.layout.post_row_recycle_home,
postViewHolder.class,
mDatabaseReference
) {
#Override
protected void populateViewHolder(postViewHolder viewHolder, post model, int position) {
viewHolder.setTi(model.getTi());
viewHolder.setde(model.getDe());
}
};
mrecyclerView.setAdapter(firebaseRecyclerAdapter);
}
public static class postViewHolder extends RecyclerViewPager.ViewHolder{
View mView;
public postViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setTi(String ti){
TextView post_ti = (TextView)mView.findViewById(R.id.ti_cardView);
post_ti.setText(ti);
}
public void setde(String de){
TextView post_de = (TextView)mView.findViewById(R.id.de_cardView);
post_de.setText(de);
}
set visibility of both gone at first and then depending on its video url or image url. manipulate Visibility of each view
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/CardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.google.android.youtube.player.YouTubePlayerView
android:visibility="gone"
android:id="#+id/youtube"
android:layout_width="match_parent"
android:layout_height="200dp"/>
<ImageView
android:visibility="gone"
android:id="#+id/po_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerCrop" />
<TextView
android:id="#+id/ti_cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="18sp"
android:textStyle="bold"/>
<TextView
android:id="#+id/de_cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp" />
<TextView
android:id="#+id/so_cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#color/SoColor"/>
</LinearLayout>
</android.support.v7.widget.CardView>
As you are using RecyclerView, don't put the VideoPlayer and the ImageView in the same xml layout, you can create two different layouts and use the getItemViewType() to choose the right layout to inflate.
The questions on how to use differente layouts on the same RecyclerView has been answered here
Sample code:
image_item_layout.xml
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/CardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:visibility="gone"
android:id="#+id/po_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerCrop" />
<...>
</LinearLayout>
</android.support.v7.widget.CardView>
video_item_layout.xml
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/CardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.google.android.youtube.player.YouTubePlayerView
android:visibility="gone"
android:id="#+id/youtube"
android:layout_width="match_parent"
android:layout_height="
<...>
</LinearLayout>
</android.support.v7.widget.CardView>
On the ImageOrVideoAdapter.java you can use something like:
public class ImageOrVideoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<ImageOrVideoItem> dataSource;
private final int VIDEO_VIEW = 0;
private final int IMAGE_VIEW = 1;
class VideoItemViewHolder extends RecyclerView.ViewHolder {
// Do your stuff with the video here
public VideoItemViewHolder(View itemView) {
}
}
class ImageItemViewHolder extends RecyclerView.ViewHolder {
// Do your stuff with the image here
public ImageItemViewHolder(View itemView) {
}
}
#Override
public int getItemViewType(int position) {
return dataSource.get(position).isVideoItem() ? VIDEO_VIEW : IMAGE_VIEW;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
switch (viewType) {
case VIDEO_VIEW:
return new VideoItemViewHolder(inflater.inflate(R.layout.video_item_layout, parent, false));
case IMAGE_VIEW:
return new ImageItemViewHolder(inflater.inflate(R.layout.image_item_layout, parent, false));
}
}
#Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
switch (holder.getItemViewType()) {
case VIDEO_VIEW:
VideoItemViewHolder viewHolder0 = (VideoItemViewHolder) holder;
break;
case IMAGE_VIEW:
ImageItemViewHolder viewHolder2 = (ImageItemViewHolder) holder;
break;
}
}
}
There are many posts all around StackOverflow about handling diferent layouts for the same RecyclerView, look around, you will find them.
EDIT
Take a look on this example using FireBase and see if it can adapted to fit your needs: Here

RecyclerView extra empty gap between items

1.i just implemented some recyclerviews, some of their items have extra empty space. this space sometimes disappears after some scrolling. i have tried every way you could think! i have changed heights to wrap content but still issue exists!
here is the screenshot:
Recyclerview empty space
here is item xml:
<android.support.v7.widget.CardView
android:id="#+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:gravity="center"
android:orientation="horizontal"
android:adjustViewBounds="true"
android:padding="8dp"
android:layout_margin="6dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/banner_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/no_image_banner" />
<TextView
android:id="#+id/txtHey"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:gravity="center"
android:maxLength="20"
android:text="sadas"
android:textColor="#4d4a46"
android:textSize="11dp"
android:textStyle="bold" />
<TextView
android:id="#+id/txtHi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:gravity="center"
android:text="sadas"
android:textColor="#999999"
android:textSize="9dp" />
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_vertical">
<TextView
android:id="#+id/txtPrice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="sadas"
android:textColor="#888888"
android:textSize="9dp" />
<LinearLayout
android:id="#+id/price_cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:background="#888888"
android:orientation="horizontal" />
</FrameLayout>
<TextView
android:id="#+id/txtFinalPrice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="sadas"
android:textColor="#3dc24d"
android:textSize="9dp" />
<TextView
android:id="#+id/txtHello"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="hey"
android:textSize="9dp"
android:visibility="gone" />
</LinearLayout>
</android.support.v7.widget.CardView>
main page xml:
<android.support.design.widget.AppBarLayout
android:id="#+id/topBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<include
layout="#layout/toolbar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/product_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="6dp"
>
</android.support.v7.widget.RecyclerView>
<TextView
android:id="#+id/request_results"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="21dp"
android:textStyle="bold" />
</RelativeLayout>
second question is i used some recyclerviews inside a nested scroll view to prevent scrolling issues. everything is good but in lower versions of android like kitkat i have problem with scrolling(cause nested wont work on sdk<21 ) . i could use a normal scrollview that works well on both versions but normal scroll view doesnt work with coordinator layout and so appbar layout hiding behaviour wont work!
can you help me with these problems please?
here is the adapter class:
private ArrayList<Struct> structs;
OnItemListener onItemListener;
private boolean isGrid;
private int Tab;
public Adapter_Recycler(ArrayList<Struct> structs, OnItemListener onItemListener, int Tab, boolean isGrid) {
this.structs = structs;
this.onItemListener = onItemListener;
this.Tab = Tab;
this.isGrid = isGrid;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = null;
if (Tab == 2) {
view = inflater.inflate(R.layout.item_product_color, parent, false);
}
if (Tab == 1 && isGrid == false) {
view = inflater.inflate(R.layout.item_product_list, parent, false);
}
if (Tab == 1 && isGrid) {
view = inflater.inflate(R.layout.item_product_list, parent, false);
}
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, final int position) {
if (Tab == 2) {
holder.txtHey.setText(structs.get(position).hey);
holder.txtPrice.setText(structs.get(position).hi);
holder.txtHi.setVisibility(View.GONE);
holder.PriceCancel.setVisibility(View.GONE);
holder.txtFinalPrice.setVisibility(View.GONE);
holder.txtHey.setTypeface(Activity_Main.SANS);
holder.txtPrice.setTypeface(Activity_Main.SANS);
Glide
.with(Activity_Main.CONTEXT)
.load(structs.get(position).image)
.placeholder(R.drawable.background_card)
.fitCenter()
.into(holder.banner_image);
}
if (Tab == 1) {
holder.txtHey.setText(structs.get(position).hey);
holder.txtHi.setText(structs.get(position).hi);
holder.txtPrice.setText(structs.get(position).price);
holder.txtFinalPrice.setText(structs.get(position).final_price);
if (holder.txtFinalPrice.getText().toString() == "") {
holder.PriceCancel.setVisibility(View.GONE);
} else {
holder.PriceCancel.setVisibility(View.VISIBLE);
}
holder.txtHey.setTypeface(Activity_Main.SANS);
holder.txtHi.setTypeface(Activity_Main.SANS);
holder.txtPrice.setTypeface(Activity_Main.SANS);
holder.txtFinalPrice.setTypeface(Activity_Main.SANS);
Glide
.with(Activity_Main.CONTEXT)
.load(structs.get(position).image)
.placeholder(R.drawable.background_card)
.fitCenter()
.into(holder.banner_image);
}
holder.linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (onItemListener != null) {
onItemListener.onItemSelect(position);
}
}
});
holder.txtHello.setText(structs.get(position).hello);
holder.txtHello.setTypeface(Activity_Main.SANS);
}
#Override
public int getItemCount() {
return structs.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
CardView linearLayout;
ImageView banner_image;
TextView txtHey;
TextView txtHi;
TextView txtHello;
TextView txtPrice;
TextView txtFinalPrice;
LinearLayout PriceCancel;
public ViewHolder(View itemView) {
super(itemView);
linearLayout = (CardView) itemView.findViewById(R.id.btn);
banner_image = (ImageView) itemView.findViewById(R.id.banner_image);
txtHey = (TextView) itemView.findViewById(R.id.txtHey);
txtHi = (TextView) itemView.findViewById(R.id.txtHi);
txtHello = (TextView) itemView.findViewById(R.id.txtHello);
txtPrice = (TextView) itemView.findViewById(R.id.txtPrice);
txtFinalPrice = (TextView) itemView.findViewById(R.id.txtFinalPrice);
PriceCancel = (LinearLayout) itemView.findViewById(R.id.price_cancel);
}
}
}
UPDATE:
the place holder aspect ratio must not be much different from the images aspect ratio. they should be the same.
If possible then provide adapter class because may be some issue with onCreateViewHolder with LayoutInflater.
I finally figured out what the problem was!
that extra space was because of
" .placeholder(R.drawable.background_card) " !
problem solved after removing the placeholder.
ofcourse issue is with the image! not placeholder.

CardView not showing content eclipse

I am using android.support.v7.widget.CardView to show ImageView with two TextView's in RecyclerView but card view not showing the content(showing only blank white cards) when the screen orientation is vertical, on the other hand it is showing content when orientation is landscape.
When I run the same project from AndroidStudio there is no problem everything works fine.
I don't understand what is problem out there, is there problem with eclipse?
Please see the code below,
PostListFragment
public class PostListFragment extends Fragment implements AppConfig {
private ArrayList<Post> mPostArrayList;
private PostRecyclerAdapter mPostRecyclerAdapter;
private RecyclerView mRecyclerView;
//other declarations
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//removed
}
private void init() {
// Set up RecyclerView
mRecyclerView = (RecyclerView) mRootView
.findViewById(R.id.mPostListRecyclerView);
// Setup layout manager for mPostArrayList and column count
final LinearLayoutManager mLayoutManager = new LinearLayoutManager(
getActivity());
// Control orientation of the mPostArrayList
mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mLayoutManager.scrollToPosition(0);
// Attach layout manager
mRecyclerView.setLayoutManager(mLayoutManager);
// Listen to the item touching
mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(
getActivity(),
new RecyclerItemClickListener.OnItemClickListener() {
#Override
public void onItemClick(View itemView, int position) {
//some action
}
}));
mPostArrayList = new ArrayList<>();
// Bind adapter to recycler
mPostRecyclerAdapter = new PostRecyclerAdapter(
getActivity(), mPostArrayList);
mRecyclerView.setAdapter(mPostRecyclerAdapter);
}
private void getPosts() {
// Execute async task
new AsyncPosts().execute(mPostURL);
}
public class AsyncPosts extends AsyncTask<Object, String, JSONObject> {
//no problem with this
}
}
Layout used for PostListFragment
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/mParentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/mPostListContainer"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/mPostListSwipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<android.support.v7.widget.RecyclerView
android:id="#+id/mPostListRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.v4.widget.SwipeRefreshLayout>
</FrameLayout>
<!-- removed -->
</LinearLayout>
Here is adapter to set the values,
PostRecyclerAdapter
public class PostRecyclerAdapter extends
RecyclerView.Adapter<PostRecyclerAdapter.SimpleItemViewHolder> {
private Context mContext;
private List<Post> post;
// Provide a suitable constructor (depends on the kind of data store)
public PostRecyclerAdapter(Context context, List<Post> items) {
this.mContext = context;
this.post = items;
}
// Return the size of your data set (invoked by the layout manager)
#Override
public int getItemCount() {
return this.post.size();
}
// Create new items (invoked by the layout manager)
// Usually involves inflating a layout from XML and returning the holder
#Override
public SimpleItemViewHolder onCreateViewHolder(ViewGroup viewGroup,
int viewType) {
View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(
R.layout.post_list_item, viewGroup, false);
return new SimpleItemViewHolder(itemView);
}
// Replace the contents of a view (invoked by the layout manager)
// Involves populating data into the item through holder
#Override
public void onBindViewHolder(SimpleItemViewHolder viewHolder, int position) {
Glide.with(mContext).load(post.get(position).IMG_URL)
.placeholder(R.drawable.ic_placeholder).crossFade(1000)
.centerCrop().into(viewHolder.mPostListSmallThumbnail);
viewHolder.mPostListSmallTitle.setText(post.get(position).POST_TITLE);
viewHolder.mPostListSmallContent
.setText(post.get(position).POST_CONTENT);
}
// Provide a reference to the views for each data item
// Provide access to all the views for a data item in a view holder
public final static class SimpleItemViewHolder extends
RecyclerView.ViewHolder {
ImageView mPostListSmallThumbnail;
TextView mPostListSmallTitle, mPostListSmallContent;
public SimpleItemViewHolder(View itemView) {
super(itemView);
mPostListSmallThumbnail = (ImageView) itemView
.findViewById(R.id.mPostListSmallThumbnail);
mPostListSmallTitle = (TextView) itemView
.findViewById(R.id.mPostListSmallTitle);
mPostListSmallContent = (TextView) itemView
.findViewById(R.id.mPostListSmallContent);
}
}
}
post_list_item
<?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/mPostListSmallCard"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:foreground="#drawable/card_background"
android:clickable="true"
app:cardCornerRadius="#dimen/blog_card_radius"
app:cardUseCompatPadding="true"
app:contentPadding="#dimen/blog_card_radius">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/mPostListSmallThumbnail"
android:layout_width="#dimen/blog_image_thumb_dim"
android:layout_height="#dimen/blog_image_thumb_dim"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#drawable/ic_placeholder"
android:adjustViewBounds="true"
android:contentDescription="#string/image_thumbnail_placeholder" />
<TextView
android:id="#+id/mPostListSmallTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="#dimen/post_list_margin_left"
android:layout_marginStart="#dimen/post_list_margin_right"
android:layout_toEndOf="#+id/mPostListSmallThumbnail"
android:layout_toRightOf="#+id/mPostListSmallThumbnail"
android:ellipsize="end"
android:lines="2"
android:singleLine="false"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/mPostListSmallContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/mPostListSmallThumbnail"
android:layout_alignEnd="#+id/mPostListSmallTitle"
android:layout_alignLeft="#+id/mPostListSmallTitle"
android:layout_alignRight="#+id/mPostListSmallTitle"
android:layout_alignStart="#+id/mPostListSmallTitle"
android:layout_below="#+id/mPostListSmallTitle"
android:ellipsize="end"
android:lines="3"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
</android.support.v7.widget.CardView>
Here is post_list_item for landscape layout
land/post_list_item
<?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/mPostListSmallCard"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="#dimen/blog_card_margin_landscape"
android:layout_marginLeft="#dimen/blog_card_margin_landscape"
app:cardCornerRadius="#dimen/blog_card_radius"
app:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/mPostListSmallThumbnail"
android:layout_width="#dimen/blog_image_thumb_dim"
android:layout_height="#dimen/blog_image_thumb_dim"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
android:contentDescription="#string/image_thumbnail_placeholder" />
<TextView
android:id="#+id/mPostListSmallTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="#dimen/post_list_margin_left"
android:layout_marginStart="#dimen/post_list_margin_right"
android:layout_toEndOf="#+id/mPostListSmallThumbnail"
android:layout_toRightOf="#+id/mPostListSmallThumbnail"
android:ellipsize="end"
android:lines="2"
android:singleLine="false"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/mPostListSmallContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/mPostListSmallThumbnail"
android:layout_alignEnd="#+id/mPostListSmallTitle"
android:layout_alignLeft="#+id/mPostListSmallTitle"
android:layout_alignRight="#+id/mPostListSmallTitle"
android:layout_alignStart="#+id/mPostListSmallTitle"
android:layout_below="#+id/mPostListSmallTitle"
android:ellipsize="end"
android:lines="3"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
</android.support.v7.widget.CardView>

Categories

Resources