RecyclerView issues: EditText loses focus - android

I've put some EditText in RecyclerView because I need to get some values. The implementation is this:
<android.support.v7.widget.RecyclerView
android:layout_below="#id/firstrow"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="true"
android:descendantFocusability="beforeDescendants"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/rectable"
android:layout_marginLeft="#dimen/table_margin"
android:layout_marginRight="#dimen/table_margin"
android:layout_marginBottom="300dp" />
and this is the custom xml with some editText:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#color/white"
android:orientation="horizontal"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="60dp ">
<TextView
android:text="TextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/description"
android:layout_weight="1"
android:textColor="#color/black" />
<View
style="#style/VerticalSeparator" />
<EditText
android:hint="lol"
android:id="#+id/weight"
android:focusable="true"
style="#style/DefaultEditCellStyle" />
<View
style="#style/VerticalSeparator" />
<EditText
android:hint="lol"
android:id="#+id/arm"
android:focusable="true"
style="#style/DefaultEditCellStyle" />
<View
style="#style/VerticalSeparator" />
<EditText
android:hint="lol"
android:focusable="true"
android:id="#+id/moment"
style="#style/DefaultEditCellStyle" />
practically when I click on an EditText it opens keyboard, lose focus and close keyboard immediately, so it's impossible to write in these. I also tried reading other question, to put this:
recyclerView.setFocusable(true);
recyclerView.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
recyclerView.setAdapter(adapter);
but, at different of listview, it doesn't work.
How could I solve this issue?
Thank you

Finally I've solved it using this:
android:focusableInTouchMode="true"
android:descendantFocusability="beforeDescendants"
in RecyclerView's layout and I add it in the Manifest:
android:windowSoftInputMode="stateHidden|adjustPan"

Maybe, it will help someone: use notifyItemRangeChanged() instead notifyDataSetChanged() in your adapter class.

If you use proper implementation in xml code, then this problem can be solved easily.
Here I have found a nice example Android Recyclerview with edittext
They solved this problem.
Here are some of the source code
xml code for recyclerview row item
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
card_view:cardCornerRadius="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:textColor="#000"
android:text="TextView:"/>
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="#+id/editid"
android:layout_marginTop="10dp"
android:paddingLeft="10dp"
android:textColor="#000"
android:text="hello"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
Adapter
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by Parsania Hardik on 17-Apr-18.
*/
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {
private LayoutInflater inflater;
public static ArrayList<EditModel> editModelArrayList;
public CustomAdapter(Context ctx, ArrayList<EditModel> editModelArrayList){
inflater = LayoutInflater.from(ctx);
this.editModelArrayList = editModelArrayList;
}
#Override
public CustomAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.rv_item, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(final CustomAdapter.MyViewHolder holder, final int position) {
holder.editText.setText(editModelArrayList.get(position).getEditTextValue());
Log.d("print","yes");
}
#Override
public int getItemCount() {
return editModelArrayList.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{
protected EditText editText;
public MyViewHolder(View itemView) {
super(itemView);
editText = (EditText) itemView.findViewById(R.id.editid);
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
editModelArrayList.get(getAdapterPosition()).setEditTextValue(editText.getText().toString());
}
#Override
public void afterTextChanged(Editable editable) {
}
});
}
}
}
As per above code, edittext values are controlled in the adapter class to remove focus and scrolling problems.

android:windowSoftInputMode="stateHidden|adjustResize|adjustPan"
From your manifest on the activity itself should be sufficient to solve the keyboard

Related

Inflating a TextView and an ImageView within a LinearLayout from a RecyclerView

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

getting abrupt height for a horizontal recyclerview lists

I am creating a horizontal recyclerView containing cardView which in turn have a list of information.
If a name is of more than 3 lines the overall height of the recyclerview is disturbed.
Let me explain with the example:
Below is the normal layout when no text is more than 1
line
Now below is the layout when there is more than 1 line
Below are all the relevant files that are used
single_cast_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
android:minHeight="250dp"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:minWidth="100dp"
android:layout_margin="#dimen/card_dimen"
android:elevation="3dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/castImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:scaleType="fitXY"
android:foregroundGravity="center"
android:contentDescription="#string/castContentDesc"
android:focusable="true"
tools:src="#drawable/ic_launcher_background" />
<TextView
android:id="#+id/castName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/castImage"
android:lines="2"
android:paddingLeft="#dimen/cast_name_padding"
android:paddingRight="#dimen/cast_name_padding"
android:paddingTop="#dimen/cast_name_padding"
android:textAlignment="center"
android:textColor="#color/movieNameBackground"
android:textSize="#dimen/cast_name_size"
tools:text="Actor Name" />
<TextView
android:id="#+id/castMovieName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/castName"
android:layout_centerHorizontal="true"
android:textAlignment="center"
android:paddingBottom="#dimen/cast_name_padding"
tools:text="character name" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
castListAdapter.java
package com.example.ashish.moviesnow.adapters;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.ashish.moviesnow.Model.CastDetails;
import com.example.ashish.moviesnow.R;
import com.squareup.picasso.Picasso;
import java.util.List;
public class CastListAdapter extends RecyclerView.Adapter<CastListAdapter.MyViewHolder> {
private Context mContext;
private List<CastDetails> mList;
public CastListAdapter(Context mContext, List<CastDetails> mList) {
this.mContext = mContext;
this.mList = mList;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.cast_layout,parent,false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
CastDetails cd = mList.get(position);
String replacedString = cd.getmCastMovieName().replace("/","/\n");
if (cd.getmCastMovieName().length()>20){
}
holder.mCastText.setText(cd.getmCastRealName());
holder.mCastMovieName.setText(replacedString);
Picasso.with(mContext).load(cd.getmCastPoster())
.placeholder(R.drawable.ic_launcher_foreground)
.into(holder.mCastImage);
}
#Override
public int getItemCount() {
return mList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
private TextView mCastText,mCastMovieName;
private ImageView mCastImage;
public MyViewHolder(View itemView) {
super(itemView);
mCastText=(TextView)itemView.findViewById(R.id.castName);
mCastImage=(ImageView) itemView.findViewById(R.id.castImage);
mCastMovieName=(TextView)itemView.findViewById(R.id.castMovieName);
}
}
}
You can use android:ellipsize = "end" so if the sentence size larger than your line it will show dots (...) at the end you can also show it as you want.
Use android:lines="2" and android:ellipsize = "end" it will be fine for you . Or increase android:lines="3" and fix the layout_height as it. So all the layouts row will be show as same height.
<TextView
....
android:text="Hi I am Abhishek, How are you ?"
android:ellipsize = "end"
/>
Output will be as below :
Hi I am Abhi.....
Start's output will be : ...am Abhishek
End's output will be : Hi I am Ab...
Middle's output will be : Hi...Abhishek

Recycler view and Card View not working properly

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.

RecyclerView Margin

i am developing application for home security. One of the feature, that must be implemented is the ability to see connected devices (for sending notifications, blocking access, etc). So far, i've been able to create RecyclerView list of the devices, everything is perfect (for me), except that cards in this list have no spacing between them.
Screenshot of how it looks now
device_data_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardview="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_margin="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="80dp">
<ImageView
android:id="#+id/deviceIcon"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:scaleType="centerCrop"
android:layout_marginLeft="8dp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="80dp"
android:layout_alignBottom="#+id/deviceIcon"
android:layout_toEndOf="#+id/deviceIcon">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Username"
android:id="#+id/deviceUsername"
android:layout_gravity="center_vertical"
android:textColor="#000000"
android:layout_marginLeft="5dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_gravity="center_vertical"
android:gravity="center_vertical|center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="25dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Device Model"
android:gravity="center_vertical|right"
android:textColor="#000000"
android:id="#+id/deviceModel"
android:layout_marginLeft="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="25dp"
android:textColor="#000000"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Device Version"
android:gravity="center_vertical|right"
android:id="#+id/deviceVersion"
android:layout_marginLeft="5dp" />
</LinearLayout>
</LinearLayout>
devices_list.xml
<?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"
android:padding="#dimen/activity_vertical_margin">
<view
android:id="#+id/connectedDevicesList"
class="android.support.v7.widget.RecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true" />
</RelativeLayout>
View Holder Code
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.exampleapp.R;
public class DeviceViewHolder extends RecyclerView.ViewHolder {
protected ImageView deviceIcon;
protected TextView deviceUsername;
protected TextView deviceModel;
protected TextView deviceVersion;
public DeviceViewHolder(View view) {
super(view);
this.deviceIcon = (ImageView) view.findViewById(R.id.deviceIcon);
this.deviceUsername = (TextView) view.findViewById(R.id.deviceUsername);
this.deviceModel = (TextView) view.findViewById(R.id.deviceModel);
this.deviceVersion = (TextView) view.findViewById(R.id.deviceVersion);
}
}
RecyclerView Adapter
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
import com.exampleapp.API.AuthorizedDevice;
import com.exampleapp.R;
public class ConnectedDeviceAdapter extends RecyclerView.Adapter<DeviceViewHolder> {
private List<AuthorizedDevice> devices;
private Context mContext;
public ConnectedDeviceAdapter(Context context, List<AuthorizedDevice> devices) {
this.devices = devices;
this.mContext = context;
}
#Override
public DeviceViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.connected_devices_layout, null);
DeviceViewHolder viewHolder = new DeviceViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(DeviceViewHolder deviceViewHolder, int i) {
AuthorizedDevice device = devices.get(i);
if (device.getDeviceId().equals("0")) {
deviceViewHolder.deviceIcon.setBackground(ContextCompat.getDrawable(mContext, R.drawable.android_icon));
} else {
deviceViewHolder.deviceIcon.setBackground(ContextCompat.getDrawable(mContext, R.drawable.apple_icon));
}
deviceViewHolder.deviceUsername.setText(device.getUsername());
deviceViewHolder.deviceModel.setText(device.getDeviceName());
deviceViewHolder.deviceVersion.setText(device.getDeviceVersion());
}
#Override
public int getItemCount() {
return (null != devices ? devices.size() : 0);
}
}
Can anyone please help me solve this problem? Thank you very much in advance!
In your RecyclerView Adapter onCreateViewHolder:
replace:
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.connected_devices_layout, null);
with:
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.connected_devices_layout, viewGroup, false);
Or you can use RecyclerView.addItemDecoration(ItemDecoration decor).

how to make an expandable list of cardviews?

I searched a lot to find a direct and clear solution for I think a popular problem but unfortunately I couldn't find it.
We want to have a list of cardviews so each card bind to a specific data and each card has a list inside that shows meta or detail data about its parent.
So we have a nested list inside a cardview.
With a simple search, we know that we should use expanded list view which parents items are cardviews and we must have another layout for its child.
So when you click on cards a list of items appears below of your cards on the root.
But we want to show child list inside of cards?
And there is no access to the something like child list id or any other things to refer to a transition animation and change of layout.
So the clear question is how to add a listview inside a cardview?
i follow my work by using tablelayout and table row. and prefer not to use external libraries for stability and version problems.
this is what i mean.
Image that describe my question
If you don't want to use an external library, you can make your CardView expand like this:
Layout file for an expandable CardView
<?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:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cv"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
card_view:cardCornerRadius="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="48dp"
>
<TextView
android:id="#+id/textView_name"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:textSize="18sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold" />
<!--My dropdown Button -->
<RelativeLayout
android:id="#+id/button"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="end"
android:layout_alignParentRight="true"
android:gravity="center"
>
<View
android:layout_width="12dp"
android:layout_height="12dp"
android:background="#drawable/triangle"
android:layout_alignParentRight="false"
android:layout_alignParentEnd="false" />
</RelativeLayout>
</RelativeLayout>
<!--The layout below is my ExpandableLayout -->
<LinearLayout
android:id="#+id/expandableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<android.support.v7.widget.AppCompatImageView
android:id="#+id/imageView_Owner"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical">
<TextView
android:id="#+id/textView_Owner"
android:textSize="16sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textView_OwnerUrl"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
My ExpandableRecyclerAdapter Class
import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.util.List;
public class ExpandableRecyclerAdapter extends RecyclerView.Adapter<ExpandableRecyclerAdapter.ViewHolder> {
private List<Repo> repos;
private SparseBooleanArray expandState = new SparseBooleanArray();
private Context context;
public ExpandableRecyclerAdapter(List<Repo> repos) {
this.repos = repos;
//set initial expanded state to false
for (int i = 0; i < repos.size(); i++) {
expandState.append(i, false);
}
}
#Override
public ExpandableRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
this.context = viewGroup.getContext();
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.expandable_card_row, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final ExpandableRecyclerAdapter.ViewHolder viewHolder, final int i) {
viewHolder.setIsRecyclable(false);
viewHolder.tvName.setText(repos.get(i).getName());
viewHolder.tvOwnerLogin.setText("Owner: " +repos.get(i).getOwner().getLogin());
viewHolder.tvOwnerUrl.setText(repos.get(i).getOwner().getUrl());
Picasso.with(context)
.load(repos.get(i).getOwner().getImageUrl())
.resize(500, 500)
.centerCrop()
.into(viewHolder.ivOwner);
//check if view is expanded
final boolean isExpanded = expandState.get(i);
viewHolder.expandableLayout.setVisibility(isExpanded?View.VISIBLE:View.GONE);
viewHolder.buttonLayout.setRotation(expandState.get(i) ? 180f : 0f);
viewHolder.buttonLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
onClickButton(viewHolder.expandableLayout, viewHolder.buttonLayout, i);
}
});
}
#Override
public int getItemCount() {
return repos.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView tvName,tvOwnerLogin, tvOwnerUrl;
private ImageView ivOwner;
public RelativeLayout buttonLayout;
public LinearLayout expandableLayout;
public ViewHolder(View view) {
super(view);
tvName = (TextView)view.findViewById(R.id.textView_name);
tvId = (TextView)view.findViewById(R.id.textView_id);
tvUrl = (TextView)view.findViewById(R.id.textView_url);
tvOwnerLogin = (TextView)view.findViewById(R.id.textView_Owner);
tvOwnerUrl = (TextView)view.findViewById(R.id.textView_OwnerUrl);
ivOwner = (ImageView) view.findViewById(R.id.imageView_Owner);
buttonLayout = (RelativeLayout) view.findViewById(R.id.button);
expandableLayout = (LinearLayout) view.findViewById(R.id.expandableLayout);
}
}
private void onClickButton(final LinearLayout expandableLayout, final RelativeLayout buttonLayout, final int i) {
//Simply set View to Gone if not expanded
//Not necessary but I put simple rotation on button layout
if (expandableLayout.getVisibility() == View.VISIBLE){
createRotateAnimator(buttonLayout, 180f, 0f).start();
expandableLayout.setVisibility(View.GONE);
expandState.put(i, false);
}else{
createRotateAnimator(buttonLayout, 0f, 180f).start();
expandableLayout.setVisibility(View.VISIBLE);
expandState.put(i, true);
}
}
//Code to rotate button
private ObjectAnimator createRotateAnimator(final View target, final float from, final float to) {
ObjectAnimator animator = ObjectAnimator.ofFloat(target, "rotation", from, to);
animator.setDuration(300);
animator.setInterpolator(new LinearInterpolator());
return animator;
}
}
In your Activity/Fragment, set up RecyclerView like this
private RecyclerView recyclerView;
private List<Repo> data;
recyclerView = (RecyclerView)findViewById(R.id.card_recycler_view);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
//fetch data and on ExpandableRecyclerAdapter
recyclerView.setAdapter(new ExpandableRecyclerAdapter(data));
So, its quite simple to create an expandable CardView without external Library. The code is almost exactly the same as using a normal CardView with RecyclerView, the main change is setting the View.GONE/View.VISIBLE on button click.
You can use ExpandLayout,and then add it in cardview.
<com.kyo.expandablelayout.ExpandableLayout
android:id="#+id/expandlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp" >
<ImageView
android:id="#+id/imageview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="#null"
android:scaleType="centerCrop"
android:src="#drawable/parent" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:contentDescription="#null"
android:scaleType="centerCrop"
android:src="#drawable/child"
app:canExpand="true" />
</com.kyo.expandable.ExpandableLayout>

Categories

Resources