I have used the following code to autoscroll the Horizontal recyclerview.
private final Runnable SCROLLING_RUNNABLE = new Runnable() {
#Override
public void run() {
recyclerViewDate.smoothScrollBy(pixelsToMove, 0);
setDateValue();
mHandler.postDelayed(this, duration);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_center_item_finder);
txtSelected=(TextView)findViewById(R.id.txtSelected);
getRecyclerviewData();
mHandler.postDelayed(SCROLLING_RUNNABLE, 100);
}
But I'm not able to perform click on recycler items, while scrolling.
Found this solution, created a custom recyclerview. Android - the item inside RecyclerView can't be clicked after scroll
Still click not working.
Related
I am using recycler view in a fragment. Now I want to open a new fragment on recycler view item click. I have used the following code in my recycler view adapter. Please solve my problem.
public void onBindViewHolder(#NonNull MyViewHolder holder, final int position) {
holder.tv_name.setText(mData.get(position).getName());
holder.img.setImageResource(mData.get(position).getPhoto());
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AppCompatActivity activity = (AppCompatActivity) v.getContext();
MiscellaneousFragment miscellaneousfragment = new MiscellaneousFragment();
activity.getSupportFragmentManager().beginTransaction().replace(R.id.linerr, MiscellaneousFragment).addToBackStack(null).commit();
}
});
But I am getting this error in Logcat.
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
at androidx.fragment.app.FragmentTransaction.doAddOp.
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MiscellaneousFragment miscellaneousfragment = new MiscellaneousFragment();
FragmentManager fragmentManager =currentfragment.getFragmentManager()
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction()
fragmentTransaction.add(R.id.linerr, miscellaneousfragment)
fragmentTransaction.addToBackStack(null)
fragmentTransaction.commit()
}
});
I want to start a dialogFragment on recycler view item click in RecyclerView Adapter
I tried
FragmentManager fr = ((Activity)context).getFragmentManager();
AnswersDialogFragment msgDialog = new AnswersDialogFragment();
msgDialog.show(fr, "Dialog");
but i get casting error
java.lang.ClassCastException: android.view.ContextThemeWrapper cannot be cast to android.support.v4.app.FragmentActivity
I want to start the dialog fragment on item click
#Override
public AnswersViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.answercard, parent, false);
AnswersViewHolder evh = new AnswersViewHolder(v, mListener);
return evh;
}
#Override
public void onBindViewHolder(AnswersViewHolder holder, int position) {
int viewType = getItemViewType(position);
if(viewType==COMMENT_TYPE){
final Comments item = (Comments) commentsList.get(position);
holder.UserName.setText(Html.fromHtml(item.getUserName()));
holder.CommentText.setText(Html.fromHtml(item.getCommentText()));
}else{
final Replys replyitem = (Replys) commentsList.get(position);
holder.UserName.setText(Html.fromHtml(replyitem.getReplyAuthor()));
holder.CommentText.setText(Html.fromHtml(replyitem.getReplyBody()));
}
}
imgLikeComment.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager fr = ((Activity)context).getFragmentManager();
AnswersDialogFragment msgDialog = new AnswersDialogFragment();
msgDialog.show(fr, "Dialog");
});
AnswersDialogFragment.java
public class AnswersDialogFragment extends DialogFragment {
ArrayList<Object> answersList;
RecyclerView recyclerView;
AnswersAdapter recyclerViewadapter;
RecyclerView.LayoutManager recyclerViewlayoutManager;
ImageView sendReply ;
EditText commentInput;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, R.style.DialogStyle);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.nestedcomments, container, false);
..... etc
The Item click is working fine , i just can't start the fragment
try to use SupportFragmentManager instead
FragmentManager manager = ((AppCompatActivity)context).getSupportFragmentManager();
I'm newbie in android and I'm trying to develop some applications. But last two weeks I've been stuck in this code. In short, I have two buttons in a layout. These buttons are Add and delete button. If I clicked the add button, add will be disabled and delete will be enabled. Or if I clicked the delete, delete will be disabled add will be enabled. I want to save state of these two buttons (which one is clicked) and get the last clicked state on create. But the problem is it's not working. When I open the app both buttons are show disable state. Or not saving either. Never working.
public class UserVideoInfoActivity2 extends AppCompatActivity {
SharedPreferences.Editor editor;
SharedPreferences preferences;
SharedPreferences.Editor editor2;
SharedPreferences preferences2;
Button buttonadd;
Button buttondelete;
private static final String ADD = "ADD_KEY";
private final static String DELETE = "DELETE_KEY";
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_video_info_layout_2);
buttonadd = (Button) findViewById(R.id.buttonaddlayout);
buttondelete = (Button) findViewById(R.id.buttondeletelayout);
preferences = getSharedPreferences(ADD, Context.MODE_PRIVATE);
preferences2 = getSharedPreferences(DELETE, Context.MODE_PRIVATE);
buttonadd.setEnabled(GetState());
buttondelete.setEnabled(GetState2());
buttonadd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
buttonadd.setEnabled(false);
buttondelete.setEnabled(true);
SaveState(buttonadd.isEnabled());
}
});
buttondelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
buttonadd.setEnabled(true);
buttondelete.setEnabled(false);
SaveState2(buttondelete.isEnabled());
}
});
}
private void SaveState(boolean isChecked) {
editor = preferences.edit();
editor.putBoolean(ADD, isChecked);
editor.commit();
}
private void SaveState2(boolean isChecked2) {
editor2 = preferences2.edit();
editor2.putBoolean(DELETE, isChecked2);
editor2.commit();
}
public boolean GetState() {
return preferences.getBoolean(ADD, false );
}
public boolean GetState2() {
return preferences2.getBoolean(DELETE, false);
}
}
<Button
android:id="#+id/buttondeletelayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="delete" />
What I'm trying to do is change the default backgrounds of a custom DialogFragment that I have written. Normally, I would do this by changing the XML layout file, however, for a DialogFragment these buttons don't exist in the layout file.
In essence, I'm trying to get access to the setPositiveButton, setNegativeButton and setNeutral buttons in order to modify them. Alternatively, I would next try to do this by getting them by id, however, since they aren't defined in a layout file, I do not have a corresponding id for them. I've found plenty examples of how to modify the rest of the layout, but I can't find anywhere that positive/neutral/negative buttons are modifiable.
Ideally, I could do this in the following block of code:
.setPositiveButton(R.string.add, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
...
}
})
Thanks in advance.
Here is the code ... The button instance is valid only after the dialog created. Hope this helps you.
public static class CustomDialog extends DialogFragment
{
public static CustomDialog newInstance()
{
return new CustomDialog();
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
super.onCreateDialog(savedInstanceState);
Builder builder = new AlertDialog.Builder(getActivity());
AlertDialog dialog = builder.create();
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK",new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "CANCEL",new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
return dialog;
}
#Override
public void onStart()
{
super.onStart();
Button pButton = ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_POSITIVE);
Button nButton = ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_NEGATIVE);
pButton.setBackgroundColor(getResources().getColor(R.color.Blue));
nButton.setBackgroundColor(getResources().getColor(R.color.Green));
}
}
I have image like pieces of pizza, and I want implement onClickListener only when I click on pizza?
pizzaImageView.setClickable(true);
pizzaImageView.setOnClickListener(pizzaClickListener);
private OnClickListener pizzaClickListener = new OnClickListener() {
public void onClick(View view){
//Do code here
}
};