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
Related
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 have an Array list here,
I want each of them when clicked, they will do a specific function or intent an activity.
Currently: each item contains, ImageButton and a Textview in a list.
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetDialogFragment;
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;
public class MyBottomSheetDialogFragment extends BottomSheetDialogFragment {
String mString;
private RecyclerView mRecyclerView;
private VerticalListAdapter mVerticalListAdapter;
static MyBottomSheetDialogFragment newInstance(String string) {
MyBottomSheetDialogFragment f = new MyBottomSheetDialogFragment();
Bundle args = new Bundle();
args.putString("string", string);
f.setArguments(args);
return f;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mString = getArguments().getString("string");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.bottom_sheet_modal_list, container, false);
mRecyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);
return v;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
List<Model> list = new ArrayList<Model>();
list.add(new Model(R.drawable.a1, "Call"));
list.add(new Model(R.drawable.a2, "Chat"));
list.add(new Model(R.drawable.a3, "Android"));
list.add(new Model(R.drawable.a4, "Location"));
list.add(new Model(R.drawable.a5, "Call"));
list.add(new Model(R.drawable.a6, "Chat"));
list.add(new Model(R.drawable.a7, "Android"));
mVerticalListAdapter = new VerticalListAdapter(getContext(), list);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
mRecyclerView.setLayoutManager(linearLayoutManager);
mRecyclerView.setAdapter(mVerticalListAdapter);
}
I can provide additional XML , Java codes of the project that is relatively connected to this code, Please leave a comment for clarification.
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.ImageButton;
import android.widget.TextView;
import java.util.List;
public class VerticalListAdapter extends RecyclerView.Adapter<VerticalListAdapter.MyViewHolder>{
private Context mContext;
private List<Model> mModelList;
public VerticalListAdapter(Context context, List<Model> list) {
this.mContext = context;
this.mModelList = list;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.modal_list_item, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.txtView.setText(mModelList.get(position).getName());
holder.imgView.setImageDrawable(mContext.getResources().getDrawable(mModelList.get(position).getImgId()));
}
#Override
public int getItemCount() {
return mModelList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView txtView;
public ImageButton imgView;
public MyViewHolder(View view) {
super(view);
txtView = (TextView) view.findViewById(R.id.txtView);
imgView = (ImageButton) view.findViewById(R.id.imgView);
}
}
}
What you need to do is to implement the OnClickListener to your Holder class and add id to your LinearLayout in modal_list_item.xml so you can click on the entire layout not only its views (if I understand you correctly) and eventually set the onClickListener.
When you do this, you will need to implement the onClick method. For inspiration see the code below:
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView txtView;
public ImageButton imgView;
private LinearLayout linearLayout;
public MyViewHolder(View view) {
super(view);
txtView = (TextView) view.findViewById(R.id.txtView);
imgView = (ImageButton) view.findViewById(R.id.imgView);
linearLayout = (LinearLayout) view.findViewById(R.id.linear_l);
linearLayout.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Log.i("RecyclerView Item", String.valueOf(getLayoutPosition()));
mModelList.remove(getLayoutPosition());
notifyDataSetChanged();
}
}
Hi i am new to android project so i am trying to use recycler view. but it is not working. i keep getting errors # this line " RecyclerAdapter adapter = new RecyclerAdapter(this, getData());" it says RecyclerAdapter (android.content.context, List) in RecyclerAdapter cannot be applied to my package class please help
statement.java
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import java.util.ArrayList;
import java.util.List;
import nnnnn.R;
import nnnnnn.RecyclerAdapter;
import nnnnnnnnnnnnn.Landscape;
public class Statements extends Fragment {
Toolbar toolbar;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.statements,container,false);
toolbar = (Toolbar) v.findViewById(R.id.toolbar);
toolbar.setTitle("Statements");
toolbar.inflateMenu(R.menu.menu_main);
setUpRecyclerView();
return v;}
private void setUpRecyclerView() {
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
RecyclerAdapter adapter = new RecyclerAdapter(this.getActivity(),Landscape.getData());
recyclerView.setAdapter(adapter);
final LinearLayoutManager mLinearLayoutManagerVertical = new LinearLayoutManager(this.getActivity()); // (Context context, int spanCount)
mLinearLayoutManagerVertical.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(mLinearLayoutManagerVertical);
recyclerView.setItemAnimator(new DefaultItemAnimator()); // Even if we dont use it then also our items shows default animation. #Check Docs
}
}
RecyclerAdapter code is below
RecyclerAdapter
import android.content.Context;
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 java.util.List;
import xx.xx.xx.xx.xx.R;
import xx.xx.xx.xx.model.Landscape;
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
private static final String TAG = RecyclerAdapter.class.getSimpleName();
private List<Landscape> mDataList;
private LayoutInflater inflater;
public RecyclerAdapter(Context context, List<Landscape> data) {
this.mDataList = data;
this.inflater = LayoutInflater.from(context);
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Log.i(TAG, "onCreateViewHolder");
View view = inflater.inflate(R.layout.statementlist, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Log.i(TAG, "onBindViewHolder" + position);
Landscape current = mDataList.get(position);
holder.setData(current, position);
holder.setListeners();
}
#Override
public int getItemCount() {
return mDataList.size();
}
public void removeItem(int position) {
mDataList.remove(position);
notifyItemRemoved(position);
// notifyItemRangeChanged(position, mDataList.size());
// notifyDataSetChanged();
}
public void addItem(int position, Landscape landscape) {
mDataList.add(position, landscape);
notifyItemInserted(position);
// notifyItemRangeChanged(position, mDataList.size());
// notifyDataSetChanged();
}
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView title;
ImageView imgThumb, imgDelete, imgAdd;
int position;
Landscape current;
public MyViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.tvTitle);
imgThumb = (ImageView) itemView.findViewById(R.id.img_row);
imgDelete = (ImageView) itemView.findViewById(R.id.img_row_delete);
imgAdd = (ImageView) itemView.findViewById(R.id.img_row_add);
}
public void setData(Landscape current, int position) {
this.title.setText(current.getTitle());
imgThumb.setImageResource(current.getImageID());
this.position = position;
this.current = current;
}
public void setListeners() {
imgDelete.setOnClickListener(MyViewHolder.this);
imgAdd.setOnClickListener(MyViewHolder.this);
imgThumb.setOnClickListener(MyViewHolder.this);
}
#Override
public void onClick(View v) {
// Log.i("onClick before operation", position + " " + mDataList.size());
switch (v.getId()) {
case R.id.img_row_delete:
removeItem(position);
break;
case R.id.img_row_add:
addItem(position, current);
break;
}
// Log.i("onClick after operation", mDataList.size() + " \n\n" + mDataList.toString());
}
}
}
You're passing in "this" for the first argument to RecyclerAdapter. "this" being a Fragment class. The Fragment class does not extend from Context, so it cannot be used as such.
Try "this.getActivity()". This may result in null, so ideally put this within the Fragment subclass's "onActivityCreated" method (instead of the "onCreateView" method)
You need to override OnActivitycreated in order to call getActivity. Call setUpRecyclerView in OnActivitycreated and add getActivity. before findViewById and it will work
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;
}
}
I tried to find solution, but my weak programming skills didn't help me to solve problem.
import android.content.Context;
import android.content.Intent;
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 com.squareup.picasso.Picasso;
import java.util.ArrayList;
import java.util.List;
public class GridAdapter extends RecyclerView.Adapter<GridAdapter.ViewHolder> {
List<HeroItem> mItems;
Context mContext;
MyOnItemClickListener listener;
public GridAdapter(Context context) {
super();
this.mContext = context;
mItems = new ArrayList<HeroItem>();
HeroItem species = new HeroItem();
//species.setName("Amur Leopard");
species.setThumbnail(R.drawable.test1);
mItems.add(species);
species = new HeroItem();
//species.setName("Black Rhino");
species.setThumbnail(R.drawable.test2);
mItems.add(species);
species = new HeroItem();
//species.setName("Orangutan");
species.setThumbnail(R.drawable.test3);
mItems.add(species);
species = new HeroItem();
//species.setName("Sea Lions");
species.setThumbnail(R.drawable.test4);
mItems.add(species);
species = new HeroItem();
//species.setName("Indian Elephant");
species.setThumbnail(R.drawable.test5);
mItems.add(species);
species = new HeroItem();
// species.setName("Giant Panda");
species.setThumbnail(R.drawable.test6);
mItems.add(species);
species = new HeroItem();
//species.setName("Snow Leopard");
species.setThumbnail(R.drawable.test1);
mItems.add(species);
species = new HeroItem();
//species.setName("Dolphin");
species.setThumbnail(R.drawable.test1);
mItems.add(species);
}
#Override
public GridAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, final int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.grid_item, viewGroup, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
HeroItem nature = mItems.get(position);
//viewHolder.tvspecies.setText(nature.getName());
//viewHolder.imgThumbnail.setImageResource(nature.getThumbnail());
Picasso.with(mContext)
.load(nature.getThumbnail())
.into(viewHolder.imgThumbnail);
viewHolder.setListener(new MyOnItemClickListener() {
#Override
public void MyOnClickListener(int position) {
//Log.d("Click","Position: " + position);
switch (position) {
case 0:
Intent intent = new Intent(mContext, Wolverine.class);
mContext.startActivity(intent);
break;
}
}
});
}
#Override
public int getItemCount() {
return mItems.size();
}
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public ImageView imgThumbnail;
//public TextView tvspecies;
public ViewHolder(View itemView) {
super(itemView);
imgThumbnail = (ImageView) itemView.findViewById(R.id.photoView);
//tvspecies = (TextView)itemView.findViewById(R.id.tv_species);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
listener.MyOnClickListener(getAdapterPosition());
}
public void setListener(MyOnItemClickListener onItemClick) {
listener = onItemClick;
}
}
public interface MyOnItemClickListener {
void MyOnClickListener(int position);
}
}
So, I wrote my own listener that needs position of the item, as you can see. I have no problems with showing position in logs Log.d("Click","Position: " + position); , but I still can't to start new Activity.
And here is code of my fragment Activity that uses adapter
import android.graphics.Point;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
public class TabHero extends Fragment {
RecyclerView mRecyclerView;
RecyclerView.LayoutManager mLayoutManager;
RecyclerView.Adapter mAdapter;
int widthRecycler,widthScreen,padding;
public static TabHero newInstance() {
Bundle args = new Bundle();
TabHero fragment = new TabHero();
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.tab_hero, container, false);
// Calling the RecyclerView
mRecyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
// The number of Columns
mLayoutManager = new GridLayoutManager(getActivity(), 2);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new GridAdapter(getContext());
mRecyclerView.setAdapter(mAdapter);
return v;
}
}
What I do wrong?
itemViewHolder.CategoryItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(context, SubcatogaryActivity.class);
i.putExtra("id",Integer.toString(received_dataList.get(position).getId()));
context.startActivity(i);
}
});