Unconditional layout inflation from view adapter - android

I'm getting a warning like Unconditional layout inflation which is causing the layout to exit and app goes crashing.I need to inflate this layout without crashing.
the crashing is on the following code.
I have the code for the custom adapter set.
public class CustomAdapter extends BaseAdapter {
private List<Response.NodesEntity> mMovieitem;
private Context mContext;
private LayoutInflater inflater;
public CustomAdapter(Context mContext, List<Response.NodesEntity> mMovieitem) {
this.mContext = mContext;
this.mMovieitem = mMovieitem;
}
#Override
public int getCount() {
return mMovieitem.size();
}
#Override
public Object getItem(int position) {
return mMovieitem.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.s, parent, false);
Response.NodesEntity item = (Response.NodesEntity) getItem(position);
ImageView thumbnail = (ImageView) rowView.findViewById(R.id.thumbnail);
TextView title = (TextView) rowView.findViewById(R.id.title);
TextView rating = (TextView) rowView.findViewById(R.id.video_url);
String imageUrl = item.getVideoTumbnail().getSrc();
Picasso.with(mContext).load(imageUrl).into(thumbnail);
title.setText(item.getTitle());
rating.setText(item.getHlsVideo());
return rowView;
}
}
The Xml is in this format . I'm inflating this layout and
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="#+id/video_list"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="info.androidhive.materialdesign.activity.FriendsFragment">
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_margin="5dp">
<ImageView
android:id="#+id/thumbnail"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/background_material_light"
android:scaleType="centerCrop" />
<TextView
android:layout_marginBottom="10dp"
android:layout_gravity="bottom"
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/thumbnail"
android:layout_toEndOf="#+id/thumbnail"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:typeface="sans"
android:textSize="22sp"
android:textColor="#ffffff" />
<TextView
android:layout_marginTop="20dp"
android:id="#+id/video_url"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/title"
android:layout_toEndOf="#+id/thumbnail"
android:layout_toRightOf="#+id/thumbnail"
android:visibility="invisible"/>
</android.support.v7.widget.CardView>
</RelativeLayout>

use View holder method for inflating each rows

Related

Animate button inside custom adapter for listview

I made a ListView with a custom adapter with my own layout, and on this layout there is a text view, a button and three image buttons that are at first with setVisibility.GONE
I am trying to animate this button for when the user clicks on it, it changes its position from right to left to give space for the three image buttons.
The problem is, the only item in which the animation is working is the last item on the list. I want the animation to work in all of the items.
Here's the code for my adapter:
public class ListaAdapter extends BaseAdapter implements ListAdapter {
private ArrayList<String> list = new ArrayList<String>();
private Context context;
private ListView lista;
private Button btnAbrir;
private RelativeLayout relativeLayout;
private ObjectAnimator animation;
public ListaAdapter (ArrayList<String> list, Context context, ListView a) {
this.list = list;
this.context = context;
this.lista = a;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int pos) {
return list.get(pos);
}
#Override
public long getItemId(int pos) {
return 0;
//just return 0 if your list items do not have an Id variable.
}
#Override
public View getView(final int position, View convertView, ViewGroup
parent)
{
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.custom_list, null);
}
TextView listItemText = (TextView)view.findViewById(R.id.textoItem);
listItemText.setText(list.get(position));
final LinearLayout btnLayout = (LinearLayout)
view.findViewById(R.id.btnLayout);
relativeLayout = (RelativeLayout) view.findViewById(R.id.layout);
btnAbrir = (Button)view.findViewById(R.id.btnAbrir);
ImageButton btnNaoSei = (ImageButton)view.findViewById(R.id.btnNaoSei);
ImageButton btnAceitar =
(ImageButton)view.findViewById(R.id.btnAceitar);
ImageButton btnNegar = (ImageButton)view.findViewById(R.id.btnNegar);
animation = ObjectAnimator.ofFloat(btnAbrir,"x",200);
btnLayout.setVisibility(View.GONE);
btnAbrir.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
btnLayout.setVisibility(View.VISIBLE);
animate();
}
});
return view;
}
private void animate()
{
animation.setDuration(500);
animation.start();
}
}
And this is my custom_list.xml 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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:animateLayoutChanges="true">
<RelativeLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#color/white">
<LinearLayout
android:id="#+id/btnLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="#dimen/_32sdp"
android:layout_height="#dimen/_50sdp"
android:background="#color/compras_barra">
<ImageButton
android:id="#+id/btnNaoSei"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="false"
android:layout_alignParentStart="false"
android:layout_alignParentTop="false"
android:backgroundTint="#color/compras_barra"
android:contextClickable="false"
app:srcCompat="#drawable/a12" />
<TextView
android:id="#+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/btnNaoSei"
android:layout_centerHorizontal="true"
android:text="NÃO SEI"
android:textColor="#color/white"
android:textSize="8sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="#dimen/_32sdp"
android:layout_height="#dimen/_50sdp"
android:background="#color/compras">
<ImageButton
android:id="#+id/btnAceitar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="11dp"
android:background="#color/compras"
app:srcCompat="#drawable/a11" />
<TextView
android:id="#+id/textView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/btnAceitar"
android:paddingEnd="#dimen/_3sdp"
android:paddingTop="#dimen/_8sdp"
android:text="ACEITAR"
android:textColor="#color/white"
android:textSize="8sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="#dimen/_32sdp"
android:layout_height="#dimen/_50sdp"
android:background="#color/compras_texto2">
<ImageButton
android:id="#+id/btnNegar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#color/compras_texto2"
app:srcCompat="#drawable/a10" />
<TextView
android:id="#+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="12dp"
android:paddingBottom="#dimen/_1sdp"
android:text="NEGAR"
android:textColor="#color/white"
android:textSize="8sp" />
</RelativeLayout>
</LinearLayout>
<Button
android:id="#+id/btnAbrir"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#drawable/a09"
android:layout_alignTop="#+id/textoItem"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:id="#+id/textoItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/_10sdp"
android:layout_marginTop="#dimen/_10sdp"
android:maxWidth="200dp"
android:text="TextView"
android:textColor="#color/compras_texto2" />
</RelativeLayout>
Any idea on how to accomplish this? Thanks in advance.
The things you need to adopt the following method and create a ViewHolder so that your adapter will know each element of each row in the List
The last UI element of the row only animates because this the only last known id of components for your Adapater when he was creating the Views in your List. So it will animates for the last row elements
You have to add ViewHolder nested class inside your Adapter and declare your UI components. Use setTag and getTag methods inside the getView
Overall you have to create your adapter things like this in getView
public class SListAdapter extends ArrayAdapter<String> {
private Context context;
private String[] seCtnColors;
private List<Subscription> item;
private ViewHolder mainViewHolder = null;
private ViewHolder viewHolder;
private LayoutInflater inflater;
private View row;
public SListAdapter(Context c, List<Subscription> subscriptions)
{
super(c, R.layout.row,R.id.rowNameTV);
this.context=c;
this.item = subscriptions;
}
#Override
public int getCount() {
return this.item.size();
}
#Override
public View getView(final int position, final View convertView, ViewGroup parent) {
row = null;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.row,parent,false);
viewHolder = new ViewHolder();
initAdapterUI();
setAdapterDataset(position);
return row;
}
private void initAdapterUI() {
viewHolder.animatedGifViewHol = row.findViewById(R.id.row_animated_gif);
viewHolder.alertBarVerticalViewHol = row.findViewById(R.id.alertBarVerticalView);
viewHolder.firstNameTVHol = (TextView) row.findViewById(R.id.rowNameTV);
viewHolder.phoneNumberTVHol = (TextView) row.findViewById(R.id.rowNumberTV);
viewHolder.switchStateTVHol = (TextView) row.findViewById(R.id.switchStateTV);
row.setTag(viewHolder);
}
private void setAdapterDataset(int position) {
mainViewHolder = (ViewHolder) row.getTag();
mainViewHolder.alertBarVerticalViewHol.setBackgroundColor(Color.RED);
mainViewHolder.switchStateTVHol.setTextColor(Color.RED);
mainViewHolder.firstNameTVHol.setText(item.get(position).getFirstName());
mainViewHolder.phoneNumberTVHol.setText(item.get(position).getNumber());
}
public class ViewHolder{
View animatedGifViewHol;
View alertBarVerticalViewHol;
TextView firstNameTVHol;
TextView switchStateTVHol;
TextView phoneNumberTVHol;
}
}

Android ListView requires two fingers to scroll

I have a rather simple layout that I am intending on using for basic chat in a fragment, but scrolling through the ListView requires me to place one finger on the screen on an item in the ListView and then scroll up or down with another finger. I was under the impression that a ListView should only require a single finger/touch to be able to scroll. Is there something I am overlooking in my layout, adapter or even potentially the code I am using to populate the adapter with?
Chat Layout XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF" >
<ListView
android:id="#+id/msgListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/form"
android:divider="#null"
android:dividerHeight="0dp"
android:scrollbarSize="3dp"
android:scrollbarStyle="outsideOverlay"
android:scrollbars="vertical"
android:scrollingCache="true"
android:smoothScrollbar="true"
android:paddingBottom="10dp" />
<LinearLayout
android:id="#+id/form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#color/primary"
android:orientation="horizontal"
android:paddingBottom="2dp" >
<EditText
android:id="#+id/messageEditText"
android:layout_width="252dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/button1"
android:layout_toLeftOf="#+id/sendMessageButton"
android:layout_weight="0.72"
android:ems="10"
android:maxHeight="80dp" />
<ImageButton
android:id="#+id/sendMessageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#drawable/chat_send_button" />
</LinearLayout>
</RelativeLayout>
ListView Adapter
public class ChatMessageAdapter extends BaseAdapter {
private static LayoutInflater inflater = null;
ArrayList<ChatMessage> chatMessageList;
public ChatMessageAdapter(Activity activity, ArrayList<ChatMessage> list) {
chatMessageList = list;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return chatMessageList.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ChatMessage message = chatMessageList.get(position);
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.chat_bubble, null);
TextView msg = (TextView) vi.findViewById(R.id.message_text);
msg.setText(message.body);
TextView dateTime = (TextView) vi.findViewById(R.id.msgDateTime);
dateTime.setText(message.dateTime);
LinearLayout layout = (LinearLayout) vi
.findViewById(R.id.bubble_layout);
LinearLayout parent_layout = (LinearLayout) vi
.findViewById(R.id.bubble_layout_parent);
// if message is mine then align to right
if (message.isMine) {
layout.setBackgroundResource(R.drawable.bubble2);
parent_layout.setGravity(Gravity.RIGHT);
}
// If not mine then align to left
else {
layout.setBackgroundResource(R.drawable.bubble1);
parent_layout.setGravity(Gravity.LEFT);
}
msg.setTextColor(Color.BLACK);
return vi;
}
public void add(ChatMessage object) {
chatMessageList.add(object);
}
}
I had the same problem!
If you're using the navigation drawer, in the app_bar_main.xml change the CoordinatorLayout for the NestedScrollView
and then in your fragment in the scrollView set android:nestedScrollingEnabled="true"

Can't click on element in ListView Android

I am trying to create a custom list. My list is contained in a Fragment that correctly implements onScrollListener and populates the list using an adapter. The problem is that I cannot click on each item and I cannot figure out why. Here there is the code of my layout fragment
<?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"
android:background="#ffffff"
android:clickable="true">
<ListView
android:id="#+id/listNotification"
android:scrollbars="none"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:paddingStart="15dp"
android:paddingEnd="15dp"
android:clickable="true"
/>
</LinearLayout>
and here there is the code of my custom list
<?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"
android:background="#ffffff"
android:clickable="true">
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/imageNotification"
android:layout_width="60dp"
android:layout_height="60dp"
android:padding="5dp"
android:focusable="false"/>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textNotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:textColor="#33CC33"
android:focusable="false"/>
<TextView
android:id="#+id/idQuestion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:focusable="false"
/>
<TextView
android:id="#+id/typeNotification"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:visibility="gone"
android:focusable="false"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
here there is the code that creates my list using the adapter and setting the onclicklistener
adapter = new NotificationListAdapter(getActivity(), this.rows);
list = (ListView) firstAccessView.findViewById(R.id.listNotification);
list.setAdapter(adapter);
list.setOnScrollListener(this);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(adapter.getItem(position).getTypeNotification()==0) {
mNotificationInteface.readQuestion(adapter.getItem(position).getQuestionId());
}
if(adapter.getItem(position).getTypeNotification()==1){
mNotificationInteface.readAnswer(adapter.getItem(position).getQuestionId());
}
}
});
and here there is the code of my adapter
public class NotificationListAdapter extends ArrayAdapter<NotificationItem> {
private View view;
private final Activity context;
private List<NotificationItem> rows;
private int count = 1;
public NotificationListAdapter(Activity context, List<NotificationItem> firstRows ) {
super(context, R.layout.list_notifications, firstRows);
this.context = context;
this.rows = firstRows;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = context.getLayoutInflater();
view = inflater.inflate(R.layout.list_notifications, null);
view.setPadding(0,10,0,10);
holder = new ViewHolder();
holder.textNotification = (TextView) view.findViewById(R.id.textNotification);
holder.idQuestion = (TextView) view.findViewById(R.id.idQuestion);
holder.typeNotification = (TextView) view.findViewById(R.id.typeNotification);
holder.imageNotification = (ImageView) view.findViewById(R.id.imageNotification);
view.setTag(holder);
} else {
view=convertView;
holder = (ViewHolder) convertView.getTag();
}
int typeNotification = this.rows.get(position).getTypeNotification();
holder.textNotification.setTextColor(Color.BLACK);
holder.idQuestion.setText(String.valueOf(this.rows.get(position).getQuestionId()));
holder.typeNotification.setText(String.valueOf(this.rows.get(position).getTypeNotification()));
if(typeNotification==0){
holder.textNotification.setText(R.string.askQuestion);
holder.imageNotification.setImageResource(R.mipmap.iconuseranonymous);
}
if(typeNotification==1){
//nome da recuperare da con id notifica, quindi id utente quindi dome
holder.textNotification.setText(R.string.answerQuestion);
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").transform(new CircleTransform()).fit().centerCrop().into(holder.imageNotification);
}
if(typeNotification==2){
//nome e immagine da recuperare
holder.textNotification.setText(R.string.newFriend);
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").transform(new CircleTransform()).fit().centerCrop().into(holder.imageNotification);
}
return view;
}
#Override
public NotificationItem getItem(int position){
return this.rows.get(position);
}
#Override
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
static class ViewHolder {
ImageView imageNotification;
TextView textNotification;
TextView idQuestion;
TextView typeNotification;
int position;
}
Remove android:clickable="true" from the ListView and it's parent in your XML layout, and also from the root of your list item layout.

ListView with BaseAdapter and LinearLayout?

I'm trying create a BaseAdapter to my ListView. The problem is when I do create a LinearLayout inside other LinearLayout the listener OnItemClickListener doesn't works. If I put the components outside of LinearLayout works fine.
How could I do this works ?
ListView
<?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">
<ListView
android:id="#+id/lvEntregasPendente"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
></ListView>
</LinearLayout>
Adapter XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffe3b3"
android:layout_margin="5dp"
android:padding="2dp"
android:id="#+id/llEntregaPendenteVendas">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/tvVenda"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Venda"
android:textColor="#color/action_bar"
android:textStyle="bold"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Entrega em: "
android:textColor="#color/action_bar"
android:textStyle="bold"
android:layout_marginLeft="10dp"
/>
<TextView
android:id="#+id/tvDataEntrega"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Data"
android:textColor="#color/action_bar"
android:textStyle="bold"
android:layout_weight="1"
/>
<CheckBox
android:id="#+id/cbEntregue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/action_bar"
android:text="Entregue"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/tvAtrasoEntrega"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Atraso de: 20 dias"
android:textColor="#FF0000"
android:padding="5dp"
android:visibility="visible"
android:layout_weight="1"
android:gravity="right"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Adapter
public class EntregaPendenteListAdapter extends BaseAdapter {
private Context context;
private List<Venda> lista;
private DateControl dateControl;
private EntregaPendenteFrag rpf;
private Venda venda;
public EntregaPendenteListAdapter(Context context, List<Venda> lista, EntregaPendenteFrag rpf) {
this.context = context;
this.lista = lista;
this.rpf = rpf;
dateControl = new DateControl();
}
/** limpa a lista */
public void clearList(){
lista.clear();
notifyDataSetChanged();
}
/** altera lista */
public void changeList(List<Venda> lista){
this.lista = lista;
notifyDataSetChanged();
}
#Override
public int getCount() {
return lista.size();
}
#Override
public Object getItem(int position) {
return lista.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
Venda venda = lista.get(position);
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.entregas_pendente_adapter, parent, false);
holder.llEntregaPendenteVendas = (LinearLayout) convertView.findViewById(R.id.llEntregaPendenteVendas);
holder.tvVenda = (TextView) convertView.findViewById(R.id.tvVenda);
holder.tvDataEntrega = (TextView) convertView.findViewById(R.id.tvDataEntrega);
holder.tvAtrasoEntrega = (TextView) convertView.findViewById(R.id.tvAtrasoEntrega);
holder.cbEntregue = (CheckBox)convertView.findViewById(R.id.cbEntregue);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
holder.tvVenda.setText("Venda: " + FormataCodigo.getCodFormat(venda.getId()));
if(venda.getData_entrega() != null){
holder.tvDataEntrega.setText(new SimpleDateFormat("dd-MM-yyyy").format(venda.getData_entrega()));
if(dateControl.getDiasVencido(venda.getData_entrega()) > 0){
holder.tvAtrasoEntrega.setText("Atraso de: " + new DateControl().getDiasVencido(venda.getData_entrega()) + "dias");
holder.tvAtrasoEntrega.setVisibility(View.VISIBLE);
}
}
if((position % 2) == 0){
holder.llEntregaPendenteVendas.setBackgroundColor(Color.parseColor("#ffe3b3"));
}else{
holder.llEntregaPendenteVendas.setBackgroundColor(Color.WHITE);
}
return convertView;
}
private static class ViewHolder{
LinearLayout llEntregaPendenteVendas;
TextView tvVenda;
TextView tvDataEntrega;
TextView tvAtrasoEntrega;
CheckBox cbEntregue;
}
}
Activity
//listview
lvEntregasPendente = (ListView)view.findViewById(R.id.lvEntregasPendente);
lvEntregasPendente.setOnItemClickListener(this);
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i("ITEM->", position + "");
}
It seems your CheckBox is stealing the focus.
Try setting these properties on it:
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"

GridView breaks while scrolling

I am working on a custom launcher application. For this reason I created a custom GridView that displays all installed applications.
The problem is that when I scroll through the apps, the gridview has a weird behavior. Sometimes it will break the horizontal alignment because of a multiline application name. Sometimes the scrolling bounds go crazy and you end up scrolling outside the gridview.
Here is a video of the problems I describe above.
I am using the following code.
public class CustomGridView extends BaseAdapter{
private Context mContext;
private List<Application> app;
public CustomGridView(Context c, List<Application> app) {
super();
mContext = c;
this.app = app;
}
#Override
public int getCount() {
return app.size();
}
#Override
public Object getItem(int position) {
return app.get(position).appPkg;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder view;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.grid_item, null);
view = new ViewHolder();
view.appName = (TextView) convertView.findViewById(R.id.grid_text);
view.appIcon = (ImageView) convertView.findViewById(R.id.grid_image);
convertView.setTag(view);
} else {
view = (ViewHolder) convertView.getTag();
}
view.appName.setText(app.get(position).appName);
view.appIcon.setImageDrawable(app.get(position).appIcon);
return convertView;
}
static class ViewHolder
{
ImageView appIcon;
TextView appName;
}
}
For the grid_item I use the following.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp"
android:layout_marginBottom="10dp"
android:gravity="fill_horizontal|center_horizontal">
<ImageView
android:layout_height="62dp"
android:id="#+id/grid_image"
android:layout_width="62dp"
android:layout_gravity="center_horizontal"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/grid_text"
android:layout_marginTop="2dp"
android:textSize="12sp"
android:ellipsize="marquee"
android:gravity="center|center_horizontal" android:textAlignment="center"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
And here is the activity layout
<GridView
android:id="#+id/gridView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnWidth="80dp"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" android:layout_marginLeft="5dp" android:layout_marginRight="5dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true" android:layout_alignParentTop="true">
</GridView>
The problem with the height of TextView. So, make TextView single line like:
android:singleLine="true"
or, If u want 2 or 3 lines for some of the items, then make all the items have 2 or 3 lines like:
android:minLines="2"

Categories

Resources