Problem with RecyclerView in BottomsheetFragment - android

i am trying to create a bottom sheet fragment with a recycler view. But i am getting an error in recycler view as adapter not set skipping layout
I am positive that i set the adapter for recycler view.I do not seem to understand the problem.
Bottomsheet Fragment code
public class QueueBottomSheetDialog extends BottomSheetDialogFragment {
//Views
private View fragmentView;
private RecyclerView queueRecyclerView;
private QueueRecyclerAdapter queueRecyclerAdapter;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
fragmentView = inflater.inflate(R.layout.queue_bottomsheet,container,false);
Initialize();
populateSongs();
return fragmentView;
}
private void Initialize(){
queueRecyclerView = fragmentView.findViewById(R.id.queue_recyclerView);
queueRecyclerView.setHasFixedSize(true);
}
private void populateSongs(){
queueRecyclerAdapter = new QueueRecyclerAdapter(getContext(),MusicService.songsSet);
queueRecyclerView.setAdapter(queueRecyclerAdapter);
queueRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
}
}
Code for recycler adapter
public class QueueRecyclerAdapter extends RecyclerView.Adapter<QueueRecyclerAdapter.QueueViewHolder> {
private Context context;
private SongSet songSet;
public QueueRecyclerAdapter(Context context,SongSet songSet){
this.context = context;
this.songSet = songSet;
}
#NonNull
#Override
public QueueViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.queue_cardview,parent,false);
return new QueueViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull QueueViewHolder holder, int position) {
holder.songTv.setText(songSet.get(position).getTitle());
holder.artistTv.setText(songSet.get(position).getArtist());
}
#Override
public int getItemCount() {
return songSet.size();
}
public static class QueueViewHolder extends RecyclerView.ViewHolder{
private TextView songTv;
private TextView artistTv;
public QueueViewHolder(#NonNull View itemView) {
super(itemView);
songTv = itemView.findViewById(R.id.sName);
artistTv = itemView.findViewById(R.id.aName);
}
}
}
I am calling like this in my main activity
queueBottomSheetDialog queueBottomSheetDialog = newQueueBottomSheetDialog();
queueBottomSheetDialog.show(getSupportFragmentManager(),"QUEUE BOTTOMSHEET");

Related

Auto focus EditText that sits inside RecyclerView adapter

I've got a Fragment, which has a RecyclerView. This RecyclerView has a header adapter, which contains an EditText.
When the Fragment comes into view, I want the EditText to auto focus (i.e. show the keyboard automatically).
How would I do this?
The Fragment:
public class My Fragment extends Fragment {
private LinearLayoutManager layoutManager;
private FeedRecyclerView recyclerView;
private PostAdapter postAdapter;
private ConcatAdapter concatAdapter;
private HeaderAdapter headerAdapter;
private List<Post> postData = new ArrayList<>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
return inflater.inflate(R.layout.my_fragment, parent, false);
}
#Override
public void onViewCreated(#NonNull View view, Bundle savedInstanceState) {
recyclerView = view.findViewById(R.id.recycler_view);
layoutManager = new FeedLinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
// This is the adapter that contains the EditText
headerAdapter = new HeaderAdapter(getContext());
postAdapter = new PostAdapter(getContext(), postData, layoutManager);
concatAdapter = new ConcatAdapter();
recyclerView.setAdapter(concatAdapter);
concatAdapter.addAdapter(searchHeaderAdapter);
}
}
The adapter class, HeaderAdapter:
public class HeaderAdapter extends RecyclerView.Adapter<HeaderAdapter.ViewHolder> {
private Context context;
public HeaderAdapter(Context context) {
this.context = context;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.my_header, parent, false));
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
//
}
#Override
public long getItemId(int position) {
return 1;
}
#Override
public int getItemCount() {
return 1;
}
public class ViewHolder extends RecyclerView.ViewHolder {
public EditText myEditText;
public ViewHolder(View v) {
super(v);
myEditText = v.findViewById(R.id.my_edit_text);
}
}
}
As you can see, my RecyclerView's adapter, HeaderAdapter, has the EditText. When the Fragment gets shown, I want to auto focus this EditText.
How would I do this?
Thanks!
Add this line myEditText.requestFocus() into init in Header ViewHolder
init {
myEditText = v.findViewById(R.id.my_edit_text)
myEditText.requestFocus()
}
try this
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.myEditText.requestFocus();
((InputMethodManager)holder.itemView.context.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(holder.myEditText, 0);
}

How could I add ViewModel arraylist data into fragment using Recyclerview

I am newbie in android ViewModel.I have tried to add arraylist item using viewmodel and recyclerview but I couldn't.Please help me..
This is an GalaryViewModel:
class GalleryViewModel extends ViewModel {
private MutableLiveData<ArrayList<String>>fruits ;
MutableLiveData<ArrayList<String>> getFruits() {
if(fruits == null){
fruits = new MutableLiveData<>();
loadFruits();
}
return fruits;
}
private void loadFruits(){
ArrayList<String> fruitList = new ArrayList<>();
fruitList.add("Apple");
fruitList.add("Banana");
fruitList.add("Pinaple");
fruitList.add("Apple");
fruitList.add("Cucamber");
fruitList.add("Cocunut");
fruits.setValue(fruitList);
}
}
This is an GalleryFragment: Where I could not adding the setText value inside the public void onChanged() Function
public class GalleryFragment extends Fragment {
private Context context;
private View view;
private GalleryViewModel galleryViewModel;
private ArrayList<String> fruitList;
public GalleryFragment(Context context, View view, GalleryViewModel galleryViewModel, ArrayList<String> fruitList) {
this.context = context;
this.view = view;
this.galleryViewModel = galleryViewModel;
this.fruitList = fruitList;
}
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
RecyclerView recyclerView = view.findViewById(R.id.gRecyclerView);
final GallaryRecyclerView adapter = new GallaryRecyclerView(context,fruitList);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this.context));
galleryViewModel =
ViewModelProviders.of(this).get(GalleryViewModel.class);
View root = inflater.inflate(R.layout.fragment_gallery, container, false);
final TextView textView = root.findViewById(R.id.text_gallery);
galleryViewModel.getFruits().observe(this, new Observer<ArrayList<String>>() {
#Override
public void onChanged(ArrayList<String> strings) {
textView.setText((CharSequence) strings);
}
// #Override
// public void onChanged(#Nullable String s) {
// textView.setText(s);
// }
});
return root;
}
}
This is an GalleryRecyclerview:
public class GallaryRecyclerView extends RecyclerView.Adapter<GallaryRecyclerView.ViewHolder>{
private Context context;
private ArrayList<String> fruitList;
public GallaryRecyclerView(Context context, ArrayList<String> fruitList) {
this.context = context;
this.fruitList = fruitList;
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView fruitsView;
ViewHolder(#NonNull View itemView) {
super(itemView);
fruitsView = itemView.findViewById(R.id.text_gallery);
}
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.fragment_gallery,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.fruitsView.setText(fruitList.get(position));
}
#Override
public int getItemCount() {
return fruitList.size();
}
}
You just have to instantiate fruitList inside onChanged method.
#Override
public void onChanged(ArrayList<String> strings) {
this.fruitList = strings;
}

RecyclerView on a fragment

i am trying to add a recyclerview on a fragment
fragments code:
public class MediaPlayerController extends Fragment{
private static final String TAG = "MediaPlayerController";
private RecyclerView recyclerViewb;
private RecycleViewAdapter myAdapter;
private ArrayList<String> myRecordings = new ArrayList<>();
//private DBHelper dbHelper;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
Log.d(TAG, "onCreateView: started");
View v = inflater.inflate(R.layout.media_player_area, container, false);
recyclerViewb = (RecyclerView)v.findViewById(R.id.recyclerViewXml);
//dbHelper = new DBHelper(v.getContext(), null, null, 1);
myRecordings.add("hau");
myRecordings.add("hau2");
myRecordings.add("hau3");
myRecordings.add("hau4");
myRecordings.add("ha5");
myRecordings.add("hau23");
myRecordings.add("ha31u");
myRecordings.add("haudsa");
System.out.println("what does this print?"+myRecordings);
myAdapter = new RecycleViewAdapter(v.getContext(), myRecordings);
recyclerViewb.setAdapter(myAdapter);
recyclerViewb.setLayoutManager(new LinearLayoutManager(v.getContext()));
Log.d(TAG, "onCreateView: returned");
return v;
}
in the logs i see that that it doesn't even get to the RecycleViewAdapter sections of code at all the fragment is completely blank
i get not errors as well
public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.ViewHolder>{
private static final String TAG = "RecycleViewAdapter";
private Context mContext;
private ArrayList<String> mRecName = new ArrayList<>();
public RecycleViewAdapter(Context mContext, ArrayList<String> mRecName) {
this.mContext = mContext;
this.mRecName = mRecName;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
Log.d(TAG, "onCreateViewHolder: i am here");
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_row_recycle, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(#NonNull final ViewHolder holder, int position) {
Log.d(TAG, "onBindViewHolder: called");
holder.recTxt.setText(mRecName.get(position).toString());
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(mContext, "sas gamaw", Toast.LENGTH_LONG);
}
});
}
#Override
public int getItemCount() {
return 0;
}
public class ViewHolder extends RecyclerView.ViewHolder{
RelativeLayout parentLayout;
TextView recTxt;
public ViewHolder(View itemView) {
super(itemView);
recTxt = itemView.findViewById(R.id.recTextView);
parentLayout = itemView.findViewById(R.id.parentLayoutXml);
}
}
}
in the logs it only displays oncreateView started and the myRecordings arraylist
onCreateView: started
I/System.out: what does this print?[hau, hau2, hau3, hau4, ha5,
hau23, ha31u, haudsa]
MediaPlayerController: onCreateView: returned
i can't think of what is going wrong :/ please help out
i also want to add that i am fairly new to android studio thanks
your code problem it is in Recycler Adapter in getItemCount() method,
You don't return any Item , you must change it to :
#Override
public int getItemCount() {
return mRecName.size();
}

How to grey out fragment activity in android on condition

I want to grey out fragment activity on some condition, I am trying to set a flag in pojo class and checking every time i try to grey out an activity.
I do not know where exactly the logic should be placed. I tried to put it inside a onCreateView() method, but it gave me an null pointer exception.
Please find the code below for your reference.
#SuppressLint("ValidFragment")
public class QuizTab extends Fragment {
private static final String TAG = "QuizTab";
RecyclerView recyclerView;
View view;
LinearLayout linearLayout;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.quiztab, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
if(flag){
grey out logic ---- not working
}
getQuizContent();
setupRecyclerView(recyclerView);
return view;
}
private void setupRecyclerView(RecyclerView recyclerView) {
recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
recyclerView.setAdapter(new SimpleStringRecyclerViewAdapter(getActivity()));
}
public class SimpleStringRecyclerViewAdapter extends RecyclerView.Adapter<SimpleStringRecyclerViewAdapter.ViewHolder> {
private Context mContext;
public class ViewHolder extends RecyclerView.ViewHolder {
public final LinearLayout mLinearlayout;
public ViewHolder(View view) {
super(view);
mLinearlayout = (LinearLayout) view.findViewById(R.id.mcq);
}
}
public SimpleStringRecyclerViewAdapter(Context context) {
mContext = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.quiztab_mcq, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
}
#Override
public int getItemCount() {
return 1;
}
}
}
LinearLayout in this case is parent of fragment i tryed to paint fragment but failure so i did something like that
if(flag){
LinearLayout layout = view.findViewById(R.id.parentOffragment);
layout.setBackgroundColor(Color.GRAY);
}

Change content of a DialogFragment from a Recyclerview Adapter

I'm not quite sure if this is possible, but I'm trying to change the content of a custom DialogFragment (read: content as in 1 imageview, and 3 textviews) inside of a Recyclerview adapter.
My current custom DialogFragment:
public class ServiceDialogFragment extends DialogFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.service_card, container, false);
}
}
And the layout:
As you see in the image above: I have a list behind the where the DialogFragment is displayed and whenever I press one of those "buttons" that DialogFragment appears. But my question is: how do I change the content of the custom DialogFragment inside of my Recyclerview Adapter?
My adapter:
public class ServiceAdapter extends RecyclerView.Adapter<ServiceAdapter.ViewHolder>{
private final String TAG = "******* Service Adapter";
private ArrayList<ServiceObject> mDataset;
private Context mContext;
private Resources mResources;
private Context location;
public ServiceAdapter(ArrayList<ServiceObject> mDataset, Context mContext, Resources mResources) {
this.mDataset = mDataset;
this.mContext = mContext;
this.mResources = mResources;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
location = parent.getContext();
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.service_box, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.title.setText(mDataset.get(position).getTitle());
holder.icon.setImageResource(mResources.getIdentifier(Integer.toString(mDataset.get(position).getIcon()), "drawable", "net.example.adapter"));
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
#Override
public int getItemCount() {
return mDataset == null ? 0 : mDataset.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView title, desc, price;
ImageView icon;
public ViewHolder(View v) {
super(v);
title = (TextView) v.findViewById(R.id.service_box_title);
price = (TextView) v.findViewById(R.id.service_card_price);
icon = (ImageView) v.findViewById(R.id.service_box_icon);
v.setOnClickListener(this);
}
#Override
public void onClick(View v) {
FragmentActivity activity = (FragmentActivity)(mContext);
FragmentManager fm = activity.getSupportFragmentManager();
ServiceDialogFragment alertDialog = new ServiceDialogFragment();
alertDialog.show(fm, "display_service");
}
}
}
Thanks to #raghunandan, I figured it out. Resource: http://developer.android.com/reference/android/app/DialogFragment.html

Categories

Resources