I can use cardflip effect in a recyclerview item? - android

I want use the card flip effect (http://developer.android.com/intl/es/training/animation/cardflip.html) in each item of a recyclerview.
I have the 3 views.
item_cancion:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
item_cancion_front and item_cancion_back (Two xml with the same info for a proof)
<?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="5dp"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imgReproduciendo"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignBottom="#+id/txtArtista"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:maxWidth="30dp"
android:layout_marginRight="10dp" />
<TextView
android:id="#+id/txtTitulo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dp"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:singleLine="true"
android:layout_toLeftOf="#+id/txtDuracion"
android:layout_toRightOf="#+id/imgReproduciendo"
android:layout_toEndOf="#+id/imgReproduciendo"
android:text="Titulo" />
<TextView
android:id="#+id/txtArtista"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dp"
android:singleLine="true"
android:maxWidth="190dp"
android:layout_below="#+id/txtTitulo"
android:layout_toRightOf="#+id/imgReproduciendo"
android:layout_toEndOf="#+id/imgReproduciendo"
android:text="Artista" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtAlbum"
android:singleLine="true"
android:textSize="14dp"
android:gravity="right"
android:layout_below="#+id/txtDuracion"
android:layout_alignRight="#+id/txtDuracion"
android:layout_alignEnd="#+id/txtDuracion"
android:layout_toRightOf="#+id/txtArtista"
android:layout_marginLeft="15dp"
android:text="Album" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtDuracion"
android:paddingLeft="25dp"
android:singleLine="true"
android:textSize="14dp"
android:textStyle="italic"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:text="Duracion" />
</RelativeLayout>
In the viewHolder adapter I have:
#Override
public AdaptadorCancionesActual.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_cancion, parent, false);
actividad.getFragmentManager().beginTransaction().add(R.id.container, new CardFrontFragment()).commit();
ViewHolder vh = new ViewHolder(v, this);
return vh;
}
and the ViewHolder is:
public ViewHolder(View v, AdaptadorCancionesActual mAdapter) {
super(v);
this.mAdapter = mAdapter;
frameLayout = (RelativeLayout) v.findViewById(R.id.container);
txtCancion = (TextView) frameLayout.findViewById(R.id.txtTitulo);
txtArtista = (TextView) frameLayout.findViewById(R.id.txtArtista);
txtAlbum = (TextView) frameLayout.findViewById(R.id.txtAlbum);
txtFin = (TextView) frameLayout.findViewById(R.id.txtDuracion);
imgReproduciendo = (ImageView) frameLayout.findViewById(R.id.imgReproduciendo);
v.setOnClickListener(this);
}
#Override
public void onClick(View view) {
int position = getLayoutPosition();
Datos.getMusicSrv().setSong(position);
Datos.getMusicSrv().playSong();
mAdapter.flipCard();
//mAdapter.notifyDataSetChanged();
}
I have two question...
First, I can use this effect in the item of recyclerView?
And if Yes... Why the TextViews are null.

Related

ExpandableRecyclerview Inside a Cardview Recyclerview

I am totally familiar with ExpandableRecyclerview Or ExpandableLayout But I have a design that is a little bit unique and I am seeking help to achieve this implementation.
I Am Attaching the design
When the user clicks the arrow shown in the image the CardView Recyclerview should expand or inflate a recyclerview in between comment button and response section. How to do that. I have tried the ExpandableRecyclerview from ThoughtBot and ExpandableListView all this components add a child to parent.
I am using ExpandableRecyclerview from Thoughtbot,
com.thoughtbot.expandablerecyclerview
My Adapter Looks like as below.
public class ReviewRecyclerAdapter extends ExpandableRecyclerViewAdapter<ReviewViewHolder, CommentsViewHolder> {
private Context context;
public ReviewRecyclerAdapter(Context context,List<? extends ExpandableGroup> groups) {
super(groups);
this.context = context;
}
#Override
public ReviewViewHolder onCreateGroupViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_review, parent, false);
return new ReviewViewHolder(view);
}
#Override
public CommentsViewHolder onCreateChildViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_comments, parent, false);
return new CommentsViewHolder(view);
}
#Override
public void onBindChildViewHolder(CommentsViewHolder holder, int flatPosition, ExpandableGroup group, int childIndex) {
final CommentsModel comments = ((ReviewsModel) group).getItems().get(childIndex);
holder.setSubTitletName(comments.getComment());
}
#Override
public void onBindGroupViewHolder(ReviewViewHolder holder, int flatPosition, ExpandableGroup group) {
holder.setGenreTitle(context, group);
}
}
// View Holders
public class ReviewViewHolder extends GroupViewHolder {
private TextView titleName;
private ImageView arrow;
private ImageView icon;
public ReviewViewHolder(View itemView) {
super(itemView);
titleName = (TextView) itemView.findViewById(R.id.list_item_genre_name);
arrow = (ImageView) itemView.findViewById(R.id.list_item_genre_arrow);
}
public void setGenreTitle(Context context, ExpandableGroup title) {
if (title instanceof ReviewsModel) {
titleName.setText(title.getTitle());
if (((ReviewsModel) title).getReview()!= null && !((ReviewsModel) title).getReview().isEmpty()){
}
}
}
#Override
public void expand() {
animateExpand();
}
#Override
public void collapse() {
animateCollapse();
}
private void animateExpand() {
RotateAnimation rotate =
new RotateAnimation(360, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(300);
rotate.setFillAfter(true);
arrow.setAnimation(rotate);
}
private void animateCollapse() {
RotateAnimation rotate =
new RotateAnimation(180, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(300);
rotate.setFillAfter(true);
arrow.setAnimation(rotate);
}
}
public class CommentsViewHolder extends ChildViewHolder {
private TextView subTitleTextView;
public CommentsViewHolder(View itemView) {
super(itemView);
subTitleTextView = (TextView) itemView.findViewById(R.id.subtitle);
}
public void setSubTitletName(String name) {
subTitleTextView.setText(name);
}
}
//Review Layout
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="24dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/review_details"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/list_avatar"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:src="#drawable/profile_placeholder" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="16dp"
android:layout_toEndOf="#+id/list_avatar">
<TextView
android:id="#+id/list_item_genre_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:maxEms="15"
android:text=" Guru natha"
android:textStyle="bold"
android:layout_alignParentTop="true" />
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxEms="15"
android:text=" From Mg Road"
android:layout_alignStart="#+id/list_item_genre_name"
android:layout_marginStart="10dp"
android:layout_below="#+id/list_item_genre_name" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true">
<TextView
android:id="#+id/days"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="10dp"
android:maxEms="15"
android:text=" 3 Days ago"
android:textSize="8sp"
android:textStyle="bold" />
<TextView
android:id="#+id/count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/days"
android:layout_below="#+id/days"
android:maxEms="15"
android:text=" 448 Views"
android:textSize="8sp" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/review_details"
android:layout_margin="20dp">
<TextView
android:id="#+id/review"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="10dp"
android:maxEms="15"
android:text="Review"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginStart="10dp"
android:maxEms="15"
android:text="Rating"
android:textStyle="bold" />
<View
android:id="#+id/lineytf"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/review"
android:background="#color/black" />
<TextView
android:layout_marginTop="10dp"
android:id="#+id/review_conetent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/lineytf"
android:layout_below="#+id/lineytf"
android:maxLines="3"
android:maxEms="15"
android:text="#string/lorem_ipsum" />
</RelativeLayout>
<RelativeLayout
android:layout_marginLeft="30dp"
android:layout_marginRight="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/content">
<TextView
android:id="#+id/response"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="30dp"
android:maxEms="15"
android:text="Response (2)"
android:textStyle="bold" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginStart="10dp"
android:maxEms="15"
android:layout_above="#+id/line2"
android:src="#drawable/white_r_arrow"
android:text="arrow"
android:textStyle="bold"
android:id="#+id/list_item_genre_arrow" />
<View
android:id="#+id/line2"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/response"
android:background="#color/black" />
<!--Add expandable views-->
<Button
android:id="#+id/commentsbutton"
android:textSize="12sp"
android:textAllCaps="false"
android:text="Comments"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_below="#+id/line2"
android:layout_toStartOf="#+id/button2" />
<Button
android:textSize="12sp"
android:textAllCaps="false"
android:text="56"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_below="#+id/list_item_genre_arrow"
android:layout_alignParentEnd="true"
android:id="#+id/button2" />
</RelativeLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
//Comment Layout
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="40dp">
<ImageView
android:id="#+id/list_item_genre_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginLeft="70dp"
android:layout_alignParentRight="true"
android:src="#drawable/android" />
<TextView
android:id="#+id/subtitle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="100dp"
android:gravity="center_vertical"
tools:text="SubTitle" />
</FrameLayout>
Current Screenshot :
Please do shoot the closest solution if you have.

How to change layout height automatically with word wrap?

As you see, height sets normally when there's only one text string, but all crashes, when it's wrapped:
Here's my 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:background="?android:attr/selectableItemBackground"
android:focusable="true"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingStart="15dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingEnd="15dp"
>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/title"
android:textSize="16sp"
android:textStyle="bold"
android:layout_marginTop="10dp"
/>
<TextView
android:id="#+id/descr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
/>
</LinearLayout>
<CheckBox
android:id="#+id/checkbox"
android:theme="#style/checkBoxStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_marginTop="7dp"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
/>
</LinearLayout>
Here's my adapter:
class AppsAdapter extends RecyclerView.Adapter<AppsAdapter.ViewHolder> {
private Activity activity;
private ArrayList<Item> items;
private int inflater;
private OnItemCheckListener onItemClick;
AppsAdapter (Activity activity, ArrayList<Item> items, int inflater, OnItemCheckListener onItemCheckListener) {
this.activity = activity;
this.items = items;
this.inflater = inflater;
this.onItemClick = onItemCheckListener;
}
static class ViewHolder extends RecyclerView.ViewHolder {
private ImageView icon;
private TextView title, descr, version;
private CheckBox checkbox;
private ViewHolder (View view) {
super (view);
icon = (ImageView) view.findViewById (R.id.icon);
title = (TextView) view.findViewById (R.id.title);
descr = (TextView) view.findViewById (R.id.descr);
version = (TextView) view.findViewById (R.id.version);
checkbox = (CheckBox) view.findViewById (R.id.checkbox);
}
}
#Override
public ViewHolder onCreateViewHolder (ViewGroup parent, int viewType) {
return new ViewHolder (LayoutInflater.from (parent.getContext ()).inflate (inflater, parent, false));
}
#Override
public void onBindViewHolder (final ViewHolder holder, int position) {
Item item = items.get (position);
if (holder.icon != null)
holder.icon.setImageDrawable (item.icon);
holder.title.setText (item.getItem (0));
holder.descr.setText (item.getItem (1));
// Other actions...
}
}
So, is it really to do it? I don't want to use a standart Android Settings activity, because I want to support KitKat (4.4), but its default themes are awful(
Or perhaps it'll be a solution with TableLayout and so on?
Thanks in advance!
You using your parent layout height was using like this android:layout_height="wrap_content"
User your height android:layout_width="match_parent"
definitely you got o/p
Change the 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:background="?android:attr/selectableItemBackground"
android:focusable="true"
android:gravity="center"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingTop="5dp">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/colorPrimary"
android:textSize="16sp"
android:text="abababababababababbabababababababab"
android:textStyle="bold" />
<TextView
android:id="#+id/descr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="abababababababababbababababababababsdhsjdhsadsahdkjsahdsadsadhsadhsauidhsauidhsauihsauihsaduisahduisahduisahduisahdusaidhsauidhsauidhsauiidhsauidhsauidhsauidhsauidhsaudhsauidhsadiusahduisahdusahu"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:layout_marginTop="7dp"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:theme="#style/checkBoxStyle" />
</LinearLayout>

How to set Visibility of any view inside listview row from button outside the listview

I have edit button on toolbar on click this button i want to show new view that is cross button inside every row of listview. is this possible and if yes how ? Thanks in advance. Below is layout files:
main layout:
<?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"
android:orientation="vertical"
android:background="#color/backgroundColor"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:background="#color/TabColor"
android:weightSum="10"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_weight="1"
android:onClick="back2"
android:layout_gravity="center"
app:srcCompat="#drawable/back_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_weight="7"
android:paddingLeft="#dimen/margin_small"
android:text="List OF Hotels"
android:textSize="#dimen/text_title"
android:textColor="#color/WhiteColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<FrameLayout
android:layout_weight="2"
android:background="#drawable/transparent_bg"
android:layout_marginRight="#dimen/margin_large"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/edit_jobs"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_marginBottom="#dimen/margin_small"
android:layout_marginTop="#dimen/margin_small"
android:text="Edit"
android:textColor="#color/WhiteColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:visibility="invisible"
android:id="#+id/done_editing"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_marginBottom="#dimen/margin_small"
android:layout_marginTop="#dimen/margin_small"
android:text="Done"
android:textColor="#color/WhiteColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
<ListView
android:id="#+id/listview1"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
listview row layout where i have to show cross image:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_marginTop="#dimen/margin_large"
android:layout_marginLeft="#dimen/margin_large"
android:layout_marginRight="#dimen/margin_large"
android:background="#drawable/border"
android:padding="#dimen/margin_large"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:weightSum="10"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text=""
android:layout_gravity="center"
android:layout_weight="9"
android:textColor="#color/TabColor"
android:singleLine="true"
android:textSize="#dimen/text_large"
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<FrameLayout
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:gravity="right"
android:id="#+id/arrow"
android:src="#drawable/rightarrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:visibility="invisible"
android:id="#+id/cross"
android:gravity="right"
android:src="#drawable/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:weightSum="10"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text=""
android:layout_weight="5"
android:textColor="#color/blackColor"
android:textSize="#dimen/text_medium"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_weight="5"
android:text=""
android:gravity="right"
android:textColor="#color/blackColor"
android:textSize="#dimen/text_medium"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Adapter class:
public class MyAdapter extends ArrayAdapter<String> {
private final Context context;
private ArrayList<String> titlelist,datalist;
public MyAdapter(Context context, ArrayList<String> title, ArrayList<String> data) {
super(context, R.layout.listview_row, title);
titlelist= new ArrayList<>();
datalist= new ArrayList<>();
this.context=context;
this.titlelist=title;
this.datalist=data;
}
private static class ViewHolder {
TextView t1,t2;
}
#Override
public int getCount() {
return titlelist.size();
}
#Override
public String getItem(int position) {
return titlelist.get(position);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.listview_row, null);
holder = new ViewHolder();
holder.t1 = (TextView) convertView.findViewById(R.id.title);
holder.t2 = (TextView) convertView.findViewById(R.id.data);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.t1.setText(titlelist.get(position));
holder.t2.setText(datalist.get(position));
return convertView;
}
}
you have to add this functionality in your adapter,
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//ur other codes.....
//
//
if(crossButtonPress){
holder.imageView.setImageResource(R.drawable.cross);
}else{
holder.imageView.setImageResource(R.drawable.default_ic);
}
}
In addition, in you actual button's onClick,
crossButtonPress = true;
adapter.notifyDatasetChanged();

Increment the value of TextView of child of particular Parent

This is the getChildView inside the Adapter class...
public View getChildView(int parent, int child, boolean lastChild, View view, ViewGroup viewGroup) {
ArrayList<String> itemDetails= (ArrayList<String>)getChild(parent,child);
if(view==null)
{
LayoutInflater inflater= (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view= inflater.inflate(R.layout.child_layout,viewGroup,false);
}
TextView textView =(TextView)view.findViewById(R.id.textView2);
textView.setText(itemDetails.get(0));
TextView textView1(TextView)view.findViewById(R.id.itemdescription);
textView1.setText(itemDetails.get(1));
TextView textView3= (TextView)view.findViewById(R.id.price);
textView3.setText(itemDetails.get(2));
final TextView textView2 = (TextView)view.findViewById(R.id.counterTextView);
ImageButton add = (ImageButton)view.findViewById(R.id.imageButton);
final ImageButton remove = (ImageButton)view.findViewById(R.id.imageButton1);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textView2.setText(String.valueOf(Integer.parseInt(textView2.getText().toString())+1));
remove.setVisibility(View.VISIBLE);
}
});
remove.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(Integer.parseInt(textView2.getText().toString())==0)
{
remove.setVisibility(View.INVISIBLE);
}
else {
textView2.setText(String.valueOf(Integer.parseInt(textView2.getText().toString()) - 1));
}
}
});
return view;
}
This is the ChildLayout:
<?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"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
android:background="#FFFFFF">
<ImageView
android:layout_width="match_parent"
android:layout_height="150dp"
android:scaleType="centerCrop"
app:srcCompat="#drawable/k_meals_rs"
android:id="#+id/imageView6" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:textSize="20sp"
android:textColor="#000000"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingRight="10dp">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/ic_remove_black"
android:id="#+id/imageButton1"
android:background="#drawable/round_button"
android:visibility="invisible"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:gravity="center_vertical"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:textColor="#000000"
android:id="#+id/counterTextView" />
<ImageButton
app:srcCompat="#drawable/ic_add_black"
android:id="#+id/imageButton"
android:background="#drawable/round_button"
android:layout_width="29dp"
android:layout_height="29dp" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="10dp">
<TextView
android:id="#+id/itemdescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:maxEms="15"
android:textColor="#000000"/>
<TextView
android:id="#+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="₹150"
android:textColor="#000000"/>
</RelativeLayout>
</LinearLayout>
i want to increment & decrements the value of counterTextView as the the + and - buttons are pressed.. but as i do so it increment and decrements the value of counterTextView in other child in other parents with same index value...any idea how to overcome this problem.. thnx

Button inside ListView not clickable

I want to be able to click on a button inside an item of a ListView. It should have a different effect from clicking the whole item. I realize there are several questions asked on stackoverflow, but none of the suggestions works for me.
The ListView is inside a Fragment.
Layout of the fragment:
<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=".EventFragment" >
<ListView
android:id="#+id/event_list"
android:background="#C0FFFFFF"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp" />
</RelativeLayout>
Layout of each list item:
<?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="vertical"
android:background="#C0101010">
<TextView
android:id="#+id/event_list_separator"
style="?android:attr/listSeparatorTextViewStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="separator"
android:textColor="#android:color/white" />
<LinearLayout
android:id="#+id/event_list_element"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF"
android:padding="6dip" >
<ImageView
android:id="#+id/event_list_element_icon"
android:layout_width="26dip"
android:layout_height="60dip"
android:layout_marginRight="6dip"
android:contentDescription="TODO" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/event_list_element_firstLine"
android:layout_width="match_parent"
android:layout_height="25dip"
android:text="item_header"
android:textSize="18sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="35dip"
android:orientation="horizontal" >
<TextView
android:id="#+id/event_list_element_secondLine"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:ellipsize="marquee"
android:singleLine="true"
android:text="Description"
android:textSize="14sp" />
<Button
android:id="#+id/event_list_element_button_1"
android:layout_width="132dip"
android:layout_height="match_parent"
android:drawableLeft="#drawable/ic_button1"
android:text="Participate"
android:textStyle="bold"
android:textSize="14sp"
/>
<Button
android:id="#+id/event_list_element_button_2"
android:layout_width="110dip"
android:layout_height="match_parent"
android:ellipsize="marquee"
android:drawableLeft="#drawable/ic_button2"
android:singleLine="true"
android:text="No thanks"
android:textStyle="bold"
android:gravity="center_vertical"
android:textSize="14sp"
/>
<TextView
android:id="#+id/event_list_element_additional_text"
android:layout_width="100dip"
android:layout_height="match_parent"
android:ellipsize="marquee"
android:singleLine="true"
android:gravity="center_vertical"
android:text="sample"
android:textStyle="bold"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
My list adapter is:
public class EventAdapter extends ArrayAdapter<Event> {
static class ViewHolder {
TextView separator;
LinearLayout relativeLayout;
TextView eventHeader;
TextView eventDescription;
ImageView blueDot;
Button button1;
Button button2;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater)
_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.event_list_item, parent, false);
viewHolder = new ViewHolder();
viewHolder.relativeLayout = (LinearLayout) convertView.findViewById(R.id.event_list_element);
viewHolder.blueDot = (ImageView) convertView.findViewById(R.id.event_list_element_icon);
viewHolder.eventHeader = (TextView) convertView.findViewById(R.id.event_list_element_firstLine);
viewHolder.eventDescription = (TextView) convertView.findViewById(R.id.event_list_element_secondLine);
viewHolder.button1 = (Button) convertView.findViewById(R.id.event_list_element_button1);
viewHolder.button2 = (Button) convertView.findViewById(R.id.event_list_element_button2);
viewHolder.separator = (TextView) convertView.findViewById(R.id.event_list_separator);
convertView.setTag(viewHolder);
} else{
viewHolder = (ViewHolder) convertView.getTag();
}
final Event item = getItem(position);
if (item != null) {
OnClickListener listener = new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(_context, "boo!", Toast.LENGTH_SHORT).show();
}
};
viewHolder.button1.setOnClickListener(listener);
}
return convertView;
}
}
The problem is that the two buttons are not clickable. What I tried to far:
ListView listView = (ListView) rootView.findViewById(R.id.event_list);
listView.setItemsCanFocus(true);
I also tried setting on the button:
android:focusable="true"
android:clickable="true"
I also experimented with android:descendantFocusability.
None of my tries made the buttons clickable.
Insert the attribute android:descendantFocusability="blocksDescendants" in the Parent Layout declaration of each list item.
The xml should be as follows:
<?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="vertical"
android:descendantFocusability="blocksDescendants"
android:background="#C0101010">
<TextView
android:id="#+id/event_list_separator"
style="?android:attr/listSeparatorTextViewStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="separator"
android:textColor="#android:color/white" />
<LinearLayout
android:id="#+id/event_list_element"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF"
android:padding="6dip" >
<ImageView
android:id="#+id/event_list_element_icon"
android:layout_width="26dip"
android:layout_height="60dip"
android:layout_marginRight="6dip"
android:contentDescription="TODO" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/event_list_element_firstLine"
android:layout_width="match_parent"
android:layout_height="25dip"
android:text="item_header"
android:textSize="18sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="35dip"
android:orientation="horizontal" >
<TextView
android:id="#+id/event_list_element_secondLine"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:ellipsize="marquee"
android:singleLine="true"
android:text="Description"
android:textSize="14sp" />
<Button
android:id="#+id/event_list_element_button_1"
android:layout_width="132dip"
android:layout_height="match_parent"
android:drawableLeft="#drawable/ic_button1"
android:text="Participate"
android:textStyle="bold"
android:textSize="14sp"
/>
<Button
android:id="#+id/event_list_element_button_2"
android:layout_width="110dip"
android:layout_height="match_parent"
android:ellipsize="marquee"
android:drawableLeft="#drawable/ic_button2"
android:singleLine="true"
android:text="No thanks"
android:textStyle="bold"
android:gravity="center_vertical"
android:textSize="14sp"
/>
<TextView
android:id="#+id/event_list_element_additional_text"
android:layout_width="100dip"
android:layout_height="match_parent"
android:ellipsize="marquee"
android:singleLine="true"
android:gravity="center_vertical"
android:text="sample"
android:textStyle="bold"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
I found the solution! I wanted to update the list of events periodically (right now it's a thread running every x milliseconds, later I want to switch that to only update the event list when there is a change). Anyway, the code was (inside my main activity):
private BroadcastReceiver _bReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(RECEIVE_EVENT)) {
Bundle bundle = intent.getExtras();
Event event = (Event) bundle.get("event");
showEvent(event);
}
}
};
private void showEvent(final Event event){
final Context context = this;
runOnUiThread(new Runnable() {
public void run() {
final ListView listview = (ListView) findViewById(R.id.event_list);
EventAdapter adapter = new EventAdapter(context, id.event_list, getEventList());
listview.setAdapter(adapter);
}
});
}
Setting the adapter each time is certainly not the right approach. Once I changed that to only set the adapter once, it worked like suggested using android:descendantFocusability="blocksDescendants"

Categories

Resources