I'm trying to make a notes app and I already got the items (notes) to be listed in the recycler. There is an ImageButton on each of the items and I want a popUpMenu to appear once it is clicked.
It is possible to click the imageButton for individual items but I cant get the popUpMenu to appear for each individual item.
If there is a better or alternative way that would be great too.
Item XML (row_notes.xml):
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="0dp"
android:clickable="true"
android:layout_margin="5dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="1"
android:background="#drawable/note_shape"
android:padding="3dp">
<TextView
android:textColor="#color/primaryText"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Note"
android:id="#+id/rowNoteTitle"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:paddingLeft="10dp"
android:textSize="20dp"
android:layout_gravity="top|left|center_vertical"
android:layout_marginTop="2dp" />
<ImageButton
android:layout_width="20dp"
android:layout_height="match_parent"
android:id="#+id/rowNoteBtn"
android:layout_gravity="right|center_vertical"
android:background="#drawable/img_btn_shape"
android:src="#drawable/ic_dots_vertical_white_24dp" />
<TextView
android:textColor="#color/secondaryText"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Note amount"
android:id="#+id/rowNoteAmount"
android:layout_alignParentTop="true"
android:textSize="12dp"
android:layout_gravity="left|bottom"
android:layout_marginLeft="20dp"
android:layout_marginTop="7dp" />
</FrameLayout>
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="5dp"
android:id="#+id/progressBar"
android:max="100"
android:progressDrawable="#drawable/progress_color"
android:background="#ffffff" />
</LinearLayout>
Recycler Adapter (nRecyclerAdapter.java) POPUPMENU CODE IS HERE:
public class nRecyclerAdapter extends RecyclerView.Adapter<nViewHolder> {
private Context context;
private ArrayList<Note> notes;
public nRecyclerAdapter(Context context, ArrayList<Note> notes) {
this.context = context;
this.notes = notes;
}
#Override
public nViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_notes, parent, false);
nViewHolder holder = new nViewHolder(v);
return holder;
}
#Override
public void onBindViewHolder(final nViewHolder holder, final int position) {
holder.noteTitle.setText(notes.get(position).getNoteTitle());
holder.noteAmount.setText(notes.get(position).getNoteAmount());
holder.optionBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Toast.makeText(context, notes.get(position).getNoteTitle()+" click button works",Toast.LENGTH_SHORT).show();
PopupMenu popupMenu = new PopupMenu(context,holder.optionBtn);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
String option = menuItem.getTitle().toString();
Toast.makeText(context, notes.get(position).getNoteTitle(),Toast.LENGTH_SHORT).show();
if(option.matches("Edit")){
Toast.makeText(context, notes.get(position).getNoteTitle()+" Edit",Toast.LENGTH_SHORT).show();
}else if(option.matches("Delete")){
Toast.makeText(context, notes.get(position).getNoteTitle()+" Delete",Toast.LENGTH_SHORT).show();
}
return true;
}
});
popupMenu.show();
}
});
//listener
holder.setItemClickListener(new noteClickListener() {
#Override
public void onNoteItemClick(View v, int position) {
Toast.makeText(context, notes.get(position).getNoteTitle(),Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return notes.size();
}
public void updateData(ArrayList<Note> mNotes){
notes.clear();
notes.addAll(mNotes);
notifyDataSetChanged();
}
public void addItem(String title, String amount){
notes.add(new Note(title,amount));
notifyDataSetChanged();
}
public void removeItem(int position){
notes.remove(position);
notifyDataSetChanged();
}
}
ViewHolder (nViewHolder.java):
public class nViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView noteTitle;
TextView noteAmount;
noteClickListener noteClickListener;
ImageButton optionBtn;
public nViewHolder(View itemView) {
super(itemView);
optionBtn = (ImageButton) itemView.findViewById(R.id.rowNoteBtn);
noteTitle = (TextView) itemView.findViewById(R.id.rowNoteTitle);
noteAmount = (TextView) itemView.findViewById(R.id.rowNoteAmount);
optionBtn.setOnClickListener(this);
itemView.setOnClickListener(this);
}
public void setItemClickListener(noteClickListener nc){
this.noteClickListener = nc;
}
#Override
public void onClick(View view) {
this.noteClickListener.onNoteItemClick(view,getLayoutPosition());
}
}
onClickListener Class (noteClickListener.java):
import android.view.View;
public interface noteClickListener {
void onNoteItemClick(View v, int position);
}
You need to create some /res/menu/popupmenu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Menu Item" />
</menu>
Then, below your line PopupMenu popupMenu = new PopupMenu(context,holder.optionBtn); you need to inflate the menu with popupMenu.inflate(R.menu.popupmenu);
You must replace only the
holder.optionBtn with view
PopupMenu popupMenu = new PopupMenu(context,view);
And your code is start working.
Related
I tried below code to implement an expend and collapse content using recyclerview(listview) a link
final boolean isExpanded = position==mExpandedPosition;
holder.details.setVisibility(isExpanded?View.VISIBLE:View.GONE);
holder.itemView.setActivated(isExpanded);
if (isExpanded)
previousExpandedPosition = position;
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mExpandedPosition = isExpanded ? -1:position;
notifyItemChanged(previousExpandedPosition);
notifyItemChanged(position);
}
});
In my case when i click the particular position in recyclerview its expanding but it goes above the recyclerview.I cant see the full expanded content.i can see only partial content.In this case i need to scroll the recyclerview to view the full content.But i am searching for a solution to view the content without scroll the recyclerview. If i click another position in recyclerview that should be placed over an recyclerview.
Hear is My Code
public class CommonFragment extends Fragment {
#BindView(R.id.news_lists)
RecyclerView news_lists;
#BindView(R.id.nested_scroll)
NestedScrollView nested_scroll;
ArrayList<String> Names;
ArrayList<String> responseProducts = null;
NewsListAdaspters mAdapter;
Context mContext;
String position_name;
public CommonFragment() {
}
public static CommonFragment getInstance(String position, ArrayList<String> response) {
CommonFragment fragmentDummy = new CommonFragment();
Bundle args = new Bundle();
args.putStringArrayList("Types", response);
args.putString("position", position);
fragmentDummy.setArguments(args);
return fragmentDummy;
}
#Override
public void setArguments(Bundle args) {
super.setArguments(args);
this.responseProducts = args.getStringArrayList("Types");
this.position_name = args.getString("position");
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
View view;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}
try {
view = inflater.inflate(R.layout.fragment_common, container, false);
ButterKnife.bind(this, view);
} catch (InflateException ignored) {
}
mContext = getActivity();
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
news_lists.setLayoutManager(mLayoutManager);
news_lists.setNestedScrollingEnabled(false);
Names = new ArrayList<>();
Names.clear();
for (int i = 0; i < 15; i++) {
Names.add("News Details " + i);
}
Log.e("position_name==>", "" + position_name);
getList();
return view;
}
private void getList() {
if (responseProducts.size() > 0) {
mAdapter = new NewsListAdaspters(mContext, responseProducts, position_name);
news_lists.setAdapter(mAdapter);
}
}
public class NewsListAdaspters extends RecyclerView.Adapter<NewsListAdaspters.MyViewHolder> {
private ArrayList<String> data;
private Context context;
private String name;
int mExpandedPosition = -1;
int previousExpandedPosition = -1;
NewsListAdaspters(Context context, ArrayList<String> maps, String selectedFragmentName) {
this.context = context;
this.data = maps;
this.name = selectedFragmentName;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.common_view, parent, false);
final MyViewHolder holder = new MyViewHolder(itemView);
return holder;
}
#Override
public void onBindViewHolder(#NonNull final MyViewHolder holder, #SuppressLint("RecyclerView") final int position) {
holder.news_description.setText(data.get(position));
holder.news_title.setText(name);
final boolean isExpanded = position == mExpandedPosition;
Log.e("isExpanded=====>", "" + isExpanded);
holder.expend_layout.setVisibility(isExpanded ? View.VISIBLE : View.GONE);
holder.itemView.setActivated(isExpanded);
if (isExpanded)
previousExpandedPosition = position;
holder.news_image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, Activity_NewsFullDetails.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
holder.expand_click_layout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mExpandedPosition = isExpanded ? -1 : position;
notifyItemChanged(previousExpandedPosition);
notifyItemChanged(position);
}
});
holder.share_fb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "WORKING!!!!", Toast.LENGTH_SHORT).show();
}
});
holder.share_whatsapp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "WORKING!!!!", Toast.LENGTH_SHORT).show();
}
});
holder.share_twet.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "WORKING!!!!", Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return data.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
#BindView(R.id.news_title)
public TextView news_title;
#BindView(R.id.news_hour)
public TextView news_hour;
#BindView(R.id.news_description)
public TextView news_description;
#BindView(R.id.news_image)
public ImageView news_image;
#BindView(R.id.expand_click_layout)
RelativeLayout expand_click_layout;
#BindView(R.id.expend_layout)
public LinearLayout expend_layout;
#BindView(R.id.full_title)
public TextView full_title;
#BindView(R.id.txt_description)
public TextView txt_description;
#BindView(R.id.share_twet)
public ImageView share_twet;
#BindView(R.id.share_whatsapp)
public ImageView share_whatsapp;
#BindView(R.id.share_fb)
public ImageView share_fb;
MyViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
}
}
}
}
XML file
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#03000000">
<android.support.v4.widget.NestedScrollView
android:id="#+id/nested_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:fitsSystemWindows="false"
android:focusable="true"
android:focusableInTouchMode="true"
android:scrollbars="none">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants">
<android.support.v7.widget.RecyclerView
android:id="#+id/news_lists"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
ITEM XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:animateLayoutChanges="true"
android:background="#drawable/comment_background"
android:stateListAnimator="#animator/comment_selection"
android:elevation="3dp"
card_view:cardCornerRadius="2dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/news_image"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_margin="2dp"
android:background="#drawable/icon_card"
android:scaleType="fitXY" />
<RelativeLayout
android:id="#+id/expand_click_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/news_image"
android:layout_alignTop="#+id/news_image"
android:layout_toEndOf="#+id/news_image"
android:layout_toRightOf="#+id/news_image">
<TextView
android:id="#+id/news_description"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottom_layout"
android:layout_marginEnd="2dp"
android:layout_marginRight="2dp"
android:maxEms="3"
android:maxLines="4"
android:padding="4dp"
android:text=""
android:textColor="#color/black_color"
android:textSize="18sp" />
<RelativeLayout
android:id="#+id/bottom_layout"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_alignParentBottom="true"
android:layout_margin="4dp">
<TextView
android:id="#+id/news_hour"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/center_text"
android:layout_toStartOf="#+id/center_text"
android:maxLines="2"
android:text="Hours"
android:textColor="#color/black_color"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/center_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
<TextView
android:id="#+id/news_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toEndOf="#+id/center_text"
android:layout_toRightOf="#+id/center_text"
android:gravity="end"
android:text="News Title"
android:textColor="#color/black_color"
android:textSize="16sp" />
</RelativeLayout>
</RelativeLayout>
<LinearLayout
android:id="#+id/expend_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/news_image"
android:orientation="vertical"
android:visibility="gone">
<TextView
android:id="#+id/full_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Title Text"
android:textColor="#color/black_color"
android:textSize="20sp" />
<TextView
android:id="#+id/txt_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="#string/large_text1"
android:textColor="#color/black_color"
android:textSize="18sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:orientation="horizontal">
<ImageView
android:id="#+id/share_fb"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/share_facebook" />
<ImageView
android:id="#+id/share_whatsapp"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/share_whatsapp" />
<ImageView
android:id="#+id/share_twet"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/share_tweet" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Try to use NestedScrollView instead of ScrollView and set below to your activity :-
recyclerView.setNestedScrollingEnabled(false);
For more information you can refer below stackoverflow links:-
How to use RecyclerView inside NestedScrollView?
Recyclerview inside ScrollView not scrolling smoothly
I'm trying to have an edittext with a list view to choose contacts from a custom listview here is the code but everything appears except the listview:
InstantDialogBox:
public class InstantTextBoxDialog extends DialogFragment {
private EditText message;
private Button sendToAll;
private Button sendToLimited;
private ListView listContacts;
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
LayoutInflater inflater=getActivity().getLayoutInflater();
View view=inflater.inflate(R.layout.intant_message,null,false);
message=(EditText)view.findViewById(R.id.instant_message);
sendToAll=(Button) view.findViewById(R.id.btn_sent_to_all);
listContacts=(ListView)view.findViewById(R.id.list_checkable);
ChackableListAdapter adapter=new ChackableListAdapter(getActivity(),R.layout.list_chackable_item,new ArrayList<Contact>());
sendToAll.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!TextUtils.isEmpty(message.getText())){
ChatService.bind.getService().sendMessageToAll(message.getText().toString());
message.setText("");
dismiss();
}
}
});
sendToLimited=(Button) view.findViewById(R.id.btn_send_to_limited);
sendToLimited.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!TextUtils.isEmpty(message.getText())){
}
}
});
listContacts.setAdapter(adapter);
builder.setView(view);
return builder.create();
}
CheckableListAdapter:
public class ChackableListAdapter extends ArrayAdapter<Contact> {
private ArrayList<Contact> contacts;
private int resource;
private Context context;
private ArrayList<Contact> choosen;
public ChackableListAdapter(#NonNull Context context, int resource,ArrayList<Contact> arrayList) {
super(context, resource,arrayList);
this.context=context;
this.resource=resource;
this.contacts=arrayList;
contacts= DatabaseHandler.getDataBaseHandler(context).getAllRegisteredContacts();
choosen=new ArrayList<>();
}
#Nullable
#Override
public Contact getItem(int position) {
return contacts.get(position);
}
#NonNull
#Override
public View getView(final int position, #Nullable View view, #NonNull ViewGroup parent) {
Holder holder=null;
if (view == null || view.getTag() == null) {
holder=new Holder();
view= LayoutInflater.from(context).inflate(resource,null);
holder.phone=(TextView)view.findViewById(R.id.text_number);
holder.name=(CheckBox)view.findViewById(R.id.checkbox_number);
view.setTag(holder);
}
else {
holder = (Holder) view.getTag();
}
holder.phone.setText(getItem(position).phone_number);
holder.name.setText(getItem(position).name);
holder.name.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
choosen.add(getItem(position));
}else {
int i=0;
for (Contact contact:choosen){
if(contact.phone_number.equalsIgnoreCase(getItem(position).phone_number)){
choosen.remove(i);
notifyDataSetChanged();
}
i++;
}
}
}
});
Log.wtf("generating rows"," yes");
return view;
}
private class Holder{
public CheckBox name;
public TextView phone;
}
public ArrayList<Contact> getChoosenContacts(){
return choosen;
}
}
intant_message.xml:
<?xml version="1.0" encoding="utf-8"?>
<EditText
android:id="#+id/instant_message"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_weight="5"
android:gravity="top"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/btn_send_to_limited"
android:text="send to limited"
android:layout_width="95dp"
android:textSize="10sp"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
/>
<Button
android:id="#+id/btn_sent_to_all"
android:layout_width="95dp"
android:layout_height="wrap_content"
android:text="send to all"
android:textSize="10sp"
android:layout_below="#+id/btn_send_to_limited"
android:layout_marginTop="5dp"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools">
<ListView
android:id="#+id/list_checkable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listitem="#layout/list_chackable_item">
</ListView>
</LinearLayout>
list_chacakble_item:
<?xml version="1.0" encoding="utf-8"?>
<CheckBox
android:id="#+id/checkbox_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Abdallah"
android:textStyle="bold"/>
<TextView
android:id="#+id/text_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7656156165156651"
android:layout_marginLeft="30dp"/>
The dialog box is trigered from an adapter of a listview and that's the adapter's code:
holder.replay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
InstantTextBoxDialog exampleDialog=new InstantTextBoxDialog();
exampleDialog.show(((Activity) activity).getFragmentManager(),"sadsdadsaadassd");
}
});
But till now nothing shows except the buttons and no listview
RecyclerView Adapter Class.
public class TravelListAdapter extends RecyclerView.Adapter<TravelListAdapter.ViewHolder> {
Context mContext;
OnItemClickListener mItemClickListener;
String []names = {"Hotels", "Travel", "Medicine", "Education", "Travel", "Hotels"};
private int[] advertImageList = {R.drawable.hotel, R.drawable.travel, R.drawable.medical, R.drawable.education, R.drawable.travel, R.drawable.hotel};
// 2
public void setOnItemClickListener(OnItemClickListener mItemClickListener) {
this.mItemClickListener = mItemClickListener;
}
public TravelListAdapter(Context context) {
this.mContext = context;
}
// 3
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public LinearLayout placeHolder;
public LinearLayout placeNameHolder;
public TextView placeName;
public ImageView placeImage;
public ViewHolder(View itemView) {
super(itemView);
placeHolder = itemView.findViewById(R.id.mainHolder);
placeName = itemView.findViewById(R.id.placeName);
placeNameHolder = itemView.findViewById(R.id.placeNameHolder);
placeImage = itemView.findViewById(R.id.placeImage);
placeHolder.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (mItemClickListener != null) {
mItemClickListener.onItemClick(itemView, getPosition());
}
}
}
public interface OnItemClickListener {
void onItemClick(View view, int position);
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.category_row_item, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
// final Place place = new PlaceData().placeList().get(position);
holder.placeName.setText(names[position]);
holder.placeName.setTextColor(R.color.black);
Picasso.with(mContext).load(advertImageList[position]).into(holder.placeImage);
Bitmap photo = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.image2);
Palette.generateAsync(photo, new Palette.PaletteAsyncListener() {
public void onGenerated(Palette palette) {
//int bgColor = palette.getMutedColor(mContext.getResources().getColor(android.R.color.transparent));
holder.placeNameHolder.setBackgroundColor(mContext.getResources().getColor(android.R.color.transparent));
}
});
}
#Override
public int getItemCount() {
return names.length;
}
}`
My Xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/placeCard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:cardBackgroundColor="#android:color/transparent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<android.support.v7.widget.AppCompatImageView
android:id="#+id/placeImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="center"
android:transitionName="tImage"
android:layout_centerInParent="true"
android:tint="#android:color/white"
android:padding="10dp"/>
<!-- Used for the ripple effect on touch -->
<LinearLayout
android:id="#+id/mainHolder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?android:selectableItemBackground"
android:orientation="horizontal" />
<LinearLayout
android:id="#+id/placeNameHolder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="horizontal"
android:transitionName="tNameHolder"
android:padding="5dp"
android:layout_below="#+id/placeImage">
<TextView
android:id="#+id/placeName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_horizontal"
android:textAppearance="?android:attr/textAppearanceListItem"
android:textColor="#android:color/white"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
I am using vector images generated from Android Studio but those images are not displayed in recyclerView.
What should i do? Let me mention that the vector images that i use inside recyclerView are properly displayed if i use them in simple image views.
I'm using a recyclerview in order to show some photos of products, when I open the app it works amazing but when I scroll, the height of each card gets a lot bigger and then when I open the keyboard the height gets it's best size and then when I scroll, it gets this bad size again, I can't figure out how to fix that.
xml
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:scrollbars="vertical" />
Adapter
public class ProductsAdapter extends RecyclerView.Adapter<ProductsAdapter.ViewHolder>{
private Context mContext;
private List<Product> productList;
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.product_card, parent, false);
return new ProductsAdapter.ViewHolder(itemView);
}
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
Product album = productList.get(position);
holder.title.setText(album.getName());
holder.price.setText(album.getPrice() + " DZA");
// loading album cover using Glide library
Glide.with(mContext).load(album.getThumbnail()).into(holder.thumbnail);
holder.overflow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showPopupMenu(holder.overflow);
}
});
}
#Override
public int getItemCount() {
return productList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView title, price;
public ImageView thumbnail, overflow;
public ViewHolder (View itemView){
super (itemView);
title = (TextView) itemView.findViewById(R.id.title);
price = (TextView) itemView.findViewById(R.id.count);
thumbnail = (ImageView) itemView.findViewById(R.id.thumbnail);
overflow = (ImageView) itemView.findViewById(R.id.overflow);
}
}
public ProductsAdapter(Context mContext, List<Product> productList) {
this.mContext = mContext;
this.productList = productList;
}
private void showPopupMenu(View view) {
// inflate menu
PopupMenu popup = new PopupMenu(mContext, view);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.menu_product_card, popup.getMenu());
popup.setOnMenuItemClickListener(new MyMenuItemClickListener());
popup.show();
}
class MyMenuItemClickListener implements PopupMenu.OnMenuItemClickListener {
public MyMenuItemClickListener() {
}
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.action_add_favourite:
Toast.makeText(mContext, "Add to favourite", Toast.LENGTH_SHORT).show();
return true;
case R.id.action_play_next:
Toast.makeText(mContext, "Play next", Toast.LENGTH_SHORT).show();
return true;
default:
}
return false;
}
}
}
product_card.xml
`
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="#dimen/card_margin"
android:elevation="3dp"
card_view:cardMaxElevation="8dp"
card_view:cardCornerRadius="#dimen/card_album_radius">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/thumbnail"
android:layout_width="match_parent"
android:layout_height="#dimen/album_cover_height"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:scaleType="fitXY" />
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/thumbnail"
android:paddingLeft="#dimen/album_title_padding"
android:paddingRight="#dimen/album_title_padding"
android:paddingTop="#dimen/album_title_padding"
android:textColor="#color/album_title"
android:textSize="#dimen/album_title" />
<TextView
android:id="#+id/count"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/title"
android:paddingBottom="#dimen/songs_count_padding_bottom"
android:paddingLeft="#dimen/album_title_padding"
android:paddingRight="#dimen/album_title_padding"
android:textSize="#dimen/songs_count" />
<ImageView
android:id="#+id/overflow"
android:layout_width="#dimen/ic_album_overflow_width"
android:layout_height="#dimen/ic_album_overflow_height"
android:layout_alignParentRight="true"
android:layout_below="#id/thumbnail"
android:layout_marginTop="#dimen/ic_album_overflow_margin_top"
android:scaleType="centerCrop"
android:src="#drawable/ic_dots" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
I fixed the problem, it was all about wrap_content and match_parent I made a very stupid mistake sorry everyone.
I changed match_parent to wrap_content in cardview and this works perfectly now.
Thanks guys.
Hello everyone i am facing a problem with recycle view.
im using a recycle view with swipe to change item position functionality where user can change the position of items by long press items.
all items comes dynamical with different types of views.
everything is working good but when im trying to scroll the view then items displaying in recycle view are overlap each other.
i search on google and got the solution that is override the method .
#Override
public int getItemViewType(int position) {
return position;
}
when im using this method in my adapter class then my swipe to change view functionality stop working.
can anyone explain my why it's happening..?
Here is whole adapter class
public class CreateEmailAdapter extends RecyclerView.Adapter<CreateEmailAdapter.MyViewHolder> implements ItemTouchHelperAdapter {
private static final String TAG = "CreateEmailAdapter";
public int position = 0;
public ArrayList<CampaignData> List;
private final OnStartDragListener mDragStartListener;
public CreateEmailAdapter(ArrayList<CampaignData> List, OnStartDragListener mDragStartListener) {
this.List = List;
this.mDragStartListener = mDragStartListener;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title;
public ImageView imageView, imageView2, imageView3;
RelativeLayout MainListView;
public MyViewHolder(final View view) {
super(view);
parentLayout = (RelativeLayout) view.findViewById(R.id.parentLayout);
MainListView = (RelativeLayout) view.findViewById(R.id.MainListView);
title = (TextView) view.findViewById(R.id.MainTitle);
imageView2 = (ImageView) view.findViewById(R.id.imageViewPopup);
imageView3 = (ImageView) view.findViewById(R.id.movearrows);
imageView = (ImageView) view.findViewById(R.id.lytPatternColorDraw);
parentLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view1) {
position = getAdapterPosition();
HandleResponse(title.getText().toString(), view1, getAdapterPosition());
}
});
}
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_row, parent, false);
return new MyViewHolder(mView);
}
#Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
CampaignData campaignData = this.List.get(position);
holder.parentLayout.setTag(campaignData);
String extraData = campaignData.getExtraData();
String MediaType = campaignData.getMediaType();
holder.MainListView.setVisibility(View.VISIBLE);
holder.title.setText(campaignData.getTitle());
holder.imageView.setImageResource(campaignData.getimage());
holder.imageView2.setTag(campaignData);
holder.imageView3.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN) {
mDragStartListener.onStartDrag(holder);
}
return false;
}
});
}
#Override
public boolean onItemMove(int fromPosition, int toPosition) {
Collections.swap(List, fromPosition, toPosition);
notifyItemMoved(fromPosition, toPosition);
return true;
}
#Override
public void onItemDismiss(int position) {
List.remove(position);
notifyItemRemoved(position);
}
#Override
public int getItemCount() {
return List.size();
}
// #Override
// public int getItemViewType(int position) {
// return position;
// }
public int getPosition() {
return position;
}
}
XMl file list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parentLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff">
<RelativeLayout
android:id="#+id/MainListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/lytGrab"
android:visibility="visible">
<LinearLayout
android:id="#+id/lytPatternColor"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingTop="5dp">
<ImageView
android:id="#+id/movearrows"
android:layout_width="18dp"
android:layout_height="36dp"
android:layout_marginRight="10dp"
android:background="#drawable/movearrows" />
<ImageView
android:id="#+id/lytPatternColorDraw"
android:layout_width="30dp"
android:layout_height="40dp"
android:orientation="vertical" />
</LinearLayout>
<TextView
android:id="#+id/MainTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:layout_toRightOf="#+id/lytPatternColor"
android:gravity="center_vertical"
android:padding="10dp"
android:singleLine="true"
android:text="Title Here "
android:textColor="#000"
android:textSize="16sp" />
</RelativeLayout>
<!--Copy images -->
<LinearLayout
android:id="#+id/lytGrab"
android:layout_width="30dp"
android:layout_height="70dp"
android:layout_alignParentRight="true"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical">
<ImageView
android:id="#+id/imageViewPopup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:src="#drawable/three" />
</LinearLayout>
</RelativeLayout>