SmoothScroller in RecyclerView - java

I have following Layout :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fafafa"
android:orientation="vertical"
android:paddingBottom="16dp"
android:paddingEnd="16dp"
android:paddingLeft="16dp"
android:paddingStart="16dp">
<View
android:id="#+id/item_divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="16dp"
android:background="#D3D3D3" />
<TextView
android:id="#+id/item_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="22sp"
tools:text="Léon: The Professional" />
<LinearLayout
android:id="#+id/sub_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:orientation="vertical">
<TextView
android:id="#+id/sub_item_genre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
tools:text="Genre: Crime, Drama, Thriller" />
<TextView
android:id="#+id/sub_item_year"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
tools:text="Year: 1993" />
</LinearLayout>
</LinearLayout>
In my recyclerview adapter:
#Override
public void onBindViewHolder(RecViewHolder holder, int position) {
Movie movie = list.get(position);
boolean expanded = movie.isExpanded();
subItem.setVisibility(expanded ? View.VISIBLE : View.GONE);
holder.title.setOnClickListener(v -> {
boolean expanded = movie.isExpanded();
movie.setExpanded(!expanded);
notifyItemChanged(position);
});
}
As you can see in the code when you click on an item, subItem get hidden/shown.
What I want to achieve is : when you click on last items in the list, I want it to display whole row (include sub item). At the current moment it changes visibility to VISIBLE, but subItem of the row is invisible to user until user scrolls to it.
In the following picture God Father is last visible item. How to display whole row includes God Father as title and sub item when user clicks ?

If you scroll to the bottom of RecyclerView expanded item, the problem will be solved. The key point is using LinearSmoothScroller for scrolling. Try below code:
public class RecAdapter extends RecyclerView.Adapter<RecAdapter.RecViewHolder> {
private List<Movie> list;
private RecyclerView mRecyclerView;
private RecyclerView.SmoothScroller mSmoothScroller;
public RecAdapter(List<Movie> list) {
this.list = list;
}
#Override
public RecViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater
.from(parent.getContext())
.inflate(R.layout.item_movie, parent, false);
return new RecViewHolder(view);
}
#Override
public void onBindViewHolder(RecViewHolder holder, int position) {
Movie movie = list.get(position);
holder.bind(movie);
holder.itemView.setOnClickListener(v -> {
boolean expanded = movie.isExpanded();
movie.setExpanded(!expanded);
notifyItemChanged(position);
if (movie.isExpanded()) {
mSmoothScroller.setTargetPosition(position);
new Handler().postDelayed(() -> mRecyclerView.getLayoutManager().startSmoothScroll(mSmoothScroller), 100);
}
});
}
#Override
public int getItemCount() {
return list == null ? 0 : list.size();
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
mRecyclerView = recyclerView;
mSmoothScroller = new LinearSmoothScroller(recyclerView.getContext()) {
#Override
protected int getVerticalSnapPreference() {
return LinearSmoothScroller.SNAP_TO_ANY;
}
#Override
protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
return 150f / displayMetrics.densityDpi;
}
};
}
public class RecViewHolder extends RecyclerView.ViewHolder {
private TextView title;
private TextView genre;
private TextView year;
private View subItem;
public RecViewHolder(View itemView) {
super(itemView);
title = itemView.findViewById(R.id.item_title);
genre = itemView.findViewById(R.id.sub_item_genre);
year = itemView.findViewById(R.id.sub_item_year);
subItem = itemView.findViewById(R.id.sub_item);
}
private void bind(Movie movie) {
boolean expanded = movie.isExpanded();
subItem.setVisibility(expanded ? View.VISIBLE : View.GONE);
title.setText(movie.getTitle());
genre.setText("Genre: " + movie.getGenre());
year.setText("Year: " + movie.getYear());
}
}
}
Here is the result:

Related

How to reference a view inside an element from my ListView?

So I have created a custom adapter for my ListView with three Views - TextView, ImageView and a basic View:
<?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:id="#+id/AllNotesFragment">
<TextView
android:id="#+id/addNoteTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="30dp"
android:layout_marginTop="25dp"
android:textSize="18sp"
android:gravity="center"
android:fontFamily="#font/ukij_qolyazma"
android:text="+new"
/>
<ImageButton
android:id="#+id/deleteNoteImageButton"
android:layout_width="50dp"
android:layout_height="70dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="0dp"
android:layout_marginEnd="30dp"
android:textSize="24sp"
android:gravity="center"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:fontFamily="#font/ukij_qolyazma"
/>
<View
android:id="#+id/underlineView"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginHorizontal="30dp"
android:layout_marginTop="70dp"
android:background="#color/colorMainDark" />
<ListView
android:id="#+id/notesListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginTop="75dp"
android:layout_marginBottom="0dp">
</ListView>
I have created and instantiated the ListView
notesListView = view.findViewById(R.id.notesListView);
And filled it with a bunch of my notes
notes = (ArrayList<Note>) db.getAllNotesForDay(NotesForDayActivity.getRememberDay(),
NotesForDayActivity.getRememberMonth(),
NotesForDayActivity.getRememberYear());
for (Note note : notes) {
noteTitles.add(note.getTitle());
}
NotesListAdapter adapter = new NotesListAdapter(((NotesForDayActivity) getActivity()).getContext(), notes);
notesListView.setAdapter(adapter);
With my custom adapter:
public class NotesListAdapter extends BaseAdapter {
private static final String TAG = "NotesListAdapter";
public static Context context;
private RelativeLayout notesListRelativeLayout;
private TextView noteTitleTextView;
private ImageView tickImage;
private View underlineView;
private List<Note> notes;
// !
private View listItemsView;
public NotesListAdapter(Context context, List<Note> notes) {
this.notes = notes;
this.context = context;
}
#Override
public int getCount() {
return NotesForDayActivity.getCountNotes();
}
#Override
public Object getItem(int position) {
return notes.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
listItemsView = convertView;
if (listItemsView == null) {
listItemsView = LayoutInflater.from(NotesForDayActivity.context).inflate(R.layout.notes_list_layout, null);
}
underlineView = listItemsView.findViewById(R.id.underlineView);
notesListRelativeLayout = (RelativeLayout) listItemsView.findViewById(R.id.notesListRelativeLayout);
noteTitleTextView = (TextView) listItemsView.findViewById(R.id.noteTitleTextView);
tickImage = (ImageView) listItemsView.findViewById(R.id.tickImageView);
noteTitleTextView.setText(notes.get(position).getTitle());
return listItemsView;
}
I can access an item inside my ListView with a OnItemClickListener, but I do not know how to access a View inside that particular Item of my ListView.
So If I set my OnClick like this:
notesListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
How can I reference one of the three Views in that item. For example I need to set the visibility of my imageview:
Any help is appreciated.
Try this, OnItemClickListener will return the view, so that you can get access through that view
notesListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ImageView tickImage = view.findViewById(R.id. tickImageView);
if(tickImage!=null){
tickImage.setVisibility(View.GONE);
}
}
});

RecyclerView - I can't add more than 1 line

I'm with a problem to add more than 1 line(item) in my RecyclerView, the item overwrites the previous one when I tap the button, but if I create a List<> with data in hardcoded on onCreate, It works adding more than 1 line in the RecyclerView. Follow the code:
ListChecklistAdapter
public class ListChecklistAdapter extends RecyclerView.Adapter<ListChecklistAdapter.ListChecklistViewHolder> {
private List<Checklist> mChecklist;
private Context mCtx;
public ListChecklistAdapter(Context ctx, List<Checklist> checklists) {
mCtx = ctx;
mChecklist = checklists;
}
#Override
public ListChecklistViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mCtx).inflate(R.layout.item_checklist, parent, false);
return new ListChecklistViewHolder(view);
}
#Override
public void onBindViewHolder(ListChecklistViewHolder holder, int position) {
holder.cbItem.setText(mChecklist.get(position).getDescricao());
holder.cbItem.setChecked(mChecklist.get(position).isCheckado());
}
#Override
public int getItemCount() {
return mChecklist.size();
}
public class ListChecklistViewHolder extends RecyclerView.ViewHolder {
#BindView(R.id.item_checkbox)
CheckBox cbItem;
public ListChecklistViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
}
}
public void refreshData(List<Checklist> item) {
this.mChecklist.clear();
this.mChecklist.addAll(item);
notifyDataSetChanged();
}
}
item_checklist
<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="wrap_content">
<CheckBox
android:id="#+id/item_checkbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:paddingLeft="6dp"
android:text=" "
tools:text="Exemplo de item de lista" />
</RelativeLayout>
MainActivity
public class MainActivity extends AppCompatActivity {
#BindView(R.id.rcv_lista)
RecyclerView rvLista;
int valor;
private List<Checklist> lista;
private ListChecklistAdapter listChecklistAdapter;
#BindView(R.id.btn_add_item)
Button btnAdd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
private int cont() {
return valor++;
}
public void novoItem(View view) {
lista = new ArrayList<>();
lista.add(new Checklist("Item " + (cont() + 1), true));
loadRecycler(lista);
}
private void loadRecycler(List<Checklist> lista) {
if (listChecklistAdapter == null) {
Log.i("LOG", "IF");
listChecklistAdapter = new ListChecklistAdapter(this, lista);
rvLista.setAdapter(listChecklistAdapter);
rvLista.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
rvLista.addItemDecoration(new DividerItemDecoration(this, 1));
return;
} else {
Log.i("LOG", "ELSE");
listChecklistAdapter.refreshData(lista);
listChecklistAdapter.onAttachedToRecyclerView(rvLista);
}
}
}
activity_main
<LinearLayout 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:orientation="vertical"
android:padding="16dp"
tools:context="teste.com.br.recyclercomcheckbox.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/rcv_lista"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn_add_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="5dp"
android:background="#android:color/transparent"
android:onClick="novoItem"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="+ novo item"
android:textColor="#009688" />
</LinearLayout>
Just remove this line this.mChecklist.clear(); from your refreshData() method.
public void refreshData(List<Checklist> item) {
this.mChecklist.addAll(item);
notifyDataSetChanged();
}
#Nilesh Rathod solved the problem! I just deleted this.mChecklist.clear(); from the ListCheckAdapter.java > refreshData()
Thanks for everyone to help too!

Show and hide listview

I want to do list view like this. What I want is when user touch 1st item, the 2nd item row appear. When user touch 3rd row item the previous item automatically hide again. How can i achieve this instead of using expandable listview? guide me in achieving this. Thanks in advance.
I'm sorry that i'm a bit busy, so i can't write the code right now. But below is the way which you can use to get what you want. I will add code when i will get time.
There is a parameter name "android:animateayoutchanges", apply it to the parent of your view which will be visible and invisible.
Save the position of the item which you have clicked and expanded.
When you click that item, Set the visibility of the view to visible.
When you again click on that item position, set that item's visibility to gone.
When you click some other item, then set the visibility of the stored item position to gone, set currently selected item's visibility to visible and store it's position.
That's it.
android:animateayoutchanges is used so that when you set the visibility to visible or gone and views move upward or downward, then this movement will have smooth animation.
I will update my answer with code later But you will get an idea from above steps.
UPDATE
public class ShippingAdapter extends RecyclerView.Adapter<ShippingAdapter.ViewHolder> {
private ArrayList<ShippingOptions> list;
private int expandedPosition = -1;
private int listSize = 0;
private int checkedPositon = -1;
private String currency;
public ShippingAdapter(ArrayList<ShippingOptions> list, String currency, AdapterInterface listener) {
this.list = list;
listSize = list.size();
this.currency = currency;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_shipping_options, parent, false);
// Layout in which you will add **android:animatelayoutchanges**
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
ShippingOptions item = list.get(position);
holder.name.setText(item.getName());
holder.fee.setText(utils.getFormattedCurrency(currency, Double.parseDouble(item.getShippingFee())));
holder.description.setText(item.getDescription());
holder.deliveryTime.setText(item.getDeliveryTime());
if (listSize == 1) {
list.get(position).setExpanded(true);
}
**if (list.get(position).isExpanded()) {
holder.expandableView.setVisibility(View.VISIBLE);
holder.checkBox.setVisibility(View.VISIBLE);
if (checkedPositon == position) {
holder.checkBox.setChecked(true);
} else {
holder.checkBox.setChecked(false);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
holder.parent.setElevation(12);
holder.parent.setTranslationZ(6);
holder.parent.setClipToPadding(false);
holder.parent.setClipToOutline(false);
}
} else {
holder.expandableView.setVisibility(View.GONE);
holder.checkBox.setVisibility(View.GONE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
holder.parent.setElevation(0);
holder.parent.setTranslationZ(0);
}
}**
**holder.parent.setOnClickListener(new View.OnClickListener() {
#TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
#Override
public void onClick(View v) {
if (listSize > 1) {
if (expandedPosition == position) {
if (list.get(position).isExpanded()) {
list.get(position).setExpanded(false);
} else {
list.get(position).setExpanded(true);
}
notifyItemChanged(position);
} else {
if (expandedPosition >= 0) {
list.get(expandedPosition).setExpanded(false);
notifyItemChanged(expandedPosition);
}
expandedPosition = position;
list.get(expandedPosition).setExpanded(true);
notifyItemChanged(expandedPosition);
}
}
}
});**
}
#Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView name, fee, description, deliveryTime;
public LinearLayout parent, expandableView;
public CheckBox checkBox;
public ViewHolder(View v) {
super(v);
name = (TextView) v.findViewById(R.id.tv_shipping_name);
fee = (TextView) v.findViewById(R.id.tv_shipping_fee);
description = (TextView) v.findViewById(R.id.tv_description);
deliveryTime = (TextView) v.findViewById(R.id.tv_delivery_time);
parent = (LinearLayout) v.findViewById(R.id.parent);
expandableView = (LinearLayout) v.findViewById(R.id.expandable_view);
checkBox = (CheckBox) v.findViewById(R.id.checkbox);
}
}
}
list_shipping_options.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:background="#color/white"
android:orientation="vertical"
android:padding="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="?listPreferredItemHeightSmall"
android:gravity="center_vertical">
<TextView
android:id="#+id/tv_shipping_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#style/text_medium"
android:textColor="#color/colorPrimary" />
<CheckBox
android:id="#+id/checkbox"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentEnd="true"
android:background="?android:attr/listChoiceIndicatorMultiple"
android:button="#null"
android:checked="false"
android:visibility="visible" />
</RelativeLayout>
<LinearLayout
android:id="#+id/expandable_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="visible">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingTop="10dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/label_shipping_fee" />
<TextView
android:id="#+id/tv_shipping_fee"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingTop="10dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/lable_description" />
<TextView
android:id="#+id/tv_description"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingTop="10dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/delivery_time" />
<TextView
android:id="#+id/tv_delivery_time"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
ShippingOptions.class
public class ShippingOptions implements Serializable {
private String id;
private String name;
private String shippingFee;
private String description;
private String deliveryTime;
private boolean isExpanded;
private boolean isChecked;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getShippingFee() {
return shippingFee;
}
public void setShippingFee(String shippingFee) {
this.shippingFee = shippingFee;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getDeliveryTime() {
return deliveryTime;
}
public void setDeliveryTime(String deliveryTime) {
this.deliveryTime = deliveryTime;
}
public boolean isExpanded() {
return isExpanded;
}
public void setExpanded(boolean expanded) {
isExpanded = expanded;
}
public boolean isChecked() {
return isChecked;
}
public void setChecked(boolean checked) {
isChecked = checked;
}
}

How do I use the swipe gesture to remove cards within a recycleview?

How do I use the swipe gesture to remove cards from my recycle-view in the same way that it is done in Google-now etc. So far I've created the cardview application but it's removing cards via a swipe gesture which I'm having problems with. I haven't found a single tutorial or question answered on this website which could help.
Any help would be very much appreciated. My code is below.
MyActivity
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_my);
setContentView(R.layout.activity_my);
RecyclerView recList = (RecyclerView) findViewById(R.id.cardList);
recList.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
recList.setLayoutManager(llm);
ContactAdapter ca = new ContactAdapter(createList(30));
recList.setAdapter(ca);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private List<ContactInfo> createList(int size) {
List<ContactInfo> result = new ArrayList<ContactInfo>();
for (int i=1; i <= size; i++) {
ContactInfo ci = new ContactInfo();
ci.name = ContactInfo.NAME_PREFIX + i;
ci.surname = ContactInfo.SURNAME_PREFIX + i;
ci.email = ContactInfo.EMAIL_PREFIX + i + "#test.com";
result.add(ci);
}
return result;
}
}
ContactInfo.java
public class ContactInfo {
protected String name;
protected String surname;
protected String email;
protected static final String NAME_PREFIX = "Name_";
protected static final String SURNAME_PREFIX = "Surname_";
protected static final String EMAIL_PREFIX = "email_";
}
ContactAdapter.java
public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.ContactViewHolder> {
private List<ContactInfo> contactList;
public ContactAdapter(List<ContactInfo> contactList) {
this.contactList = contactList;
}
#Override
public int getItemCount() {
return contactList.size();
}
#Override
public void onBindViewHolder(ContactViewHolder contactViewHolder, int i) {
ContactInfo ci = contactList.get(i);
contactViewHolder.vName.setText(ci.name);
contactViewHolder.vSurname.setText(ci.surname);
contactViewHolder.vEmail.setText(ci.email);
contactViewHolder.vTitle.setText(ci.name + " " + ci.surname);
}
#Override
public ContactViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.
from(viewGroup.getContext()).
inflate(R.layout.card_layout, viewGroup, false);
return new ContactViewHolder(itemView);
}
public static class ContactViewHolder extends RecyclerView.ViewHolder {
protected TextView vName;
protected TextView vSurname;
protected TextView vEmail;
protected TextView vTitle;
public ContactViewHolder(View v) {
super(v);
vName = (TextView) v.findViewById(R.id.txtName);
vSurname = (TextView) v.findViewById(R.id.txtSurname);
vEmail = (TextView) v.findViewById(R.id.txtEmail);
vTitle = (TextView) v.findViewById(R.id.title);
}
}
}
activity_my.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MyActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/cardList"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
card_layout.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_width="match_parent"
android:layout_height="match_parent"
card_view:cardCornerRadius="4dp"
android:layout_margin="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="20dp"
android:background="#color/bkg_card"
android:text="contact det"
android:gravity="center_vertical"
android:textColor="#android:color/white"
android:textSize="14dp"/>
<TextView
android:id="#+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:gravity="center_vertical"
android:textSize="10dp"
android:layout_below="#id/title"
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp"/>
<TextView
android:id="#+id/txtSurname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Surname"
android:gravity="center_vertical"
android:textSize="10dp"
android:layout_below="#id/txtName"
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp"
/>
<TextView
android:id="#+id/txtEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email"
android:textSize="10dp"
android:layout_marginTop="10dp"
android:layout_alignParentRight="true"
android:layout_marginRight="150dp"
android:layout_alignBaseline="#id/txtName"/>
<TextView
android:id="#+id/txtAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address"
android:textSize="10dp"
android:layout_alignStart="#id/txtEmail"
android:layout_alignBaseline="#id/txtSurname"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
You can use below code (ItemTouchHelper.SimpleCallback from Android support V7) to remove the cards from RecyclerView using swipe gesture
ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
#Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
#Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {
contacts.remove(viewHolder.getAdapterPosition());
ca.notifyItemRemoved(viewHolder.getAdapterPosition());
}
#Override
public void onMoved(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, int fromPos, RecyclerView.ViewHolder target, int toPos, int x, int y) {
super.onMoved(recyclerView, viewHolder, fromPos, target, toPos, x, y);
}
};
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
itemTouchHelper.attachToRecyclerView(recList);

Gridview adapter images with title name for images group

I have a gridView witch contains a group of images with two columns, what I want to do is to put a title for a group of images.
Now I can do that but the problem that title is that the title is only in one column and the second column is always empty, how can I put the title in the two columns?
Here is my adapter:
public class MultimediaPhotosAdapter extends BaseAdapter {
private Context mContext;
private List<ImagesDto> imagesDto = new ArrayList<ImagesDto>();
ImagesFlickrDto imagesFlickrDto;
final String formatImage = ImagesNameFormat.FORMAT_IMAGE_DEFAULT.getImagesNameFormat();
ImagesFormatsDto currentImageFormatToDisplay;
GridView gridViewImages;
public MultimediaPhotosAdapter(Context context, GridView gridViewImages) {
this.gridViewImages = gridViewImages;
this.mContext = context;
}
#Override
public int getCount() {
return imagesDto.size();
}
#Override
public Object getItem(int position) {
return imagesDto.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View itemView = null;
HolderElementGridView holderElementGridView;
ImagesDto currentImage = imagesDto.get(position);
if(convertView == null) {
holderElementGridView = new HolderElementGridView();
itemView = View.inflate(mContext,R.layout.item_multimedia_photo,null);
holderElementGridView.image = (NetworkImageView) itemView.findViewById(R.id.imageView);
holderElementGridView.title = (TextView) itemView.findViewById(R.id.title);
itemView.setTag(holderElementGridView);
}
else {
itemView = convertView;
holderElementGridView = (HolderElementGridView)itemView.getTag();
}
if(imagesDto.get(position).isFirstElementOfCategorie()){
holderElementGridView.image.setVisibility(View.GONE);
holderElementGridView.title.setVisibility(View.VISIBLE);
holderElementGridView.title.setText(currentImage.getTitle());
}
else if(imagesDto.get(position).getUrl()!=null){
holderElementGridView.image.setVisibility(View.VISIBLE);
holderElementGridView.title.setVisibility(View.GONE);
holderElementGridView.image.setImageUrl(StringFormat.getUrlImage(mContext, currentImage.getUrl(), currentImageFormatToDisplay.getSuffix(), currentImageFormatToDisplay.getExtension()),VolleySingleton.getInstance(mContext).getImageLoader());
holderElementGridView.image.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
DrawerLayoutInterface screenInterface=(DrawerLayoutInterface)mContext;
if(screenInterface!=null)
screenInterface.showDiaporama(imagesFlickrDto,position);
}
});
}
// column near title is empty
else{
holderElementGridView.image.setVisibility(View.VISIBLE);
holderElementGridView.title.setVisibility(View.GONE);
}
return itemView;
}
public void update(ImagesFlickrDto imagesFlickrDto){
this.imagesFlickrDto = imagesFlickrDto;
this.imagesDto = imagesFlickrDto.getListImages();
AdministrerImagesSA administrerImages = new AdministrerImagesSAImpl();
currentImageFormatToDisplay = administrerImages.getFormatByProriete(imagesFlickrDto.getImagesFormatsDto(), formatImage);
}
class HolderElementGridView{
NetworkImageView image;
TextView title;
}
}
item_multimedia_photo.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:geekui="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<com.netcosports.anderlecht.activity.utils.TypefaceTextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/text_size_item_menu"
android:visibility="gone"
android:layout_marginLeft="5dp"
geekui:customTypeface="fonts/DIN-Regular.otf" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.android.volley.toolbox.NetworkImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:src="#drawable/ic_launcher" >
</com.android.volley.toolbox.NetworkImageView>
</LinearLayout>
</LinearLayout>

Categories