Button doesn't get focus in Recyclerview item - android

am trying to click button in Recyclerview item but it not receive onclick event.but it worked for double tap to respond onclick listener
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_margin="5dp"
android:descendantFocusability="blocksDescendants"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:layout_width="62dp"
android:layout_height="62dp"
android:layout_marginRight="5dp"
android:focusableInTouchMode="false"
android:scaleType="fitXY"
android:layout_marginLeft="5dp"
android:id="#+id/calllog_contact_img"
android:src="#mipmap/ic_launcher"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/calllog_contact_name"
android:layout_marginBottom="2dp"
android:text="Pounkumar Purushothaman"
android:textSize="18dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginRight="20dp"
android:focusableInTouchMode="false"
android:src="#android:drawable/sym_call_incoming"
android:layout_marginBottom="10dp"
android:id="#+id/calllog_call_type"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/calllog_no"
android:text="+919043974134"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10:20"
android:layout_marginRight="20dp"
android:id="#+id/calllog_call_duration"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10:20"
android:id="#+id/calllog_call_timing"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/llExpandArea"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="visible"
android:gravity="center"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:id="#+id/call_txt"
android:text="Call" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:paddingLeft="15dp"
android:id="#+id/block_txt"
android:text="Msg" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
am trying to click button in Recyclerview item but it not receive onclick event.but it worked for double tap to respond onclick listener
Adapter:
public class calllog_customadapter extends RecyclerView.Adapter<calllog_customadapter.RecordHolder> {
private List<calllog_item> calllog_items;
Context c;
RecyclerView list;
private int expandedPosition = -1;
#Override
public RecordHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.calllog_item, parent, false);
return new RecordHolder(itemView);
}
calllog_customadapter(Context context, RecyclerView list, final List<calllog_item> calllog_item){
this.c=context;
this.list=list;
this.calllog_items=calllog_item;
list.addOnItemTouchListener(new RecyclerItemClickListener(c, list ,new RecyclerItemClickListener.OnItemClickListener() {
#Override
public void onItemClick(View view, int position) {
Button call= (Button) view.findViewById(R.id.call_txt);
Button block= (Button) view.findViewById(R.id.block_txt);
call.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(c, "call", Toast.LENGTH_SHORT).show();
}
});
block.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(c, "blockbbb222", Toast.LENGTH_SHORT).show();
}
});
if (expandedPosition >= 0) {
int prev = expandedPosition;
notifyItemChanged(prev);
}
expandedPosition = position;
notifyItemChanged(expandedPosition);
}
#Override
public void onLongItemClick(View view, int position) {
// do whatever
}
}));
}
#Override
public void onBindViewHolder(RecordHolder holder, int position) {
calllog_item list=calllog_items.get(position);
holder.contact_name.setText(list.getContact_name());
holder.contact_img.setImageBitmap(list.getContact_img());
holder.call_type.setImageBitmap(list.getCall_type());
holder.call_duration.setText(list.getCall_duration());
holder.call_timing.setText(list.call_timing);
holder.contact_no.setText(list.getCall_no());
if (position == expandedPosition) {
holder.llExpandArea.setVisibility(View.VISIBLE);
} else {
holder.llExpandArea.setVisibility(View.GONE);
}
}
#Override
public int getItemCount() {
return calllog_items.size();
}
public class RecordHolder extends RecyclerView.ViewHolder {
TextView contact_name,call_timing,call_duration,contact_no;
ImageView contact_img,call_type;
LinearLayout llExpandArea;
public RecordHolder(View itemView) {
super(itemView);
contact_img= (ImageView) itemView.findViewById(R.id.calllog_contact_img);
call_type= (ImageView) itemView.findViewById(R.id.calllog_call_type);
call_duration= (TextView) itemView.findViewById(R.id.calllog_call_duration);
call_timing= (TextView) itemView.findViewById(R.id.calllog_call_timing);
contact_name= (TextView) itemView.findViewById(R.id.calllog_contact_name);
contact_no= (TextView) itemView.findViewById(R.id.calllog_no);
llExpandArea=(LinearLayout)itemView.findViewById(R.id.llExpandArea);
}
}
}

Did you try to set it as clicable and focusable?
XML
android:focusable="true"
android:clickable="true"
Java
button.setClickable("true");
button.setFocusable("true");
If nothing changes, try to put a log call in your onClick method (maybe there's something bad in the code), or in OnTouch.

Related

How to fetch data related to a clicked item in a recyclerView from room database to a new activity?

My app has a RawMaterialFragment that displays raw materials data in a recyclerView from room database.
I am trying to build a detail activity(MetrialItemView) to show up the details of an individual raw material by clicking or selecting the raw material from the recyclerView.
my problem is how to send the data from the adapter and how to receive the data in the MetrialItemView Activity and display it.
MaterialListAdapter:
public class MaterialListAdapter extends RecyclerView.Adapter<MaterialListAdapter.ViewHolder> {
private final LayoutInflater mInflater;
private FragmentRawMaterials mContext;
private List<RawMaterialsEntity> mMaterial; // Cached copy of Materials
RawMaterialsEntity mCurrent;
public MaterialListAdapter(FragmentRawMaterials context) {
mInflater = LayoutInflater.from(context.getActivity());
mContext = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = mInflater.inflate(R.layout.list_item, parent, false);
return new ViewHolder(itemView);
}
public class ViewHolder extends RecyclerView.ViewHolder {
private final TextView materialName;
private final TextView materialBrand;
private final TextView materialQuantity;
LinearLayout parentLayout;
private ViewHolder(View itemView) {
super(itemView);
materialName = itemView.findViewById(R.id.raw_material_name);
materialBrand = itemView.findViewById(R.id.raw_material_brand);
materialQuantity = itemView.findViewById(R.id.raw_material_quantity);
parentLayout = itemView.findViewById(R.id.parent_layout);
}
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
if (mMaterial != null) {
mCurrent = mMaterial.get(position);
holder.materialName.setText(mCurrent.getRawMaterialName());
holder.materialBrand.setText(mCurrent.getRawMaterialBrand());
holder.materialQuantity.setText(String.valueOf(mCurrent.getRawMaterialQuantity()));
} else {
// Covers the case of data not being ready yet.
holder.materialName.setText("Name NA");
holder.materialBrand.setText("Brand NA");
holder.materialQuantity.setText("Quantity NA");
}
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(mContext.getContext(), MaterialItemView.class);
mContext.startActivity(intent);
}
});
}
public void setMaterial(List<RawMaterialsEntity> materials){
mMaterial = materials;
notifyDataSetChanged();
}
// getItemCount() is called many times, and when it is first called,
// mWords has not been updated (means initially, it's null, and we can't return null).
#Override
public int getItemCount() {
if (mMaterial != null)
return mMaterial.size();
else return 0;
}
}
MaterialItemView:
public class MaterialItemView extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.material_item_view);
}
}
material_list_item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Raw Material Name:"
style="#style/OtherTextViews"/>
<TextView
android:id="#+id/material_name_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/OtherTextViewsBody"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Raw Material Brand:"
style="#style/OtherTextViews"/>
<TextView
android:id="#+id/material_brand_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/OtherTextViewsBody"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Unit Weight:"
style="#style/OtherTextViews"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2000"
style="#style/OtherTextViewsBody"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="gm"
style="#style/OtherTextViewsBody"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Unit Cost:"
style="#style/OtherTextViews"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="50"
style="#style/OtherTextViewsBody"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cost per gm/ml:"
style="#style/OtherTextViews"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0.1"
style="#style/OtherTextViewsBody"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Available Quantity:"
style="#style/OtherTextViews"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1000"
style="#style/OtherTextViewsBody"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total Cost:"
style="#style/OtherTextViews"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="50000"
style="#style/OtherTextViewsBody"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Supplier Name:"
style="#style/OtherTextViews"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pandah"
style="#style/OtherTextViewsBody"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Supplier Email:"
style="#style/OtherTextViews"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pandah#panadh.com"
style="#style/OtherTextViewsBody"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Supplier Phone:"
style="#style/OtherTextViews"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+966555699517"
style="#style/OtherTextViewsBody"/>
</LinearLayout>
</LinearLayout>
FragmentRawMaterials:
public class FragmentRawMaterials extends Fragment{
private RawMaterialViewModel mMaterialViewModel;
private static final int NEW_MATERIAL_ACTIVITY_REQUEST_CODE = 1;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_raw_materials, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// Setup any handles to view objects here
//FloatingActionButton fab to insert recipes
TextView emptyViewText = view.findViewById(R.id.empty_raw__materials_view);
FloatingActionButton fab = view.findViewById(R.id.fab_raw_materials);
fab.setOnClickListener(view1 -> {
Intent intent = new Intent(getActivity(), RawMaterialsEditor.class);
startActivityForResult(intent, NEW_MATERIAL_ACTIVITY_REQUEST_CODE);
});
RecyclerView recyclerView = view.findViewById(R.id.recyclerview);
//Decoration to add a line divider between recyclerView items
DividerItemDecoration decoration =
new DividerItemDecoration(Objects.requireNonNull(this.getActivity()),
R.drawable.border_line);
recyclerView.addItemDecoration(decoration);
recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
final MaterialListAdapter adapter = new MaterialListAdapter(this);
recyclerView.setAdapter(adapter);
// Check if adapter list is empty, if so empty text view will appear.
adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
#Override
public void onChanged() {
super.onChanged();
if (adapter.getItemCount() == 0) {
recyclerView.setVisibility(View.GONE);
emptyViewText.setVisibility(View.VISIBLE);
}
else {
recyclerView.setVisibility(View.VISIBLE);
emptyViewText.setVisibility(View.GONE);
}
}
});
mMaterialViewModel = new ViewModelProvider(this).get(RawMaterialViewModel.class);
// Update the cached copy of the words in the adapter.
mMaterialViewModel.getAllMaterials().observe(this, adapter::setMaterial);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == NEW_MATERIAL_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
RawMaterialsEntity material = new RawMaterialsEntity(data
.getStringExtra(RawMaterialsEditor.EXTRA_REPLY_NAME),
data.getStringExtra(RawMaterialsEditor.EXTRA_REPLY_BRAND),
Float.valueOf(data.getStringExtra(RawMaterialsEditor.EXTRA_REPLY_WEIGHT)),
Float.valueOf(data.getStringExtra(RawMaterialsEditor.EXTRA_REPLY_COST)),
Integer.valueOf(data.getStringExtra(RawMaterialsEditor.EXTRA_REPLY_QUANTITY)),
data.getStringExtra(RawMaterialsEditor.EXTRA_REPLY_S_NAME),
data.getStringExtra(RawMaterialsEditor.EXTRA_REPLY_S_EMAIL),
data.getStringExtra(RawMaterialsEditor.EXTRA_REPLY_S_PHONE),
data.getStringExtra(RawMaterialsEditor.EXTRA_REPLY_UOM));
mMaterialViewModel.insertMaterial(material);
mMaterialViewModel.costPerGm();
mMaterialViewModel.totalCost();
} else {
Toast.makeText(
Objects.requireNonNull(getActivity()).getApplicationContext(),
R.string.editor_insert_rm_failed,
Toast.LENGTH_LONG).show();
}
}
}
each row of dataModel() must have ID and then use putExtra() whan clicked It happens
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(mContext.getContext(), MaterialItemView.class);
intent.putExtra("ID",mCurrent.getID());
mContext.startActivity(intent);
}
});
and use getIntent() in detailActivity
int id =getIntent().getIntExtra("ID",-1);
and then get a row data in detail activity from database(viewModel)by ID and parse it

grid view items not scrolling down to up (repeating the items) and when i am loading data from server in android?

I have a grid view and data updated in grid view using api calls and grid item loads a image from url using picasso library all are perfect but when i am scrolling down to up the grid items not scrolled and repeating the items(up to down scrolling was perfect).
Adapterclass.java
private class ProductAdapter extends BaseAdapter {
ViewHolder holder = null;
#Override
public int getCount() {
return mProducts != null ? mProducts.size() : 0;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
holder = new ViewHolder();
if (convertView == null) {
// convertView = LayoutInflater.from(Products_activity.this).inflate(R.layout.product_layout, null, false);
LayoutInflater vi = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.product_layout, null);
holder.product_name = (TextView) convertView.findViewById(R.id.product_name);
holder.product_descr = (TextView) convertView.findViewById(R.id.product_desc);
holder.product_price = (TextView) convertView.findViewById(R.id.product_price);
holder.product_pic = (ImageView) convertView.findViewById(R.id.iv_division_img);
holder.product_share = (ImageView) convertView.findViewById(R.id.productshare_button);
holder.like_image=(ImageView) convertView.findViewById(R.id.like_image);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
if (mProducts != null) {
String url = "http://staggershop.pivalue.net/assets/product_images/"+mProducts.get(position).product_image;
String urls = url.replaceAll(" ", "%20");
Picasso.with(getApplicationContext()).load(urls).resize(300,300).noFade().error(R.drawable.home_brands).into(holder.product_pic);
holder.product_name.setText(mProducts.get(position).product_name);
//product_descr.setText(mProducts.get(position).pr);
// product_price.setText(mProducts.get(position).product_price);
/* Picasso
.with(getApplicationContext())
.setLoggingEnabled(true);
Picasso.with(getApplicationContext())
.load(urls)
.error(R.drawable.home_brands).memoryPolicy(MemoryPolicy.NO_STORE)
.networkPolicy(NetworkPolicy.NO_STORE)
.into(product_pic);*/
holder.product_descr.setText(mProducts.get(position).product_description);
if(!recently_beans.isEmpty()){
for(int i=0;i<recently_beans.size();i++) {
if (mProducts.get(position).prdid.equals(recently_beans.get(i).prod_id))
{
holder.like_image.setImageResource(R.drawable.like);
}
}
notifyDataSetChanged();
}
}
holder.product_share.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
BottomSheetFragment bottomSheetFragment = new BottomSheetFragment();
bottomSheetFragment.show(getSupportFragmentManager(), bottomSheetFragment.getTag());
}
});
holder.like_image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
public void run() {
if (!recently_beans.isEmpty()) {
for (int i = 0; i < recently_beans.size(); i++) {
if (mProducts.get(position).prdid.equals(recently_beans.get(i).prod_id)) {
holder.like_image.setImageResource(R.drawable.dislike);
Inserfavorite(mProducts.get(position).prdid);
}
}
}
}
});
}
});
// notifyDataSetChanged();
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Inserrecent(mProducts.get(position).prdid);
Intent intent = new Intent(Products_activity.this, Product_description.class);
startActivity(intent);
}
});
return convertView;
}
}
public static class ViewHolder {
public TextView product_name;
public TextView product_descr;
public TextView product_price;
public ImageView product_pic ;
public ImageView product_share;
public ImageView like_image;
}
This my product.xml
<RelativeLayout
android:id="#+id/rl_divisioin_image"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_centerHorizontal="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="#+id/product_img">
<ImageView
android:id="#+id/iv_division_img"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/view"
android:adjustViewBounds="true"
android:layout_centerHorizontal="true"
android:scaleType="fitXY"
android:src="#drawable/home_brands" />
</LinearLayout>
<LinearLayout
android:id="#+id/prod_name_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="#+id/product_img"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/product_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:elevation="4dp"
android:fontFamily="#string/font_family_regular"
android:gravity="center"
android:padding="5dp"
android:maxLines="1"
android:text="product name"
android:textColor="#color/black"
android:textSize="10sp" />
<TextView
android:id="#+id/product_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:elevation="4dp"
android:fontFamily="#string/font_family_regular"
android:gravity="center"
android:maxLines="1"
android:padding="5dp"
android:text="product Description"
android:textColor="#color/black"
android:textSize="10sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/product_desc"
android:layout_gravity="center"
android:layout_marginTop="20dp">
<TextView
android:id="#+id/product_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:elevation="4dp"
android:text="100$"
android:textColor="#color/black"
android:textSize="12sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/productshare_button"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentEnd="true"
android:elevation="4dp"
android:layout_marginRight="5dp"
android:src="#mipmap/symbol_dotss"
android:text="product name"
android:textColor="#color/black"
android:textSize="10sp" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_margin="5dp"
android:id="#+id/like_image"
android:layout_alignParentEnd="true"
android:src="#drawable/dislike" />
</RelativeLayout>
activty_product.xml
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/coordinator"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/rltool"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/toolbar1"
android:background="#color/textColorPrimary"
app:titleTextColor="#color/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/AppTheme.Toolbar"
app:layout_scrollFlags="scroll|enterAlways">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/search1"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="#drawable/background_shape_buttons">
<!-- <EditText
android:id="#+id/searchEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#D6D5C0"
android:hint="Search..................."
android:layout_gravity="center"
android:backgroundTint="#00000000" />-->
<SearchView
android:id="#+id/search"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:textColorHint="#D6D5C0"
android:text="#string/search"
android:layout_gravity="center"
android:iconifiedByDefault="false">
<requestFocus />
</SearchView>
</LinearLayout>
</android.support.v7.widget.Toolbar>
<View
android:id="#+id/shadow_view"
android:layout_width="match_parent"
android:layout_alignBottom="#+id/toolbar1"
android:visibility="visible"
android:layout_height="3dp"
android:background="#drawable/toolbar_shadow" />
</RelativeLayout>
<GridView
android:id="#+id/product_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:scrollbars="vertical"
android:layout_margin="15dp"
android:horizontalSpacing="10dp"
android:numColumns="2"
android:verticalSpacing="10dp">
</GridView>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
To avoid the unexpected behaviour , provide the implementations of
#Override
public Object getItem(int position) {
return mProducts.get(position);
}
#Override
public long getItemId(int position) {
//return position; or better to provide some unique id
// as there is one in your code, product id
return mProducts.get(position).prod_id;
}
Note : remove notifyDataSetChanged(); in getView because it will trigger the adapter to refresh all view while they are under creating which can cause this behaviour.
Recommended Read
What is the intent of the methods getItem and getItemId in the Android class BaseAdapter?

using two layout for single item in recyclerview

i am trying to switch setLayoutManager between LinearLayoutManager(List) to GridLayoutManager(GridView) on toggle button it is working fine but when the layout changes for list to grid its giving me an exception in list view i am showing extra data than girdView and setting texts cant not find this Textviews in gridView single item file so its giving me an error can you please guide how can i handle two layouts and views in single Adapter
this is my Adapter Code
public class SavedAlbumAdapter2 extends RecyclerView.Adapter<SavedAlbumAdapter2.ItemViewHolder> {
private final GlideRequest<Drawable> request;
private ArrayList<AlbumLocalObj> itemModels;
private static final int LIST_ITEM = 0;
private static final int GRID_ITEM = 1;
private ClickListener mListener;
private LocalPreferences localPreferences;
public SavedAlbumAdapter2(Activity context, ArrayList<AlbumLocalObj> values, ClickListener clickListener) {
itemModels = values;
this.mListener = clickListener;
localPreferences = new LocalPreferences(context);
int reqWidth = ((HomeActivity) context).screenWidth / 2;
request = GlideApp.with(context)
.asDrawable()
.centerCrop()
.override(reqWidth, reqWidth)
.transition(withNoTransition());
}
#Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView;
if (viewType == LIST_ITEM) {
itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_gallery2, parent, false);
} else {
itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_gallery, parent, false);
}
return new ItemViewHolder(itemView, mListener);
}
#Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
AlbumLocalObj model = itemModels.get(position);
holder.img_label.setText(model.getName());
holder.tv_count.setText(String.valueOf(model.getCount()));
initializeViews(model, holder, position);
}
#Override
public int getItemViewType(int position) {
if (localPreferences.isGridViewEnabled()) {
return GRID_ITEM;
} else {
return LIST_ITEM;
}
}
private void initializeViews(AlbumLocalObj model, final ItemViewHolder holder, int position) {
if (model.getMediaFileObject() == null)
holder.gallery_img.setImageResource(R.drawable.ic_empty_fol);
else {
request.load(model.getMediaFileObject()).listener(new RequestListener<Drawable>() {
#Override
public boolean onLoadFailed(#Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
Log.i("glide_action", "failure");
clearResource((MediaFileObject) model);
return false;
}
#Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
clearResource((MediaFileObject) model);
Log.i("glide_action", "success");
return false;
}
})
.into(holder.gallery_img);
}
}
public interface ClickListener {
void onItemClick(View view, int piecesAtPos);
void onItemLongClick(View view, int piecesAtPos);
}
private void clearResource(MediaFileObject item) {
try {
if (item.getInputStream() != null) {
Log.i("resource", "clear");
item.getInputStream().close();
item.setInputStream(null);
//Log.i("closing_str", pos + "");
}
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public int getItemCount() {
return itemModels.size();
}
public static class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
SquarImageView gallery_img;
TextView img_label;
TextView tv_count;
ImageButton info;
private ClickListener mListener;
ItemViewHolder(View itemView, ClickListener listener) {
super(itemView);
gallery_img = itemView.findViewById(R.id.img);
info = itemView.findViewById(R.id.info);
img_label = itemView.findViewById(R.id.label);
tv_count = itemView.findViewById(R.id.count);
this.mListener = listener;
info.setOnClickListener(this);
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
}
#Override
public void onClick(View view) {
mListener.onItemClick(view, getAdapterPosition());
}
#Override
public boolean onLongClick(View view) {
mListener.onItemLongClick(view, getAdapterPosition());
return false;
}
}
}
this is my layout for single item of grid View
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/content_add_messages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="1dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<SquarImageView
android:id="#+id/img"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:padding="0dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/img"
android:alpha="0.8"
android:background="#color/darkColor"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:padding="7dp">
<TextView
android:id="#+id/label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:ellipsize="end"
android:maxLines="1"
android:singleLine="true"
android:text="akhskajhskjahkasjkhajskhjksasahjksahjkshajksahkjsahkjshjksjhakjhskjah"
android:textColor="#android:color/white"
android:textSize="16sp" />
<TextView
android:id="#+id/count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:text="lahslahashkjjsahshjaksjhaksajhksahkjsahjksahkjsahjksahkjsahkjsahjksajhksahkjsakhjsahkjsahkjsahsklhask"
android:textColor="#android:color/white"
android:textSize="12sp" />
</LinearLayout>
<ImageButton
android:id="#+id/info"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="?android:attr/selectableItemBackground"
android:padding="7dp"
app:srcCompat="#drawable/info_white" />
</LinearLayout>
</RelativeLayout>
and this is for list item
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal">
<RelativeLayout
android:id="#+id/parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<RelativeLayout
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginLeft="3dp"
android:layout_marginStart="3dp"
android:layout_marginTop="3dp"
android:background="#drawable/album_item_background" />
<RelativeLayout
android:layout_width="70dp"
android:layout_height="70dp"
android:background="#drawable/album_item_background">
<SquarImageView
android:id="#+id/img"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="3dp"
android:scaleType="fitXY" />
</RelativeLayout>
</RelativeLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="kajhskahskhas" />
<TextView
android:id="#+id/count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="kajhskahskhas" />
<TextView
android:id="#+id/3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
</LinearLayout>
<LinearLayout
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<ImageButton
android:id="#+id/info"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srcCompat="#drawable/info"
android:background="?attr/selectableItemBackgroundBorderless"
/>
</LinearLayout>
in my list view there are extra textview and button when i change list view to grid it can not find that views what should i do? do i have to create separate adapter?or how can i handle this in single adapter please help me
Why do the extra TextViews in List item layout is having the same id?
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="kajhskahskhas" />
<TextView
android:id="#+id/count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="kajhskahskhas" />
<TextView
android:id="#+id/count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />

When I press the button inside cardview, both cardview item and button is clicked

I have a Cardview with Button
card view layout
<?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:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="0dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
card_view:cardCornerRadius="3dp"
card_view:cardElevation="0.01dp"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="123dp"
android:background="#drawable/posts_card_bg"
android:orientation="horizontal"
android:paddingBottom="3dp"
android:paddingRight="3dp"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="0.8"
>
<TextView
android:id="#+id/post_title"
android:textColor="#android:color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:alpha="1"
android:gravity="center_vertical"
android:maxLines="2"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:text="title"
android:textSize="30sp"/>
<TextView
android:id="#+id/post_body"
android:layout_width="match_parent"
android:textColor="#android:color/white"
android:layout_height="wrap_content"
android:maxLines="2"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:text="Body"
android:textSize="20sp"/>
</LinearLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="120dp"
>
<Button
android:id="#+id/expand"
android:layout_width="40dp"
android:layout_height="37dp"
android:background="#drawable/expand"
/>
<TextView
android:id="#+id/post_id"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/circle"
android:gravity="center"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:text="99"
android:textColor="#android:color/white"
android:textSize="16sp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
PostCardAdapter.java
#Override
public void onBindViewHolder(ContactViewHolder holder, int position) {
......
holder.btn_expand.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//for showing details of item on dialog
listener.onItemClicked(view);
}
});
}
Recyclerview item click
listener = this;
mRecyclerView = (RecyclerView) findViewById(R.id.cardlist_posts);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setAdapter(new PostCardAdapter(listener,db.getAllPosts(), R.layout.cardview_post));
mRecyclerView.addOnItemTouchListener(
new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() {
#Override
public void onItemClick(View view, int id) {
// TODO Handle item click
Intent intent = new Intent(MainActivity.this, ShowPostDetails.class);
intent.putExtra("KEY_POST_ID", String.valueOf(id + 1));
System.out.println(String.valueOf(id + 1));
//Starting another activity to show post details
startActivity(intent);
}
})
);
MainActivity.java
#Override
public void onItemClicked(View v) {
DialogPostFragment df= new DialogPostFragment();
df.show(getFragmentManager(), "Dialog");
}
When the button is pressed then the cardview item is clicked and ongoing back dialog is also opened.
I want to open dialog only when I press the button.
Thank You.
I had faced exactly the same issue once.This is how I solved it :
add id to the first linear layout inside the cardview :
<LinearLayout
android:id="#+id/rowLL"
android:layout_width="fill_parent"
android:layout_height="123dp"
android:background="#drawable/posts_card_bg"
android:orientation="horizontal"
android:paddingBottom="3dp"
android:paddingRight="3dp"
>
Now,in your PostCardAdapter.java :
public class PostCardAdapter extends RecyclerView
.Adapter<ContactViewHolder> {
...
private static MyClickListener myClickListener;
...
public void setOnItemClickListener(MyClickListener myClickListener) {
PostCardAdapter.myClickListener = myClickListener;
}
...
public interface MyClickListener {
public void onItemClick(int position, View v);
}
}
Then,in your ContactViewHolder.java
public class ContactViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private PostCardAdapter.MyClickListener myClickListener;
...
public ContactViewHolder(final Context context, View itemView, PostCardAdapter.MyClickListener clickListener) {
super(itemView);
...
myClickListener = clickListener;
itemView.findViewById(R.id.rowLL).setOnClickListener(this);
}
#Override
public void onClick(View v) {
myClickListener.onItemClick(getLayoutPosition(), v);
}
}

No toast shown when item clicked RecyclerView

Ok, I know, probably someone has already asked this question. I have already found a possible solution for my problem on this link. But, for an unknown reason, that solution doesn't work for me.
I want to display a Toast when an item (a CardView) is clicked inside a RecyclerView. This Toast must show me the position of the item clicked.
When I click on a CardView I see the ripple - so that means that the card is clicked - but unfortunately, I don't see any toast. Can someone explain me why? Thanks in advance.
This is my Adapter:
public class MainRecyclerViewAdapter extends RecyclerView.Adapter<MainRecyclerViewAdapter.MyMainViewHolder> {
private List<Information> mainInfo;
MyDatabase myDatabase;
public MainRecyclerViewAdapter(List<Information> mainInfo) {
this.mainInfo = mainInfo;
}
public static class MyMainViewHolder extends RecyclerView.ViewHolder {
TextView titleTextView, notesTextView, dateTextView, timeTextView;
ImageView imageView;
LinearLayout linearLayout;
public MyMainViewHolder(View itemView) {
super(itemView);
linearLayout = (LinearLayout) itemView.findViewById(R.id.mainLayout);
imageView = (ImageView) itemView.findViewById(R.id.imgIcon);
titleTextView = (TextView) itemView.findViewById(R.id.title_TextView);
notesTextView = (TextView) itemView.findViewById(R.id.notes_TextView);
dateTextView = (TextView) itemView.findViewById(R.id.date_TextView);
timeTextView = (TextView) itemView.findViewById(R.id.time_TextView);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Position: " + Integer.toString(getAdapterPosition()), Toast.LENGTH_LONG).show();
}
});
}
}
#Override
public MyMainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_single_raw, parent, false);
MyMainViewHolder holder = new MyMainViewHolder(v);
return holder;
}
public void delete(int position){
mainInfo.remove(position);
notifyItemRemoved(position);
}
#Override
public void onBindViewHolder(final MyMainViewHolder holder, final int position) {
holder.imageView.setImageResource(mainInfo.get(position).getICON_ID());
holder.titleTextView.setText(mainInfo.get(position).getTITLE());
holder.notesTextView.setText(mainInfo.get(position).getNOTES());
holder.dateTextView.setText(mainInfo.get(position).getDATE());
holder.timeTextView.setText(mainInfo.get(position).getTIME());
}
#Override
public int getItemCount() {
return mainInfo.size();
}}
This is the layout file of my cardView:
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/card_view1"
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
card_view:cardCornerRadius="3dp"
android:layout_margin="5dp">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0.5dp"
android:clickable="true"
android:background="?android:selectableItemBackground"
android:orientation="horizontal"
android:weightSum="10">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="16dp"
android:layout_weight="8.5">
<TextView
android:id="#+id/title_TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="8dp"
android:layout_marginTop="5dp"
android:text="Title"
android:fontFamily="sans-serif-light"
android:textColor="#F57C00"
android:textSize="25sp"
/>
</RelativeLayout>
<View
android:layout_width="fill_parent"
android:id="#+id/divider1"
android:layout_height="0.1dp"
android:layout_below="#+id/title_TextView"
android:background="#android:color/darker_gray"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="16dp"
android:layout_weight="8.5">
<ImageView
android:id="#+id/imgIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#mipmap/ic_alarm_check_grey600_48dp" />
</RelativeLayout>
<LinearLayout
android:id="#+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:orientation="vertical">
<TextView
android:id="#+id/notes_TextView"
android:paddingTop="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="8dp"
android:text="notes"
android:textColor="#ff565656"
android:textSize="12sp" />
<TextView
android:id="#+id/date_TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="8dp"
android:text="date"
android:textColor="#ff565656"
android:textSize="12sp" />
<TextView
android:id="#+id/time_TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="8dp"
android:text="time"
android:textColor="#ff565656"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Problem solved! This is the working code:
public static class MyMainViewHolder extends RecyclerView.ViewHolder {
TextView titleTextView, notesTextView, dateTextView, timeTextView;
ImageView imageView;
LinearLayout linearLayout;
public MyMainViewHolder(final View itemView) {
super(itemView);
linearLayout = (LinearLayout) itemView.findViewById(R.id.mainLayout);
imageView = (ImageView) itemView.findViewById(R.id.imgIcon);
titleTextView = (TextView) itemView.findViewById(R.id.title_TextView);
notesTextView = (TextView) itemView.findViewById(R.id.notes_TextView);
dateTextView = (TextView) itemView.findViewById(R.id.date_TextView);
timeTextView = (TextView) itemView.findViewById(R.id.time_TextView);
itemView.setClickable(true);
itemView.setFocusableInTouchMode(true);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(itemView.getContext(), "Position: " + Integer.toString(getAdapterPosition()), Toast.LENGTH_LONG).show();
}
});
linearLayout.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
Toast.makeText(itemView.getContext(), "Position: " + Integer.toString(getAdapterPosition()), Toast.LENGTH_LONG).show();
}
});
}
}
So now, if i click on the linearLayout - so on the entire cardView - I see its position!
Credits: link
Add the following line in your view.xml
android:clickable=true
Remove the following line from first LinearLayout
android:clickable="true"
try this,
#Override
public MyMainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_single_raw, parent, false);
MyMainViewHolder holder = new MyMainViewHolder(v);
return holder;
}
public void delete(int position){
mainInfo.remove(position);
notifyItemRemoved(position);
}
#Override
public void onBindViewHolder(final MyMainViewHolder holder, final int position) {
holder.imageView.setImageResource(mainInfo.get(position).getICON_ID());
holder.titleTextView.setText(mainInfo.get(position).getTITLE());
holder.notesTextView.setText(mainInfo.get(position).getNOTES());
holder.dateTextView.setText(mainInfo.get(position).getDATE());
holder.timeTextView.setText(mainInfo.get(position).getTIME());
holder.titleTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context,""+position,Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return mainInfo.size();
}}
Pay attention in cardview scope clickable is true
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_weight="1"
android:gravity="center_vertical|center|center_horizontal"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="#+id/card_view"
android:layout_width="match_parent"
card_view:contentPadding="10dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:clickable="true"
android:transitionGroup="false"
android:layout_margin="10dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/select_photo"
android:layout_alignParentTop="true"
android:layout_gravity="center_horizontal"
android:layout_margin="5dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/select_name"
android:textSize="25sp"
android:textStyle="bold"
android:layout_gravity="bottom|center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/txt_zoom_in"
/>`enter code here`
</android.support.v7.widget.CardView>
</LinearLayout>

Categories

Resources