Android onClick toggle for RecyclerView items - android

Running this code as I have it now, I can build a list of items, where when clicked, the background will change to gray as if the item is selected. Click again, and the background returns to white. The problem I am having and can't figure out is how can I keep the background color permanently gray like a shared preference or something so when the app changes orientation, or is exited out, the selected items stay selected until clicked again to be unselected?
public class CampingItemRecyclerViewAdapter extends RecyclerView.Adapter<CampingItemRecyclerViewAdapter.ViewHolder>{
int i;
public final List<CampingItem> campingItems;
ListListener listListener;
public CampingItemRecyclerViewAdapter(List<CampingItem> campingItems){
this.campingItems = campingItems;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recycler_item, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
final CampingItem campingItem = campingItems.get(position);
if (campingItem != null){
holder.campingItem = campingItem;
holder.txtLine1.setText(campingItem.getItemName());
holder.txtLine2.setText(campingItem.getItemCost());
holder.txtLine3.setText(campingItem.getInStock());
holder.txtLine4.setText(campingItem.getLocation());
holder.root.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (i == 0){
v.setBackgroundColor(Color.GRAY);
i++;
}
else if (i == 1) {
v.setBackgroundColor(Color.WHITE);
i = 0;
}
}
});
holder.root.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
Bundle bundle = new Bundle();
bundle.putInt("campingItem_pk", campingItem.get_id());
CampingItemDetailsDialogFragment detailsFragment = new CampingItemDetailsDialogFragment();
detailsFragment.setArguments(bundle);
AppCompatActivity activity = (AppCompatActivity) v.getContext();
activity.getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, detailsFragment)
.addToBackStack(null)
.commit();
return true;
}
});
}
}
#Override
public int getItemCount() {
return campingItems.size();
}
public void addItems(List<CampingItem> campingItems){
this.campingItems.clear();
this.campingItems.addAll(campingItems);
notifyDataSetChanged();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public View root;
public CampingItem campingItem;
public TextView txtLine1, txtLine2, txtLine3, txtLine4;
public ViewHolder(View itemView){
super (itemView);
root = itemView;
txtLine1 = (TextView)root.findViewById(R.id.txtLine1);
txtLine2 = (TextView)root.findViewById(R.id.txtLine2);
txtLine3 = (TextView)root.findViewById(R.id.txtLine3);
txtLine4 = (TextView)root.findViewById(R.id.txtLine4);
}
}
}
public class MainActivityFragment extends Fragment {
View root;
private RecyclerView recyclerView;
private CampingItemRecyclerViewAdapter campingItemRecyclerViewAdapter;
private int columnCount = 1;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
root = inflater.inflate(R.layout.fragment_main, container, false);
recyclerView = (RecyclerView)root.findViewById(R.id.recyclerView);
return root;
}
#Override
public void onResume() {
super.onResume();
Context context = getContext();
campingItemRecyclerViewAdapter = new CampingItemRecyclerViewAdapter(new ArrayList<CampingItem>());
if(columnCount <= 1){
recyclerView.setLayoutManager(new LinearLayoutManager(context));
}else{
recyclerView.setLayoutManager(new GridLayoutManager(context, columnCount));
}
recyclerView.setAdapter(campingItemRecyclerViewAdapter);
recyclerView.setHasFixedSize(false);
ViewModelProviders.of(this)
.get(AllCampingItemsViewModel.class)
.getCampingItemList(context)
.observe(this, new Observer<List<CampingItem>>() {
#Override
public void onChanged(List<CampingItem> campingItems) {
if (campingItems != null){
campingItemRecyclerViewAdapter.addItems(campingItems);
}
}
});
}
}
public class MainActivity extends AppCompatActivity {
FragmentManager fm;
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Camping List");
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
fm = getSupportFragmentManager();
fm.beginTransaction()
.add(android.R.id.content, new NewCampingItemDialogFragment())
.addToBackStack(null)
.commit();
}
});
}
}

the functionallity you look for is part of the recyclerview-selection library. Take a look at this. Unfortunately it is for kotlin, but you should be able to rewrite it in java without problems.

Related

No Data in Recycler View in Fragment

I am building an Android Application which have Tab Layout in which there are Fragments and in Fragment I am displaying some value.
I have just only added hardcoded values to test the layout, but even these are not visible in the fragment.
Here is the code for ViewPagerAdapter:
public class FavouriteViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> favouriteFragmentList = new ArrayList<>(); // this line can cause crashes
private final List<String> favouriteFragmentListTitles = new ArrayList<>();
public FavouriteViewPagerAdapter(#NonNull FragmentManager fm, int behavior) {
super(fm, behavior);
}
#NonNull
#Override
public Fragment getItem(int position) {
return favouriteFragmentList.get(position);
}
#Override
public int getCount() {
return favouriteFragmentListTitles.size();
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
return favouriteFragmentListTitles.get(position);
}
public void AddFragment(Fragment fragment, String title) {
favouriteFragmentList.add(fragment); // this line can cause crashes
favouriteFragmentListTitles.add(title);
}
}
I have tried the answers available already none of them solves my problem.
I debugged the application and my RecyclerViewAdapter is not even called, very strange. I have done this many time but this never happened.
Here is my code for RecyclerViewAdapter :
public class ProductsRecyclerAdapter extends RecyclerView.Adapter<ProductsRecyclerAdapter.viewHolder> {
Context context;
private List<FavouriteProducts> favouriteProductsData;
public ProductsRecyclerAdapter(Context context, List<FavouriteProducts> favouriteProductsData) {
this.context = context;
this.favouriteProductsData = favouriteProductsData;
}
#NonNull
#Override
public viewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.single_favourite_product_item, parent, false);
viewHolder vHolder = new viewHolder(v);
return vHolder;
}
#Override
public void onBindViewHolder(#NonNull viewHolder holder, int position) {
holder.product_name.setText(favouriteProductsData.get(position).getProduct_name());
holder.product_price.setText(favouriteProductsData.get(position).getProduct_price());
holder.product_image.setImageResource(favouriteProductsData.get(position).getProduct_image());
Toast.makeText(context,favouriteProductsData.get(position).getProduct_price() , Toast.LENGTH_SHORT).show();
/*holder.heart_button.setOnLikeListener(new OnLikeListener() {
#Override
public void liked(LikeButton likeButton) {
showSuccessToast(context,"Liked");
}
#Override
public void unLiked(LikeButton likeButton) {
showSuccessToast(context,"Unliked");
}
});*/
}
#Override
public int getItemCount() {
return favouriteProductsData.size();
}
static class viewHolder extends RecyclerView.ViewHolder {
MaterialTextView product_name, product_price;
ImageView product_image;
LikeButton heart_button;
public viewHolder(#NonNull View itemView) {
super(itemView);
product_name = itemView.findViewById(R.id.product_name);
product_price = itemView.findViewById(R.id.product_price);
product_image = itemView.findViewById(R.id.product_image);
heart_button = itemView.findViewById(R.id.heart_button);
}
}
}
I am not getting any errors but after I check adapter is not even called. Some help will be really helpful.
Fragment Code:
public class FavouriteProductsFragment extends Fragment {
View v;
private List<FavouriteProducts> productList;
public FavouriteProductsFragment() {
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
productList = new ArrayList<>();
productList.add(new FavouriteProducts("All Weather", "200/Ltr", R.drawable.all_weather));
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
v = inflater.inflate(R.layout.products_favourite_fragment, container, false);
RecyclerView productRecyclerView = v.findViewById(R.id.favourite_products_rv);
ProductsRecyclerAdapter recyclerAdapter = new ProductsRecyclerAdapter(getContext(), productList);
productRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
productRecyclerView.hasFixedSize();
productRecyclerView.setAdapter(recyclerAdapter);
return v;
}
}
productList = new ArrayList<>();
productList.add(new FavouriteProducts("All Weather", "200/Ltr",R.drawable.all_weather));
recyclerAdapter.notifyDataSetChanged()
RecyclerView productRecyclerView = v.findViewById(R.id.favourite_products_rv);
ProductsRecyclerAdapter recyclerAdapter = new ProductsRecyclerAdapter(getContext(), productList);
productRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
productRecyclerView.hasFixedSize();
productRecyclerView.setAdapter(recyclerAdapter);
You need to add
RecyclerView productRecyclerView = v.findViewById(R.id.favourite_products_rv);
ProductsRecyclerAdapter recyclerAdapter = new ProductsRecyclerAdapter(getContext(), productList);
productRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
productRecyclerView.hasFixedSize();
productRecyclerView.setAdapter(recyclerAdapter);
productList = new ArrayList<>();
productList.add(new FavouriteProducts("All Weather", "200/Ltr",R.drawable.all_weather));recyclerAdapter.notifyDataSetChanged()
and Remove oncreate() method
try productRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); befor setting adapter in recyclerview.
Step1: First Create one or MoreFragment For Your ViewPager and in This Fragment Add your Recyclerview Code as per Your Requirement.
Like Below Code
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_whatsapp_image, container, false);
context = getActivity();
((HomeActivity) getActivity()).toolbar.setVisibility(View.VISIBLE);
setupView(root);
return root;
}
private void setupView(ViewGroup root) {
recyclerView = (RecyclerView) root.findViewById(R.id.recyclerViewImage);
progressBar=root.findViewById(R.id.simpleProgressBar);
txtinfo=root.findViewById(R.id.txtinfo);
setUPList();
}
Step2: Then Create ViewPager Adapter for Your TabLayout
public class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
#Override
public Fragment getItem(int position) {
if(position == 0) return new AudioFragment();
if(position == 1) return new VideoFragment();
if(position == 2) return new ImageFragment();
throw new IllegalStateException("Unexpected position " + position);
}
#Override
public int getCount() {
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
if(position == 0) return "Audio";
if(position == 1) return "Video";
if(position == 2) return "Image";
throw new IllegalStateException("Unexpected position " + position);
}
}
Step3 : Then Call Your SetupViewpager Method in Your Oncreate method of your Activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar=findViewById(R.id.toolbar);
viewPager=findViewById(R.id.viewpager);
tabLayout=findViewById(R.id.tabs);
//add toolbar
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setupViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
setTabIcons();
}
NOTE: You Can Download ViewPager Code from Below Link.Click Here to View and Download

Starting a fragment on RecyclerView Adapter

I've read multiple posts regarding it; but for my specific case, I can't seem to make it work. The fragment doesn't seem to be replaced when the view in the adapter is clicked.
Related posts:
start Fragment from RecycleView Adapter Onclick
how to open a different fragment on recyclerview OnClick
Here is my RecyclerView
public class LocationFilterAdapter extends RecyclerView.Adapter<LocationFilterAdapter.LocationFilterViewHolder> {
public Context mActivity;
#Override
public LocationFilterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.profile_tab_become_a_host_basic_questions_location_filter_item, parent, false);
mActivity = parent.getContext();
return new LocationFilterAdapter.LocationFilterViewHolder(view);
}
#Override
public void onBindViewHolder(LocationFilterViewHolder holder, int position) {
holder.bindView(position);
}
#Override
public int getItemCount() {
return LocationFilterFragment.RESULT_SIZE;
}
public class LocationFilterViewHolder extends RecyclerView.ViewHolder{
TextView tvLocations;
public LocationFilterViewHolder(View itemView) {
super(itemView);
tvLocations = (TextView) itemView.findViewById(R.id.tvFilterLocations);
}
public void bindView(int position){
final String output = LocationFilterFragment.STREET_NAME + ", " + LocationFilterFragment.CITY_NAME
+", " + LocationFilterFragment.STATE_NAME + ", " + LocationFilterFragment.COUNTRY_NAME;
tvLocations.setText(output);
tvLocations.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText etStreet = (EditText) LocationFilterFragment.mView.findViewById(R.id.etStreetFilter);
etStreet.setText(output);
LocationFragment locationFragment = new LocationFragment();
locationFragment.setArguments(bundle);
//Fragment will not start
((AppCompatActivity) mActivity).getSupportFragmentManager().beginTransaction().replace(R.id.progressFragment, new LocationFragment()).commit();
}
});
}
Here is my LocationFragment (SupportFragment)
public class LocationFragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.profile_tab_become_a_host_basic_questions_location_fragment, container, false);
Log.d("fragment lifecycle", "OnCreateView");
if(getArguments() != null){
TextView tvStreet = (TextView) view.findViewById(R.id.tvStreet);
tvStreet.setText(getArguments().getString("STREET_NAME"));
TextView tvState = (TextView) view.findViewById(R.id.tvState);
tvState.setText(getArguments().getString("STATE_NAME"));
TextView tvCity = (TextView) view.findViewById(R.id.tvCity);
tvCity.setText(getArguments().getString("CITY_NAME"));
}
return view;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.findViewById(R.id.layoutStreet).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getFragmentManager().beginTransaction()
.replace(R.id.progressFragment, new LocationFilterFragment()).addToBackStack("locationFragment").commit();
}
});
view.findViewById(R.id.bContinue).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getFragmentManager().beginTransaction()
.replace(R.id.progressFragment, new MapFragment()).addToBackStack(null).commit();
}
});
view.findViewById(R.id.layoutTapInfo).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getFragmentManager().beginTransaction()
.replace(R.id.progressFragment, new LocationInfoFragment()).addToBackStack("locationFragment").commit();
}
});
}
}
This is embarrassing but, the way I did it was right. For some reason. my Android Studio was acting weird; when I rebuild my project, the code totally worked :)

I get RecycleView in Fragment and after click on recycleview element i need to send data to this fragment and restart it

Hi i have recycleView in my fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_list_petrole, container, false);
recyclerView = (RecyclerView)view.findViewById(R.id.recycle_view);
allPetroles = getArguments().getParcelableArrayList("petroles");
mLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mLayoutManager);
mAdapter = new RecyclerAdapter(allPetroles, getActivity());
recyclerView.setAdapter(mAdapter);
return view;
}
In RecycleView adapter i set OnClickListener in onBindViewHolder, so i need after click on some raw sent back to Fragment some info(position) and then make some changes(resent this info to Activity and add some new view there on Gmaps)
This onBindViewHolder:
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.linearClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
positionClick = position;
notifyDataSetChanged();
--------------I NEED CODE HERE----------------------
}
});
if (positionClick == position) {
holder.linearClick.setSelected(true);
}
else {
holder.linearClick.setSelected(false);
}
and i don't understand how i should this do
In RecyclerAdapter :
1- add an Inner Interface :
/**********
*
* Inner interface
*
*/
public interface OnClickListener {
void onItemClicked(Item item);
}
2- Add a parameter in the constructor :
public RecyclerAdapter(Context context, List<Item> data, OnClickListener onClickListener) {
mContext = context;
mData = data;
mListener = onClickListener
}
3- Call the listener :
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.linearClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//positionClick = position;
//you don't need this here : notifyDataSetChanged();
mListener.onItemClicked(mData.get(position));
}
});
if (positionClick == position) {
holder.linearClick.setSelected(true);
}
else {
holder.linearClick.setSelected(false);
}
Your Fragment should implements RecyclerAdapter.OnClickListener
public class YourFragment extends Fragment implements RecyclerAdapter.OnClickListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_list_petrole, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
recyclerView = (RecyclerView)view.findViewById(R.id.recycle_view);
allPetroles = getArguments().getParcelableArrayList("petroles");
mLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mLayoutManager);
mAdapter = new RecyclerAdapter(getActivity(), allPetroles, this);
recyclerView.setAdapter(mAdapter);
}
#Override
void onItemClicked(Item item) {
// TODO put your code here
}
}

RecyclerView.Adapter onClick ClassCastException

I'm trying to load a Fragment inside an Activity and I was able to load the items. Now, what I want to do is to click the items on the list and pass it to the DetailsView.class which is a Fragment that will receive the bundle data. But everytime I click on the item I always get an error. Below is the logcat error that I'm getting
Logcat Error
java.lang.ClassCastException: com.test.example.LoadAFragment cannot be cast to com.test.example.MainActivity at com.test.example.controller.DetailsView.onCreateView(DetailsView.java:178)
line java:178
MainActivity activity = ((MainActivity) getActivity());
LoadAFragment.class
public class LoadAFragment extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.load_layout);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
MyFragment myFragment = new MyFragment();
fragmentTransaction.add(R.id.frame_container, myFragment);
fragmentTransaction.commit();
}
}
RecyclerView Adapter
public class MyFragmentAdapter extends RecyclerView.Adapter <RecyclerView.ViewHolder> {
private List<ListModel> list;
private Context mContext = null;
private DetailsView detailsView;
public MyFragmentAdapter(Context context, List<ListModel> list) {
mContext = context;
this.list = list;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
final ListModel list = list.get(holder.getAdapterPosition());
String mID = list.getID();
final int id = Integer.valueOf(mID );
((MyViewHolder) holder).title.setText(list.getTitle());
((MyViewHolder) holder).caption.setText(list.getCaption());
((MyViewHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager manager = ((Activity) mContext).getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
Bundle bundle = new Bundle();
bundle.putInt("id", id);
detailsView = new DetailsView ();
detailsView.setTitle(title);
detailsView.setArguments(bundle);
transaction.replace(R.id.frame_container, detailsView);
transaction.addToBackStack("list");
transaction.commit();
}
});
}
#Override
public int getItemCount() {
return (list != null? list.size():0);
}
private class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title;
public TextView caption;
MyViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.title);
caption = (TextView) itemView.findViewById(R.id.caption );
}
}
}
MyFragment.class
public class MyFragment extends Fragment{
List<ListModel> list;
RecyclerView mRecyclerView;
MyFragmentAdapter myFragmentAdapter;
ListDb listDb;
public MyFragment() {}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View rootView = inflater.inflate(R.layout.fragment, container, false);
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerview);
listDb = new ListDb(getActivity());
list = listDb.getList();
myFragmentAdapter = new MyFragmentAdapter (getActivity(), list);
mRecyclerView.setAdapter(myFragmentAdapter);
return rootView;
}
}
DetailsView.class
public class DetailsView extends Fragment{
private MainActivity activity;
Bundle b;
public DetailsView() {}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null)
b = savedInstanceState.getBundle("save");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
activity = ((MainActivity) getActivity());
return rootView;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);}
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (b != null) {
setTitle(b.getString("title"));
id = b.getInt("id");
}
if (b != this.getArguments().getInt("id")) {
b = this.getArguments().getInt("id");
//get data from id
}
}
}
The best and cleanest solution would be to pass a listener to the adapter.
public interface MyClickListener {
void onResult(Data data);
}
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public MyAdapter(Context context, MyClickListener listener, List<ListModel> list) {
mContext = context;
this.list = list;
this.listener = listener;
}
#Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
Data data = getData(position);
//bind views
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.onResult(data);
}
});
}
}
Have the Fragment implement the listener and create the new adapter.
public class MyFragment extends Fragment implements MyClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyAdapter adapter = new MyAdapter(getActivity(),this,listData);
}
#Override
public void onResult(Data data) {
//do work after an item is clicked
}
}
Cast getActivity() to 'LoadAFragment' instead of 'MainActivity' in DetailsView.java. This way:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
activity = ((LoadAFragment) getActivity());
return rootView;
}
try this way to get Reference of MainActivity in your DetailViewFragment
private MainActivity parent_activity;
#Override
public void onAttach(Activity activity)
{
parent_activity = (MainActivity.class.isAssignableFrom(activity
.getClass())) ? (MainActivity) activity : null;
super.onAttach(activity);
}
#Override
public void onDetach()
{
parent_activity = null;
super.onDetach();
}
Now use the reference using:
if(parent_activity!=null)
{
parent_activity.someMethod();
}

RecyclerView.Adapter onClick

I am having trouble implementing view.OnClickListener in my RecyclerView.Adapter. I am trying to implement multi-pane layout (landscape tablet layout version with RecyclerView and detail side by side). I have so far created this Activity:
public class MovieListActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_movie_list);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
MovieListFragment movieListFragment = new MovieListFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.recycler_view_fragment, movieListFragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
which starts a Fragment:
public class MovieListFragment extends Fragment {
#BindView(R.id.recyclerView)
RecyclerView mRecyclerView;
private MoviesAdapter mAdapter;
public MovieListFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAdapter = new MoviesAdapter(getContext());
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_movie_list, container, false);
ButterKnife.bind(this,view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
mRecyclerView.setAdapter(mAdapter);
return view;
}
#Override
public void onStart() {
super.onStart();
getPopularMovies();
}
private void getPopularMovies() {
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("http://api.themoviedb.org")
.setLogLevel(RestAdapter.LogLevel.FULL)
.build();
MoviesApiService service = restAdapter.create(MoviesApiService.class);
service.getPopularMovies(new Callback<Movie.MovieResult>() {
#Override
public void success(Movie.MovieResult movieResult, Response response) {
mAdapter.setMovieList(movieResult.getResults());
}
#Override
public void failure(RetrofitError error) { error.printStackTrace(); }
});
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
}
#Override
public void onDetach() {
super.onDetach();
}
}
which calls the RecyclerView.Adapter:
public class MoviesAdapter extends RecyclerView.Adapter<MovieViewHolder> {
private List<Movie> mMovieList;
private LayoutInflater mInflater;
private Context mContext;
public MoviesAdapter(Context context) {
this.mContext = context;
this.mInflater = LayoutInflater.from(context); //
}
#Override
public MovieViewHolder onCreateViewHolder(ViewGroup parent, final int viewType) {
View view = mInflater.inflate(R.layout.movie_row_item, parent, false);
final MovieViewHolder viewHolder = new MovieViewHolder(view);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int position = viewHolder.getAdapterPosition();
Intent intent = new Intent(mContext, MovieDetailActivity.class);
intent.putExtra(MovieDetailActivity.MOVIE_EXTRA, mMovieList.get(position));
mContext.startActivity(intent);
}
});
return viewHolder;
}
#Override
public void onBindViewHolder(MovieViewHolder holder, int position) {
Movie movie = mMovieList.get(position);
Picasso.with(mContext)
.load(movie.getPoster())
.placeholder(R.color.colorAccent)
.into(holder.thumbnail);
holder.movieTitle.setText(movie.getTitle());
holder.rating.setText(movie.getRating());
}
#Override
public int getItemCount() {
return (mMovieList == null) ? 0 : mMovieList.size();
}
public void setMovieList(List<Movie> movieList) {
this.mMovieList = new ArrayList<>();
this.mMovieList.addAll(movieList);
notifyDataSetChanged();
}
}
Here in onClick I need to decide whether to start a new Activity (like in my code) or update content of detail Fragment (in my tablet landscape layout).
According to this link, you should decide based on fact if your DetailFragment is != null in activity like this:
public void onItemSelected(int position) {
DisplayFragment displayFrag = (DisplayFragment) getFragmentManager()
.findFragmentById(R.id.display_frag);
if (displayFrag == null) {
// DisplayFragment (Fragment B) is not in the layout (handset layout),
// so start DisplayActivity (Activity B)
// and pass it the info about the selected item
Intent intent = new Intent(this, DisplayActivity.class);
intent.putExtra("position", position);
startActivity(intent);
} else {
// DisplayFragment (Fragment B) is in the layout (tablet layout),
// so tell the fragment to update
displayFrag.updateContent(position);
}
}
But I'm not able to check this in Adapter. Any help would be highly appreciated!
Thank you.
Created the custom interface click action to get the control in fragment class
i hope it will works for you
public class MoviesAdapter extends RecyclerView.Adapter {
MoviesAdapterCallback moviesAdapterCallback;
private List<Movie> mMovieList;
private LayoutInflater mInflater;
private Context mContext;
public MoviesAdapter(Context context,MoviesAdapterCallback moviesAdapterCallback) {
this.mContext = context;
this.moviesAdapterCallback =moviesAdapterCallback;
this.mInflater = LayoutInflater.from(context); //
}
#Override
public MovieViewHolder onCreateViewHolder(ViewGroup parent, final int viewType) {
View view = mInflater.inflate(R.layout.movie_row_item, parent, false);
final MovieViewHolder viewHolder = new MovieViewHolder(view);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
/* int position = viewHolder.getAdapterPosition();
Intent intent = new Intent(mContext, MovieDetailActivity.class);
intent.putExtra(MovieDetailActivity.MOVIE_EXTRA, mMovieList.get(position));
mContext.startActivity(intent);
*/ moviesAdapterCallback.MovieClicked(view);
}
});
return viewHolder;
}
#Override
public void onBindViewHolder(MovieViewHolder holder, int position) {
Movie movie = mMovieList.get(position);
Picasso.with(mContext)
.load(movie.getPoster())
.placeholder(R.color.colorAccent)
.into(holder.thumbnail);
holder.movieTitle.setText(movie.getTitle());
holder.rating.setText(movie.getRating());
}
#Override
public int getItemCount() {
return (mMovieList == null) ? 0 : mMovieList.size();
}
public void setMovieList(List<Movie> movieList) {
this.mMovieList = new ArrayList<>();
this.mMovieList.addAll(movieList);
notifyDataSetChanged();
}
public interface MoviesAdapterCallback {
void MovieClicked(View view);
}
}
in Fragment Class
public class MovieListFragment extends Fragment {
#BindView(R.id.recyclerView)
RecyclerView mRecyclerView;
private MoviesAdapter mAdapter;
public MovieListFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_movie_list, container, false);
ButterKnife.bind(this,view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
mAdapter = new MoviesAdapter(getContext(), new MoviesAdapter.MoviesAdapterCallback(){
#Override
public void MovieClicked(View view) {
DisplayFragment displayFrag = (DisplayFragment) getFragmentManager()
.findFragmentById(R.id.display_frag);
if (displayFrag == null) {
// DisplayFragment (Fragment B) is not in the layout (handset layout),
// so start DisplayActivity (Activity B)
// and pass it the info about the selected item
Intent intent = new Intent(this, DisplayActivity.class);
intent.putExtra("position", position);
startActivity(intent);
} else {
// DisplayFragment (Fragment B) is in the layout (tablet layout),
// so tell the fragment to update
displayFrag.updateContent(position);
}
}
});
mRecyclerView.setAdapter(mAdapter);
return view;
}
#Override
public void onStart() {
super.onStart();
getPopularMovies();
}
private void getPopularMovies() {
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("http://api.themoviedb.org")
.setLogLevel(RestAdapter.LogLevel.FULL)
.build();
MoviesApiService service = restAdapter.create(MoviesApiService.class);
service.getPopularMovies(new Callback<Movie.MovieResult>() {
#Override
public void success(Movie.MovieResult movieResult, Response response) {
mAdapter.setMovieList(movieResult.getResults());
}
#Override
public void failure(RetrofitError error) { error.printStackTrace(); }
});
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
}
#Override
public void onDetach() {
super.onDetach();
}
}

Categories

Resources