I need dividers in RecyclerView like an Image,I cant understand how I can do this.Dividers must look like textview or image,I need to make different ViewHolder or what?Im google it and don't find any example look like I need.
Its must look like this
http://www.picshare.ru/view/7210663/
My Adapter:
package com.example.myapplication;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
import java.util.Objects;
import butterknife.Bind;
/**
* Created by Рост on 20.03.2016.
*/
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.MyViewHolder> {
private List<User> UserList;
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder = null;
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View viewUser = inflater.inflate(R.layout.row_item_layout,parent,false);
viewHolder = new MyViewHolder(viewUser);
return (MyViewHolder) viewHolder;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.UserTitle.setText(UserList.get(position).getTitle());
holder.UserDescription.setText(UserList.get(position).getDescription());
holder.UserIcon.setImageResource(R.drawable.emilypic);
}
public UserAdapter(List<User> UserList){
this.UserList = UserList;
}
#Override
public int getItemCount() {
return UserList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public ImageView UserIcon;
public TextView UserTitle;
public TextView UserDescription;
public Button UserFollowButton;
public MyViewHolder(View itemView) {
super(itemView);
UserIcon = (ImageView) itemView.findViewById(R.id.item_icon);
UserTitle = (TextView) itemView.findViewById(R.id.item_title);
UserDescription = (TextView) itemView.findViewById(R.id.item_description);
UserFollowButton = (Button) itemView.findViewById(R.id.item_follow_button);
}
}
}
Fragment with data:
package com.example.myapplication;
import android.annotation.TargetApi;
import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
import butterknife.Bind;
public class Fragment_Username_Search extends Fragment {
private List<User> userList = new ArrayList<>();
private RecyclerView recyclerView;
private UserAdapter userAdapter;
#TargetApi(Build.VERSION_CODES.M)
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
private void prepareUserData() {
User user = new User(R.drawable.emilypic,"Marry Young","marbear",R.id.item_follow_button);
userList.add(user);
user = new User(R.drawable.emilypic,"Marry Young","marbear",R.id.item_follow_button);
userList.add(user);
user = new User(R.drawable.emilypic,"Sammy Lee","sambam",R.id.item_follow_button);
userList.add(user);
user = new User(R.drawable.emilypic,"Lilly Martinson","lilmartini",R.id.item_follow_button);
userList.add(user);
userAdapter.notifyDataSetChanged();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment__username__search, container, false);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
recyclerView = (RecyclerView) view.findViewById(R.id.RecyclerView);
userAdapter = new UserAdapter(userList);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(userAdapter);
prepareUserData();
return view;
}
}
Related
My RecyclerView gets the same value as the other once I edited/updated a specifc item. Here is the example of what I am getting instead of update one specific item only. example
package com.example.crudrecycler;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import org.w3c.dom.Text;
import java.lang.reflect.Array;
import java.util.ArrayList;
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ModelViewHolder> {
Context c;
ArrayList<StudentModel> studentArrayList= new ArrayList<>();
SQLiteDatabase sqlDB;
public CustomAdapter(Context c, int onedata, ArrayList<StudentModel> studentArrayList, SQLiteDatabase sqlDB) {
this.c = c;
this.studentArrayList = studentArrayList;
this.sqlDB = sqlDB;
}
#NonNull
#Override
public CustomAdapter.ModelViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(c);
View view = inflater.inflate(R.layout.onedata, null);
return new ModelViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull CustomAdapter.ModelViewHolder holder, int position) {
final StudentModel model = studentArrayList.get(position);
holder.txtName.setText(model.getName());
holder.txtAddress.setText(model.getAddress());
holder.btn_Edit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Bundle bundle = new Bundle();
bundle.putInt("id", model.getId());
bundle.putString("name", model.getName());
bundle.putString("address", model.getAddress());
Intent intent = new Intent(c, MainActivity.class);
intent.putExtra("userdata", bundle);
c.startActivity(intent);
notifyDataSetChanged();
}
});
}
#Override
public int getItemCount() {
return studentArrayList.size();
}
public class ModelViewHolder extends RecyclerView.ViewHolder {
TextView txtName, txtAddress;
Button btn_Edit;
public ModelViewHolder(#NonNull View itemView) {
super(itemView);
txtName = (TextView)itemView.findViewById(R.id.txtName);
txtAddress = (TextView)itemView.findViewById(R.id.txtAddress);
btn_Edit = (Button)itemView.findViewById(R.id.btn_Edit);
}
}
}
I think the problem here is that I am setting my intent to my MainActivity.class and once I click edit/update on my MainActivity it also updates other items inside the RecyclerView, but still I don't know how to fix where and which is the problem here since I am just new and exploring things inside android studio.
i try to get the name of the movies which are in recycler view n store in variable 'name' but how do i pass in to dictionary fragment n set it on definitionn(edittext). i tried with this code but it shows empty
i try to get the name of the movies which are in recycler view n store in variable 'name' but how do i pass in to dictionary fragment n set it on definitionn(edittext). i tried with this code but it shows empty
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
// item of recyclerview was not visible until included
app:layoutManager="android.support.v7.widget.LinearLayoutManager">
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerAdapter recyclerAdapter;
List<String> moviesList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
moviesList = new ArrayList<>();
recyclerView = findViewById(R.id.recyclerView);
recyclerAdapter = new RecyclerAdapter(moviesList);
recyclerView.setAdapter(recyclerAdapter);
DividerItemDecoration dividerItemDecoration = new
DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
recyclerView.addItemDecoration(dividerItemDecoration);
moviesList.add("IronMan");
moviesList.add("The Incredible Hulk");
moviesList.add("Iron Man 2");
moviesList.add("Thor");
moviesList.add("Captain America: The First Avenger");
moviesList.add("The Avengers");
moviesList.add("Iron Man 3");
moviesList.add("Thor: The Dark World");
moviesList.add("Captain America: The Winter Soldier");
moviesList.add("Guardians of the Galaxy");
moviesList.add("Avengers: Age of Ultron");
moviesList.add("Ant-Man");
moviesList.add("Captain America: Civil War");
moviesList.add("Doctor Strange");
moviesList.add("Guardians of the Galaxy Vol. 2");
moviesList.add("Spider-Man: Homecoming");
moviesList.add("Thor: Ragnarok");
moviesList.add("Black Panther");
moviesList.add("Avengers: Infinity War");
moviesList.add("Ant-Man and the Wasp");
moviesList.add("Captain Marvel");
moviesList.add("Avengers: Endgame");
moviesList.add("Spider-Man: Far From Home");
}
}
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
public class RecyclerAdapter extends
RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
String name="";
private static final String TAG = "RecyclerAdapter";
List<String> moviesList;
public RecyclerAdapter(List<String> moviesList) {
this.moviesList = moviesList;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int
viewType) {
LayoutInflater layoutInflater =
LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.row_item, parent,
false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int
position) {
holder.rowCountTextView.setText(String.valueOf(position));
holder.textView.setText(moviesList.get(position));
}
#Override
public int getItemCount() {
return moviesList.size();
}
class ViewHolder extends RecyclerView.ViewHolder implements
View.OnClickListener {
ImageView imageView;
TextView textView, rowCountTextView;
public ViewHolder(#NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageView);
textView = itemView.findViewById(R.id.textView);
rowCountTextView =
itemView.findViewById(R.id.rowCountTextView);
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(new
View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
moviesList.remove(getAdapterPosition());
notifyItemRemoved(getAdapterPosition());
return true;
}
});
}
#Override
public void onClick(View view) {
name=moviesList.get(getAdapterPosition()).toString();
AppCompatActivity activity=(AppCompatActivity)
view.getContext();
Fragment myfragment = new Dictionary();
activity.getSupportFragmentManager().beginTransaction().
replace(R.id.activitymain,myfragment).addToBackStack(null).commit();
}
}
}
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
public class Dictionary extends Fragment {
public EditText definitionnn;
public Dictionary() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_dictionary, container, false);
definitionnn = (EditText) v.findViewById(R.id.definition);
RecyclerAdapter recycle = new RecyclerAdapter(null);
String word = recycle.name;
return v;
}
#Override
public void onStart() {
super.onStart();
RecyclerAdapter recycle = new RecyclerAdapter(null);
String word = recycle.name;
definitionnn.setText(word);
}
}
The problem is that in your OnClick() Method you create and add the new Dictionary Fragment without passing the value you want. Something like this would be your solution.
In your RecyclerAdapter class:
Bundle bundle = new Bundle();
bundle.putString(yourVariable, "word");
Fragment fragment = new Dictionary();
fragobj.setArguments(bundle);
activity.getSupportFragmentManager().beginTransaction().
replace(R.id.activitymain,fragment).addToBackStack(null).commit();
And in your Dictionary Fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_dictionary, container, false);
String word = getArguments().getString("word");
definitionnn = (EditText) v.findViewById(R.id.definition)
definitionnn.setText(word);
return v;
}
I am new to Android Studio so I'm trying to make a RecyclerView with its Adapter and ViewHolder, the Adapter class needs a list to make each item in the list, the list should be filled from database from FireBase.
the FireBase reference retrieve the data needed and add them to the list, but the list always stays empty, Why?
Here's the code :
MainActivity:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
public class MainActivity extends AppCompatActivity {
private RecyclerView rView;
private RecyclerView.LayoutManager layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rView = findViewById(R.id.rView);
layout = new LinearLayoutManager(this);
rView.setLayoutManager(layout);
rView.setHasFixedSize(true);
ProjectAdapter reAdapter = new ProjectAdapter();
rView.setAdapter(reAdapter);
}
}
Adapter:
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
import com.google.firebase.firestore.QuerySnapshot;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import java.util.ArrayList;
import java.util.List;
public class ProjectAdapter extends RecyclerView.Adapter<ProjectAdapter.ProjectViewHolder> {
private List<Project> proList;
private FirebaseFirestore firestore;
private FirebaseStorage storage;
private StorageReference storageRef;
private Project p;
public ProjectAdapter(){
firestore = FirebaseFirestore.getInstance();
storage = FirebaseStorage.getInstance();
storageRef = FirebaseStorage.getInstance().getReference();
proList = new ArrayList<>();
firestore.collection("Projects").addSnapshotListener(new EventListener<QuerySnapshot>() {
#Override
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
if(e == null){
for(DocumentSnapshot doc : documentSnapshots){
Log.d("MainActivity", "onComplete: " + doc.getString("proName"));
p = new Project();
p.setProName(doc.getString("proName"));
p.setProDes(doc.getString("proDescription"));
p.setProPic(doc.getString("picture"));
System.out.println(p.getProName());
proList.add(p);
}
}
}
});
}
public static class ProjectViewHolder extends RecyclerView.ViewHolder {
public TextView proName;
public ImageView proPic;
public TextView proBreif;
public ProjectViewHolder(#NonNull View itemView) {
super(itemView);
proName = itemView.findViewById(R.id.proName);
proBreif = itemView.findViewById(R.id.DesctextView);
proPic = itemView.findViewById(R.id.proPic);
}
}
#NonNull
#Override
public ProjectViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.activity_item, viewGroup, false);
return new ProjectViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ProjectViewHolder projectViewHolder, int i) {
projectViewHolder.proName.setText(proList.get(i).getProName());
projectViewHolder.proBreif.setText(proList.get(i).getProDes());
[projectViewHolder.proPic.setImageURI(Uri.parse(proList.get(i).getProPic()));][1]
}
#Override
public int getItemCount() {
System.out.println(proList.size()); // the output always 0, it got called before retrieving form the database.
return proList.size();
}
#Override
public int getItemViewType(final int position) {
return R.layout.activity_item;
}
}
the output is :
the names are the data retrieved from database.
I would recommend using one of Firestore's built in adapters instead - the Firestore Recycler Adapter. https://github.com/firebase/FirebaseUI-Android/tree/master/firestore
It is built especially for this purpose and makes it much easier to implement.
So when I delete a row in the SQLITE Database and the actual activity is closing, I see the CardView which is delete.
I really tried to google but nothing changed, so maybe you could help me out guys.
This is the SQLITE Delete function:
public void deleteEntry(String ID){
SQLiteDatabase db = this.getWritableDatabase();
String query = "DELETE FROM " + DB_NAME + " WHERE ID = '" + ID + "'";
Log.d("QUERY",query);
db.execSQL(query);
db.close();
}
MyAdapter.java
package simplylabs.com.simplynote;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import static android.content.ContentValues.TAG;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>
{
public MyAdapter(List<ListItem> listItems, Context context) {
this.listItems = listItems;
this.context = context;
}
private List<ListItem> listItems;
private Context context;
#Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.row_design,parent,false);
return new MyViewHolder(v);
}
#Override
public void onBindViewHolder(MyAdapter.MyViewHolder holder, int position)
{
final ListItem list = listItems.get(position);
holder.insert_NOTIZENNAME.setText(list.getNOTIZENNAME());
holder.insert_NOTIZ.setText(list.getNOTIZ());
holder.insert_TIME.setText(list.getTIME());
if(list.getPRIORITAET().equals("Sehr wichtig")){
holder.prioColor.setBackgroundResource(R.color.rot);
}
if(list.getPRIORITAET().equals("Wichtig")){
holder.prioColor.setBackgroundResource(R.color.orange);
}
if(list.getPRIORITAET().equals("Nicht wichtig")){
holder.prioColor.setBackgroundResource(R.color.gruen);
}
}
#Override
public int getItemCount() {
return listItems.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
TextView insert_NOTIZENNAME;
TextView insert_NOTIZ;
TextView insert_TIME;
View prioColor;
public MyViewHolder(View itemView) {
super(itemView);
insert_NOTIZENNAME = itemView.findViewById(R.id.showNOTIZENNAME);
insert_NOTIZ = itemView.findViewById(R.id.show_NOTIZ);
insert_TIME = itemView.findViewById(R.id.show_DATUM);
prioColor = itemView.findViewById(R.id.view_prioritaet);
}
}
}
This is when I click on the menu Item:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
DBHandler dbHandler = new DBHandler(this);
final Bundle extras = getIntent().getExtras();
switch(item.getItemId()){
case R.id.notiz_loeschen:
dbHandler.deleteEntry(extras.getString("ID"));
Toast.makeText(this, extras.getString("ID"), Toast.LENGTH_SHORT).show();
finish();
break;
case R.id.notiz_aendern:
break;
}
return super.onOptionsItemSelected(item);
}
MainActivity.java
recyclerView = rootView.findViewById(R.id.recyclerview_alle_notizen);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setItemAnimator(new DefaultItemAnimator());
adapter = new MyAdapter(listItems,getActivity());
adapter.notifyDataSetChanged();
recyclerView.setAdapter(adapter);
recyclerView.addOnItemTouchListener(
new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() {
#Override public void onItemClick(View view, int position) {
if (data.moveToPosition(position))
{
Intent i = new Intent(getActivity(),NotizAnzeigen.class);
i.putExtra("NotizName",data.getString(1));
i.putExtra("Notiz",data.getString(2));
startActivity(i);
}
}
})
);
Could you please help me out please?
I've changed this, it is still only remove the cardview when i move to the last tab ...
package simplylabs.com.simplynote;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import java.util.ArrayList;
import java.util.List;
public class AlleNotizen extends Fragment {
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
private List<ListItem> listItems;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.alle_notizen, container, false);
recyclerView = rootView.findViewById(R.id.recyclerview_alle_notizen);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setItemAnimator(new DefaultItemAnimator());
return rootView;
}
#Override
public void onResume() {
listItems = new ArrayList<>();
ListItem listItem = new ListItem();
final DBHandler db = new DBHandler(getActivity());
final Cursor data = db.getData();
while(data.moveToNext()){
ListItem li = new ListItem(data.getString(0), data.getString(1),data.getString(2),data.getString(4),data.getString(3));
listItems.add(li);
}
adapter = new MyAdapter(listItems,getActivity());
adapter.notifyDataSetChanged();
recyclerView.setAdapter(adapter);
recyclerView.addOnItemTouchListener(
new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() {
#Override public void onItemClick(View view, int position) {
if (data.moveToPosition(position))
{
Intent i = new Intent(getActivity(),NotizAnzeigen.class);
i.putExtra("NotizName",data.getString(1));
i.putExtra("Notiz",data.getString(2));
startActivity(i);
}
}
})
);
super.onResume();
}
}
As you are deleting data from your local storage you need to notify the adapter that data set has changed.To do that you could try this.
After deleting data from SQLite you need to read data again and store
it in a list/arraylist variable.
create a function inside your adapter like this,
public void loadWithNewData(List<items> newCollectedData) {
this.listItems.clear();
this.listItems = newCollectedData;
this.notifyDataSetChanged();
}
And then call that function with adapter variable like this.
adapter.loadWithNewData(newDataCollectedFromDatabase);
hope it will help.
You are deleting the row from the database, but you are not updating your listItems array. You should update listItems and then call adapter.notifyDataSetChanged();
hi i have a recyclerview with GridLayoutManager that have 3 cols but in the first row just shows 2 items like this :
where i was wrong in this codes :
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.github.florent37.materialviewpager.MaterialViewPagerHelper;
import com.github.florent37.materialviewpager.adapter.RecyclerViewMaterialAdapter;
import java.util.ArrayList;
import java.util.List;
public class Main_Recycle6 extends Fragment {
private RecyclerView recyclerView;
private Main_Recycle_Adapter2 movieRecyclerviewAdapter;
private List<Main> movies;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
return inflater.inflate(R.layout.main_grid4, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
movies = new ArrayList<>();
recyclerView = (RecyclerView) getView().findViewById(R.id.Recycle1);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
// recyclerView.setNestedScrollingEnabled(false);
RecyclerView.LayoutManager layoutManager;
layoutManager = new GridLayoutManager(getActivity(), 3);
recyclerView.setLayoutManager(layoutManager);
movies = getData();
movieRecyclerviewAdapter = new Main_Recycle_Adapter2 (getActivity(), movies);
recyclerView.setAdapter(movieRecyclerviewAdapter);
RecyclerViewMaterialAdapter movieRecyclerviewAdapterMaterial = new RecyclerViewMaterialAdapter(movieRecyclerviewAdapter);
// 4. set adapter
recyclerView.setAdapter(movieRecyclerviewAdapterMaterial);
//5. Register the recyclerview to MaterialViewPager
MaterialViewPagerHelper.registerRecyclerView(getActivity(), recyclerView, null);
}
public static Main_Recycle newInstance() {
return new Main_Recycle();
}
private List<Main> getData() {
List<Main> movies = new ArrayList<>();
String names[] = { "آزمون 1",
"آزمون 2",
.....
};
String logos[] = {"i1",
"i2",
.....
};
for (int i = 0; i < names.length; i++) {
Main newMovie = new Main(names[i], logos[i]);
movies.add(newMovie);
}
return movies;
}
adapter :
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.Collections;
import java.util.List;
public class Main_Recycle_Adapter2 extends RecyclerView.Adapter<Main_Recycle_Adapter2.MovieViewHolder> {
List<Main> movies = Collections.emptyList();
LayoutInflater layoutInflater;
Context context;
public Main_Recycle_Adapter2(Context context, List<Main> movies){
this.context = context;
layoutInflater = LayoutInflater.from(context);
this.movies = movies;
}
//;;;;;;;;;;;;;;
#Override
public int getItemCount() {
return movies.size();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public MovieViewHolder onCreateViewHolder(ViewGroup parent, int viewType )
{
View row;
row = layoutInflater.inflate(R.layout.gridview_custom_layout, parent, false);
MovieViewHolder holder = new MovieViewHolder(row);
return holder;
}
#Override
public void onBindViewHolder(MovieViewHolder holder, int position) {
Main thisMovie = movies.get(position);
holder.tozih.setText(thisMovie.getName());
holder.tablooo.setImageResource(context.getResources().getIdentifier(thisMovie.getLogo(), "drawable", context.getPackageName()));
}
public class MovieViewHolder extends RecyclerView.ViewHolder {
private TextView tozih;
private ImageView tablooo;
public MovieViewHolder(View itemView) {
super(itemView);
tozih = (TextView) itemView.findViewById(R.id.gridview_text);
tablooo = (ImageView) itemView.findViewById(R.id.gridview_image);
}
}
}
here is the grid code
the recyclerview is in a fragment and the fragment is in another activity
RecyclerView.LayoutManager layoutManager;
layoutManager = new GridLayoutManager(getActivity(), 3);
recyclerView.setLayoutManager(layoutManager);
thanks in advance