Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
My goal is to display a list where each element (iteration) is displayed with two buttons:
The buttons would change a variable in the specific element of the list.
My problem: How do I create this gui in Android Studio?
Here I implement a simple example of RecyclerView in android (java). You can change it as you need:
Add item.xml in your layout folder, and write these lines in it:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/titleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/option2Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/option1Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toLeftOf="#+id/option2Button"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
this is based on your sample image. this layout contains all views in a single row of list. if you want more buttons or texts in each row, you must add them in this file.
You always need an Adapter class for RecyclerViews. So, add MyAdapter.java class to your project as below:
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
{
private Context context;
private List<String> titles;
MyAdapter(Context context, List<String> titles)
{
this.context = context;
this.titles = titles;
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType)
{
RecyclerView.ViewHolder viewHolder;
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
viewHolder = getViewHolder(parent, inflater);
return viewHolder;
}
private RecyclerView.ViewHolder getViewHolder(ViewGroup parent, LayoutInflater inflater)
{
RecyclerView.ViewHolder viewHolder;
View v1 = inflater.inflate(R.layout.item, parent, false);
viewHolder = new ItemVH(v1);
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position)
{
ItemVH itemVH = (ItemVH) holder;
// set title for each item:
itemVH.titleTextView.setText(titles.get(position));
itemVH.option1Button.setText("option 1");
itemVH.option2Button.setText("option 2");
// click listener for first button:
itemVH.option1Button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// option 1 button clicked
}
});
// click listener for second button:
itemVH.option2Button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// option 2 button clicked
}
});
}
#Override
public int getItemCount()
{
return titles.size();
}
// add item to list:
public void addItem(String title)
{
titles.add(title);
notifyItemInserted(titles.size() - 1);
}
// remove item from list:
public void remove(String title)
{
int position = titles.indexOf(title);
if (position > -1)
{
titles.remove(position);
notifyItemRemoved(position);
}
}
protected class ItemVH extends RecyclerView.ViewHolder
{
private TextView titleTextView;
private Button option1Button;
private Button option2Button;
public ItemVH(View itemView)
{
super(itemView);
titleTextView = itemView.findViewById(R.id.titleTextView);
option1Button = itemView.findViewById(R.id.option1Button);
option2Button = itemView.findViewById(R.id.option2Button);
}
}
}
The titles list is list of Strings shown on items. You create this list and send it to the MyAdapter class from MainActivity. all views in single item are find in ItemVH class, and you can work with these views in onBindViewHolder function.
In activity_main.xml add a RecyclerView:
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recyclerView"/>
In MainActivity.java add these lines in onCreate:
// create list:
List<String> titles = new ArrayList<>();
titles.add("first item");
titles.add("second item");
titles.add("third item");
// define the adapter:
MyAdapter adapter = new MyAdapter(this, titles);
// set the RecyclerView:
RecyclerView recyclerView = findViewById(R.id.recyclerView);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
Result:
Related
I have some doubts about how to solve the doubt about how to click on a RecyclerView and change image from another activity, such as MainActivity.
In the image you can see what I'm trying to do. I have a total of 6 images, 3 Small images and another 3 Large images.
In the footer of MainActivity I have the ReciclerView that loads the 3 small images, I want that when they click for example to the image_Small_2/item_Small_2, that replaces the image that is in the center by the image_Large_2/item_Large_2.
I don't know if I'm explaining myself well, I leave you a screenshot and the code to see if you can give me a hand. Thanks in advance. P.S. I use the glide library to load the images.
My Adapter RecyclerView
public class AdaptadorX extends RecyclerView.Adapter<AdaptadorX.ViewHolder> {
private ArrayList<Items> itemsLi;
private Context context;
public static class ViewHolder extends RecyclerView.ViewHolder{
public ImageView idSrcImagen;
public ViewHolder(#NonNull View itemView) {
super(itemView);
idSrcImagen = itemView.findViewById(R.id.idImagen);
}
}
public AdaptadorX(ArrayList<Items> itemsListado, Context context_L){
itemsLi = itemsListado;
context = context_L;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_item, parent, false);
ViewHolder content = new ViewHolder(view);
return content;
}
#Override
public void onBindViewHolder(#NonNull final ViewHolder holder, final int position) {
final Items contarItems = itemsLi.get(position);
Glide.with(context).load(contarItems.getxNombre_imagen()).into(holder.idSrcImagen);
holder.idSrcImagen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("Mensaje_AdaptadorX.java", "Mi Posicion FOTO es: "+String.valueOf(position));
}
});
}
#Override
public int getItemCount() {
Log.d("Mensaje_Size_Tamaño", String.valueOf(itemsLi.size()));
return itemsLi.size();
}
}
Class Items
public class Items {
private String xNombre_imagen;
public Items (String nombre_imagen_M){
xNombre_imagen = nombre_imagen_M;
}
public String getxNombre_imagen() {
return xNombre_imagen;
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
public ImageView idImgHead;
private ArrayList<Items> items;
private RecyclerView idRecyclerView;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
idRecyclerView = findViewById(R.id.idRecyclerView);
idImgHead = findViewById(R.id.idImgHead);
Glide.with(this).load("https://www.midominio.com/Imagen_GRANDE_head_01.jpg").into(idImgHead); //IMAGE BIG
listadoXhead();
}
private void listadoXhead() {
ArrayList<Items> items = new ArrayList<>();
items.add(new Items("https://www.midominio.com/Imagen_Pequeña_head_01.jpg")); //IMAGE Small
items.add(new Items("https://www.midominio.com/Imagen_Pequeña_head_02.jpg")); //IMAGE Small
items.add(new Items("https://www.midominio.com/Imagen_Pequeña_head_03.jpg")); //IMAGE Small
idRecyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(MainActivity.this);
((LinearLayoutManager) layoutManager).setOrientation(RecyclerView.HORIZONTAL);
adapter = new AdaptadorX(items, MainActivity.this);
idRecyclerView.setLayoutManager(layoutManager);
idRecyclerView.setAdapter(adapter);
}
}
activity_main
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="#+id/idImgHead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:scaleType="fitCenter"
app:layout_constraintBottom_toTopOf="#+id/linearLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#mipmap/ic_launcher" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/idRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
activity_item
<?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">
<ImageView
android:id="#+id/idImagen"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/ic_launcher_foreground"
android:onClick="accionBoton"/>
</LinearLayout>
Example Image Here
You need to implement an interface listener in your activity and pass it to your adapter. Add this to your adapter and pass it through the constructor
interface OnImageClickListener{
void onSelected(String url);
}
Your whole adapter should look like this.
public class AdaptadorX extends RecyclerView.Adapter<AdaptadorX.ViewHolder> {
private ArrayList<Items> itemsLi;
private Context context;
pruvate OnImageClickListener listener;
interface OnImageClickListener{
void onSelected(String url);
}
public static class ViewHolder extends RecyclerView.ViewHolder{
public ImageView idSrcImagen;
public ViewHolder(#NonNull View itemView) {
super(itemView);
idSrcImagen = itemView.findViewById(R.id.idImagen);
}
}
public AdaptadorX(ArrayList<Items> itemsListado, Context context_L, OnImageClickListener listener){
itemsLi = itemsListado;
context = context_L;
this.listener = listener;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_item, parent, false);
ViewHolder content = new ViewHolder(view);
return content;
}
#Override
public void onBindViewHolder(#NonNull final ViewHolder holder, final int position) {
final Items contarItems = itemsLi.get(position);
Glide.with(context).load(contarItems.getxNombre_imagen()).into(holder.idSrcImagen);
holder.idSrcImagen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
listener.onSelected(contarItems.getxNombre_imagen())
}
});
}
#Override
public int getItemCount() {
Log.d("Mensaje_Size_Tamaño", String.valueOf(itemsLi.size()));
return itemsLi.size();
}
}
And in your Activity you implement this interface and pass it to the adapter.
public class MainActivity extends AppCompatActivity implements OnImageClickListener {
public ImageView idImgHead;
private ArrayList<Items> items;
private RecyclerView idRecyclerView;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
idRecyclerView = findViewById(R.id.idRecyclerView);
idImgHead = findViewById(R.id.idImgHead);
Glide.with(this).load("https://www.midominio.com/Imagen_GRANDE_head_01.jpg").into(idImgHead); //IMAGE BIG
listadoXhead();
}
#Override
public onSelected(String url) {
Glide.with(this).load(url).into(idImgHead);
}
private void listadoXhead() {
ArrayList<Items> items = new ArrayList<>();
items.add(new Items("https://www.midominio.com/Imagen_Pequeña_head_01.jpg")); //IMAGE Small
items.add(new Items("https://www.midominio.com/Imagen_Pequeña_head_02.jpg")); //IMAGE Small
items.add(new Items("https://www.midominio.com/Imagen_Pequeña_head_03.jpg")); //IMAGE Small
idRecyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(MainActivity.this);
((LinearLayoutManager) layoutManager).setOrientation(RecyclerView.HORIZONTAL);
adapter = new AdaptadorX(items, MainActivity.this, this);
idRecyclerView.setLayoutManager(layoutManager);
idRecyclerView.setAdapter(adapter);
}
}
I want to add items to RecyclerView when I click the "Add" button
This currently works but only once , meaning if I click the Add button the first time, the item is added and visible, but after that, nothing is added.
Here is my code for RecyclerView Adapter
public class Adapter extends RecyclerView.Adapter<Adapter.myViewHolder> {
List<Integer> listItem;
public Adapter(List<Integer> passedListItem){
this.listItem = passedListItem;
}
#Override
public myViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recyclerview_layout, parent, false);
myViewHolder holder = new myViewHolder(itemView);
return holder;
}
#Override
public void onBindViewHolder(myViewHolder holder, int position) {
int itemNumber = position+1;
holder.itemTextView.setText("Item Number " + itemNumber + ": " + listItem.get(position));
}
#Override
public int getItemCount() {
return listItem.size();
}
public class myViewHolder extends RecyclerView.ViewHolder {
TextView itemTextView;
public myViewHolder(View view){
super(view);
itemTextView = view.findViewById(R.id.tv_itemTextView);
}
}
}
Here's my MainActivity
public class MainActivity extends AppCompatActivity {
List<Integer> itemList = new ArrayList<>();
EditText itemEditText;
RecyclerView recyclerView;
Adapter rvAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView)findViewById(R.id.rv_itemsRecyclerView);
itemEditText = (EditText)findViewById(R.id.et_editText);
//Setting the layout and Adapter for RecyclerView
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
rvAdapter = new Adapter(itemList);
recyclerView.setAdapter(rvAdapter);
}
//Click listener for "Add" Button
public void onAddButtonClicked(View view) {
try {
int IntegerFormat = Integer.valueOf(itemEditText.getText().toString());
itemList.add(IntegerFormat);
rvAdapter.notifyItemInserted(itemList.size() - 1);
itemEditText.setText("");
} catch(NumberFormatException e) {
Toast.makeText(getApplicationContext(), "The field is empty",
Toast.LENGTH_SHORT).show();
}
}
}
When i click the Add button, the first item is added and is visible, but when I click the Add button second time, nothing happens.
Solved
EDIT: Apparently, my Recycler view layout had its width and height set to match_parent instead of wrap_content, so the second item was getting added after clicking the button but it was added way way below. And I was stupid enough to not even try to scroll down. Everything was just working fine but I was ignorent.
I just checked your code and it is working fine. The mistake is in the recyclerview_layout.xml file which you havent posted. What you have is this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/tv_itemTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="TextView" />
</LinearLayout>
Please change this to:
<?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">
<TextView
android:id="#+id/tv_itemTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
Your first list item is filling the entire recycler view because your linear layout has android:layout_height="match_parent" instead of android:layout_height="wrap_content" so the first item is hiding the other items but they are there in the adapter. You can confirm this by logging in logcat.
I want to add card style in my app like this
i use in my app mysql database so i need to make like this cards and put my data from database in it now i use ListView with this code
public void listAllItme() {
ListAdapter lA = new listAdapter(listitems);
listView.setAdapter(lA);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent open = new Intent(R_arabic.this, rewaya_show.class);
open.putExtra("name", listitems.get(position).name);
open.putExtra("url", listitems.get(position).url);
open.putExtra("img", listitems.get(position).img);
open.putExtra("num", listitems.get(position).num);
startActivity(open);
}
}
});
}
class listAdapter extends BaseAdapter {
ArrayList<listitem_gib> lista = new ArrayList<listitem_gib>();
public listAdapter(ArrayList<listitem_gib> lista) {
this.lista = lista;
}
#Override
public int getCount() {
return lista.size();
}
#Override
public Object getItem(int position) {
return lista.get(position).name;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = getLayoutInflater();
View view = layoutInflater.inflate(R.layout.row_item_gib, null);
TextView name = (TextView) view.findViewById(R.id.textView_gib);
ImageView img = (ImageView) view.findViewById(R.id.imageView_gib);
TextView num = (TextView) view.findViewById(R.id.textView_gib2);
TextView size = (TextView) view.findViewById(R.id.textView_gib3);
name.setText(lista.get(position).name);
num.setText(lista.get(position).num);
size.setText(lista.get(position).size);
Picasso.with(R_arabic.this).load("http://grassyhat.com/android/image/" + lista.get(position).img).into(img);
return view;
}
}
first i want to know how i can make like this card style
second how i can use this code with card menu not listview
sorry im new in android and sorry for my bad english
What do you mean by card menu? because the example in the image is a recyclerview with a cardview item, you can achieve this by doing something like this
This will be your activity
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
//Just your list of objects, in your case the list that comes from the db
List<Items> itemsList = new ArrayList<>();
CardAdapter adapter = new CardAdapter(this, itemsList);
//RecyclerView needs a layout manager in order to display data so here we create one
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
//Here we set the layout manager and the adapter to the listview
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
}
Inside the layout file you just have to place the recyclerview like this
<RelativeLayout
android:id="#+id/activity_main"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="jsondh.myapplication.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
Then your adapter will be something like this
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.CardViewHolder> {
private List<Items> itemsList;
private Activity activity;
public CardAdapter(Activity activity, List<Items> items){
this.activity = activity;
this.itemsList = items;
}
#Override
public CardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = activity.getLayoutInflater().inflate(R.layout.cardview_layout, parent, false);
return new CardViewHolder(itemView);
}
#Override
public void onBindViewHolder(CardViewHolder holder, int position) {
//Here you bind your views with the data from each object from the list
}
#Override
public int getItemCount() {
return itemsList.size();
}
public class CardViewHolder extends RecyclerView.ViewHolder {
public ImageView bookImage;
public TextView bookLabel01, bookLabel02;
public CardViewHolder(View itemView) {
super(itemView);
bookImage = (ImageView)itemView.findViewById(R.id.image);
bookLabel01 = (TextView)itemView.findViewById(R.id.label01);
bookLabel02 = (TextView)itemView.findViewById(R.id.label02);
}
}
And the last one will be the layout from each item on the list, like this
<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="match_parent"
android:layout_margin="10dp">
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardElevation="15dp"
app:cardBackgroundColor="#3369Ed">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/image"
android:layout_width="150dp"
android:layout_height="130dp"/>
<TextView
android:id="#+id/label01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Label"
android:layout_gravity="right"
android:padding="5dp"
android:textColor="#ffffff"/>
<TextView
android:id="#+id/label02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LongerLabel"
android:layout_gravity="right"
android:padding="5dp"
android:textColor="#ffffff"/>
</LinearLayout>
</android.support.v7.widget.CardView>
You also have to add this to your gradle file:
compile 'com.android.support:recyclerview-v7:25.0.0'
compile 'com.android.support:cardview-v7:25.0.0'
Hope it helps!
It's pretty simple. You will have to use a RecyclerView with GridLayoutManager and add a cardView to it.
Then, use an Adapter and ViewHolder to feed the data.
I suggest you to check this out:
https://developer.android.com/training/material/lists-cards.html
I'm removing images from a gridview when a button is pressed.
The images does remove fine and the gridview also updates with the call "adapter.notifyDataSetChanged();".
When an imageview is removed another image next to this position should take its position. This happens, but the image here won't reload so there's just a blank space? How can I get this imageview to reload its image?
The problem:
Here is my gridadapter:
public class FavoriteMovieGridAdapter extends BaseAdapter {
Context context;
ArrayList<DataFavorites> List;
FavoritesMovie fragment;
private static LayoutInflater inflater = null;
FavoriteMovieGridAdapter adapter = this;
public FavoriteMovieGridAdapter(Context context, ArrayList<DataFavorites> List, FavoritesMovie fragment) {
this.context = context;
this.List = List;
this.fragment = fragment;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return List.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// Avoid unneccessary calls to findViewById() on each row
final ViewHolder holder;
/*
* If convertView is not null, reuse it directly, no inflation
* Only inflate a new View when the convertView is null.
*/
if (convertView == null) {
convertView = inflater.inflate(R.layout.favorite_grid_item, null);
holder = new ViewHolder();
holder.poster = (ImageView) convertView.findViewById(R.id.upcoming_image);
holder.editbutton = (ImageView) convertView.findViewById(R.id.delete_item);
// The tag can be any Object, this just happens to be the ViewHolder
convertView.setTag(holder);
}
else{
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
}
final View finalConvertView = convertView;
final DataFavorites e;
new DataFavorites();
e = List.get(position);
String url = String.valueOf(e.getUrl());
// load image url into poster
// Seems as if this doesn't run for the imageview next to this when this view is removed?
Picasso.with(context).load(url).fit().placeholder(R.drawable.movie_back).into(holder.poster);
// Create onclick and show edit button
convertView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// show edit button
holder.editbutton.setVisibility(View.VISIBLE);
YoYo.with(Techniques.FadeIn).duration(700).playOn(finalConvertView.findViewById(R.id.delete_item));
// onclick edit button remove item and update gridview
holder.editbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
YoYo.with(Techniques.ZoomOut).duration(700).playOn(finalConvertView.findViewById(R.id.favorite_relative));
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
adapter.List.remove(e);
adapter.notifyDataSetChanged();
// Delete specific movie from data base
DatabaseHandlerMovie db = new DatabaseHandlerMovie(context);
// Reading all movies
db.deleteMovie(e);
}
}, 1000);
}
});
return false;
}
});
return convertView;
}
static class ViewHolder {
ImageView poster;
ImageView editbutton;
}
}
My grid item layout:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/favorite_relative"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal">
<ImageView android:layout_width="123.3dp"
android:layout_height="185.3dp"
android:id="#+id/upcoming_image"
android:scaleType="fitXY" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/delete_item"
android:src="#drawable/ic_remove_circle_outline_white_24dp"
android:tint="#color/colorPrimaryDark"
android:clickable="true"
android:visibility="gone"
android:layout_alignRight="#+id/upcoming_image"
android:layout_alignEnd="#+id/upcoming_image"/>
</RelativeLayout>
My grid layout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".Favorites"
android:background="#FFFFFF"
android:focusableInTouchMode="true">
<GridView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:id="#+id/movies_gridlayout"
android:paddingRight="-1dp"
android:paddingEnd="-1dp"
android:numColumns="3"
android:background="#ffffff"
android:visibility="invisible"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#+id/favorite_none"
android:text="No favorite movies found"
android:textSize="15sp"
android:visibility="gone"
android:textColor="#color/SecondaryText"
/>
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone"
>
</ProgressBar>
</FrameLayout>
Turns out a 3rd-party lib was causing this:
YoYo.with(Techniques.ZoomOut).duration(700).playOn(finalConvertView.findViewById(R.id.favorite_relative));
you will have adapter and mainactivity for gridview
So u will have to implement public interface in both of them to communicate
following code may help you :)
In Adapter class add following code
private OnItemClickListner onItemClickListner;
public MyAdapter(Context c, List<PDFDoc> pdfDocs,OnItemClickListner onItemClickListner) {
this.context = c;
this.pdfDocs = pdfDocs;
this.onItemClickListner=onItemClickListner;
}
public interface OnItemClickListner {
void removefromadapter(int position);
}
ImageButton imageButton = view.findViewById(R.id.cancelid);
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// write your stuff here
onItemClickListner.removefromadapter(position);
}
});
In MainActivity of gridview add following code
implements OnItemClickListner required**
public class addclient_fragment extends Fragment implements MyAdapter.OnItemClickListner {
you will required to add this method in mainactivity
#Override
public void removefromadapter(int position) {
// write your stuff here
}
I've a RecyclerView in a LinearLayout which is in a ScrollView.
I get the items for the RecyclerView from the net so I've to notifyDataSetChanged.
This is working - I get the new data in the Adapter. But the view doesn't change.
Here the xml like I wrote above:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="#+id/viewPager_featured"
android:layout_width="match_parent"
android:layout_height="#dimen/featured_object_height"
android:layout_gravity="center" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
</LinearLayout>
when I get the data...:
public void initAdapter(ArrayList<Stream> list) {
// initializing: passed the streams element
this.streams.addAll(list);
// the RecyclerView Adapter
mBitmovinAdapter.notifyDataSetChanged();
mLinearLayout.invalidate();
mRecyclerView.invalidate();
}
and my adapter..:
BitmovinAdapter(ArrayList<Stream> streams) {
this.mStreams = streams;
}
ArrayList<Stream> mStreams;
class BitmovinHolder extends RecyclerView.ViewHolder {
TextView title;
TextView format;
ImageView poster;
public BitmovinHolder(final View view) {
super(view);
title = (TextView) view.findViewById(R.id.title);
format = (TextView) view.findViewById(R.id.format);
poster = (ImageView) view.findViewById(R.id.poster);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
BasePlayerActivity.launchPlayerActivity(view.getTag().toString(), title.getText().toString(), 0, MainActivity.debug, view.getContext());
}
});
}
}
#Override
public void onBindViewHolder(BitmovinHolder holder, int position) {
Stream stream = mStreams.get(position);
holder.title.setText(stream.title);
holder.format.setText(stream.format);
Glide.with(holder.poster.getContext()).load(stream.poster).into(holder.poster);
holder.itemView.setTag(stream.stream);
}
#Override
public BitmovinHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new BitmovinHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false));
}
#Override
public int getItemCount() {
return mStreams.size();
}
Set the data to adapter and then call notifyDataSetChanged.
mPagerAdapter.setData(streams);
mPagerAdapter.notifyDataSetChanged();
Try this In RecyclerView
Add this Method in your RecyclerView.Adapter
ArrayList<Stream> list
/*
* Inserting a new item at the head of the list. This uses a specialized
* RecyclerView method, notifyItemInserted(), to trigger any enabled item
* animations in addition to updating the view.
*/
public void addItem(int position) {
if (position > list.size()) return;
list.add(position, generateDummyItem());
notifyItemInserted(position);
}
Try something like adapter.clear() before adding new elements. Then call adapter.addAll().