How to get Layout width modified in RecyclerView - java

I'm struggling in using a RecyclerView with a layout that has some listeners.
The code is this
ViewHolder
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView tickerSymbol;
public TextView companyName;
public View highlightBuy;
public LinearLayout categoryImage;
public LayoutParams categoryLayoutParams;
public LinearLayout arrowImage;
public ImageView categoryIconSeparator;
public ImageView arrowSeparator;
public ImageView arrowIcon;
public RelativeLayout stockRow;
public LinearLayout tickerInfo;
public Boolean isCategoryOpened = false;
public ViewHolder(View itemView) {
super(itemView);
this.tickerSymbol = (TextView) itemView.findViewById(R.id.ticker_symbol_textview);
this.companyName = (TextView) itemView.findViewById(R.id.company_name_textview);
this.highlightBuy = itemView.findViewById(R.id.highlight_buy_view);
this.categoryImage = (LinearLayout) itemView.findViewById(R.id.category_image);
this.categoryLayoutParams = (LayoutParams) categoryImage.getLayoutParams();
this.arrowImage = (LinearLayout) itemView.findViewById(R.id.arrow);
this.categoryIconSeparator = (ImageView) itemView.findViewById(R.id.category_icon_separator);
this.arrowSeparator = (ImageView) itemView.findViewById(R.id.arrow_separator);
this.arrowIcon = (ImageView) itemView.findViewById(R.id.arrow_icon_imageview);
this.stockRow = (RelativeLayout) itemView.findViewById(R.id.stock_row);
this.tickerInfo = (LinearLayout) itemView.findViewById(R.id.ticker_info);
}
}
onCreateViewHolder
#Override
public StockListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View stockView = inflater.inflate(R.layout.stock_list_row, parent, false);
return new StockListAdapter.ViewHolder(stockView);
}
onBindViewHolder
#Override
public void onBindViewHolder(final StockListAdapter.ViewHolder holder, final int position) {
final Stock stock = mStocks.get(position);
final int collapsedCategoryWidth = (int) mContext.getResources().getDimension(R.dimen.collapsed_category_icon_width);
final int stockRowHeight = (int) mContext.getResources().getDimension(R.dimen.stock_row_height);
//Reset holder status
if (holder.isCategoryOpened) {
holder.isCategoryOpened = false;
holder.categoryImage.getLayoutParams().width = collapsedCategoryWidth;
holder.categoryImage.setLayoutParams(holder.categoryImage.getLayoutParams());
holder.arrowIcon.setRotationY(0);
}
holder.tickerSymbol.setText(stock.ticker);
holder.companyName.setText(stock.name);
holder.companyName.setSelected(true);
holder.companyName.requestFocus();
holder.categoryImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final int newWidth;
final int startWidth;
final int startArrowAngle;
final int newArrowAngle;
if (!holder.isCategoryOpened) {
holder.isCategoryOpened = true;
newWidth = holder.stockRow.getWidth() - holder.categoryLayoutParams.leftMargin - holder.arrowImage.getWidth() - ((LayoutParams) holder.arrowImage.getLayoutParams()).rightMargin - holder.categoryIconSeparator.getWidth();
startWidth = collapsedCategoryWidth;
startArrowAngle = 0;
newArrowAngle = 180;
} else {
holder.isCategoryOpened = false;
newWidth = collapsedCategoryWidth;
startWidth = holder.categoryImage.getWidth();
startArrowAngle = 180;
newArrowAngle = 0;
}
Animation a = new Animation() {
#Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
holder.categoryLayoutParams.width = startWidth + (int) ((newWidth - startWidth) * interpolatedTime);
//MAYBE PROBLEM HERE??
holder.categoryImage.setLayoutParams(holder.categoryLayoutParams);
holder.arrowIcon.setRotationY(startArrowAngle + (newArrowAngle - startArrowAngle) * interpolatedTime);
}
};
a.setDuration(500);
holder.categoryImage.startAnimation(a);
}
});
I'll attach a sample of the bug I have.
When I first click a view, the onClick work successfully, but if I scroll and then go back to that view, only the arrow gets flipped (which means the onClick gets called) but the LinearLayout Width won't get modified.
But now what's really strange is that if I click on another view that hasn't been clicked before, or (I guess) hasn't been recycled, happens the following:
So I guess, the Width is set, but isn't show.
I don't really know how to fix this.
An help or other ideas are appreciated!
Here is what I've already tried:
Putting the status in the stock;
Putting the listener outside the adapter;
Putting the listener inside the holder;

add 1 variable in your store class to stored value if you already clicked on that item or not..
the ilustration like this , i set variable isSelected in store :
private boolean mSelected=false;
public boolean getIsSelected(){
return this.mSelected;
}
public void setIsSelected(boolean mSelected){
this.mSelected=mSelected;
}
and then in your onClick method just add stock.setIsSelected(!stock.getIsSelected()) after if conditional and set your if conditional to read boolean from stock.getIsSelected() and the last, just remove variable isCategoryOpened from your holder.
public void onClick(View v) {
final int newWidth;
final int startWidth;
final int startArrowAngle;
final int newArrowAngle;
if (stock.getIsSelected()) {
newWidth = holder.stockRow.getWidth() - holder.categoryLayoutParams.leftMargin - holder.arrowImage.getWidth() - ((LayoutParams) holder.arrowImage.getLayoutParams()).rightMargin - holder.categoryIconSeparator.getWidth();
startWidth = collapsedCategoryWidth;
startArrowAngle = 0;
newArrowAngle = 180;
} else {
newWidth = collapsedCategoryWidth;
startWidth = holder.categoryImage.getWidth();
startArrowAngle = 180;
newArrowAngle = 0;
}
stock.setIsSelected(!stock.getIsSelected())
Animation a = new Animation() {
#Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
holder.categoryLayoutParams.width = startWidth + (int) ((newWidth - startWidth) * interpolatedTime);
//MAYBE PROBLEM HERE??
holder.categoryImage.setLayoutParams(holder.categoryLayoutParams);
holder.arrowIcon.setRotationY(startArrowAngle + (newArrowAngle - startArrowAngle) * interpolatedTime);
}
};
a.setDuration(500);
holder.categoryImage.startAnimation(a);
}
Correct me if i'm wrong :)

Problem Solved, the thing that was causing all this misfunctions was the holder.companyName.requestFocus() that I was calling to get the marquee working in the TextView.
It comes out I don't need the requestFocus() to get it working, and that it was the cause of the problem.

Related

getResources usage inside adapter class in android

I have adapted class like this
public class InboxAdapter extends RecyclerView.Adapter<InboxAdapter.ViewHolder> {
private static final String LOG_TAG = InboxAdapter.class.getSimpleName();
private List<inbox> mInboxes;
private Context context;
public InboxAdapter(List<inbox> inboxes) {
mInboxes = inboxes;
}
#Override
public InboxAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View contactView = inflater.inflate(R.layout.recyclerview_mail_item, parent, false);
ViewHolder viewHolder = new ViewHolder(contactView);
return viewHolder;
}
#Override
public void onBindViewHolder(InboxAdapter.ViewHolder holder, int position) {
inbox inbox = mInboxes.get(position);
holder.tvIcon.setText(inbox.getName().substring(0,1).toUpperCase());
holder.tvEmailSender.setText(inbox.getFrom());
holder.tvEmailTitle.setText(inbox.getSubject());
}
#Override
public int getItemCount() {
return mInboxes.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView tvIcon;
public TextView tvEmailSender;
public TextView tvEmailTitle;
public TextView tvEmailDetails;
public TextView tvEmailTime;
public ViewHolder(View itemView) {
super(itemView);
tvIcon = itemView.findViewById(R.id.tvIcon);
tvEmailSender = itemView.findViewById(R.id.tvEmailSender);
tvEmailTitle = itemView.findViewById(R.id.tvEmailTitle);
tvEmailDetails = itemView.findViewById(R.id.tvEmailDetails);
tvEmailTime = itemView.findViewById(R.id.tvEmailTime);
}
}
private int getRandomMaterialColor(String typeColor) {
int returnColor = Color.GRAY;
int arrayId =getResources().getIdentifier("mdcolor_" + typeColor, "array", getPackageName());
if (arrayId != 0) {
TypedArray colors = getResources().obtainTypedArray(arrayId);
int index = (int) (Math.random() * colors.length());
returnColor = colors.getColor(index, Color.GRAY);
colors.recycle();
}
return returnColor;
}
}
Its working fine but I want set different color for my view holder text so I have added function in it called getRandomMaterialColor. But its using getResources inside it and I am not able to use getActivity. Anyone here can please help me for solve puzzle for me? Thanks!
You can use the view(itemView) in ViewHolder Class for get the resources, for example:
private int getRandomMaterialColor(View view,String typeColor) {
int returnColor = Color.GRAY;
int arrayId =view.getResources().getIdentifier("mdcolor_" + typeColor, "array", getPackageName());
if (arrayId != 0) {
TypedArray colors = view.getResources().obtainTypedArray(arrayId);
int index = (int) (Math.random() * colors.length());
returnColor = colors.getColor(index, Color.GRAY);
colors.recycle();
}
return returnColor;
}

How to put banner in recyclerview

I was able to put the banner in recylerview, but it is overlapping one of the items, it replaces one, instead of getting between them, does anyone know how to fix it?
I'm pulling an api, I do not know if that makes any difference. I searched for several tutorials, but I did not find it, what I need to do is add an admob native banner.
public class HistoricoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
List<Capitulos> capitulos;
Context context;
private static final int DEFAULT_VIEW_TYPE = 0;
private static final int NATIVE_AD_VIEW_TYPE = 1;
public HistoricoAdapter(List<Capitulos> capitulos, Context context) {
this.capitulos = capitulos;
this.context = context;
}
#Override
public int getItemViewType(int position) {
if (position>1 && position % 3 == 0) {
return NATIVE_AD_VIEW_TYPE;
}
return DEFAULT_VIEW_TYPE;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
LayoutInflater layoutInflater = LayoutInflater.from(context);
switch (viewType) {
default:
view = layoutInflater
.inflate(R.layout.row_historico, parent, false);
return new MyViewHolder(view);
case NATIVE_AD_VIEW_TYPE:
view = layoutInflater.inflate(R.layout.ad_unified, parent, false);
return new NativeAdViewHolder(view);
}
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (!(holder instanceof MyViewHolder)) {
return;
}
MyViewHolder holder2 = (MyViewHolder) holder;
Capitulos l = capitulos.get(position);
int aInt = Integer.parseInt(l.getTempo());
Calendar cal = Calendar.getInstance(Locale.ENGLISH);
cal.setTimeInMillis(aInt * 1000L);
String dataFormato = android.text.format.DateFormat.format("dd-MM-yyyy HH:mm:ss", cal).toString();
SpannableString ultimoLido = new SpannableString("Último lido: " + l.getCapitulo());
ultimoLido.setSpan(new StyleSpan(Typeface.BOLD), 0, "Último lido: ".length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
SpannableString data = new SpannableString("Data: " + dataFormato);
data.setSpan(new StyleSpan(Typeface.BOLD), 0, "Data: ".length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
holder2.titulo.setText(l.getTitle());
holder2.ultimoLido.setText(ultimoLido);
holder2.data.setText(data);
Picasso.get()
.load("http://unionmangas.site/assets/uploads/mangas/" + l.getCapa())
.into(holder2.capaView);
}
#Override
public int getItemCount() {
return capitulos.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView titulo;
TextView ultimoLido;
TextView data;
ImageView capaView;
public MyViewHolder(View itemView) {
super(itemView);
titulo = itemView.findViewById(R.id.tituloHistoricoViewID);
ultimoLido = itemView.findViewById(R.id.ultimosLidosViewID);
data = itemView.findViewById(R.id.dataViewID);
capaView = itemView.findViewById(R.id.capaHistoricoViewID);
}
}
public class NativeAdViewHolder extends RecyclerView.ViewHolder {
public AdView mAdView;
public NativeAdViewHolder(View view) {
super(view);
mAdView = (AdView) view.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
mAdView.loadAd(adRequest);
}
}}
Edited answer to ignore the banner click event
Issue is with getItemCount. As you are adding ads in between items you also need to increase the count as well. So that the number of items not get replaced with ad item.
public static final int ITEM_PER_AD = 3;
#Override
public int getItemCount() {
int itemCount = capitulos.size();
itemCount += itemCount / ITEM_PER_AD ;
return itemCount;
}
Accordingly you also need to exclude positions for ads, to get the original item position.
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (!(holder instanceof MyViewHolder)) {
return;
}
int itemPosition = position - position / ITEM_PER_AD ; // need to adjust to get the list item position excluding ads
}
Below code is about to ignore click event for the ads.
#Override
public void onItemClick(View view, int position) {
if (position>1 && position % ITEM_PER_AD == 0) {
return;
}
// rest of the code keep as it is.
}

Get value of textview in cardview from recycleview

I have a RecycleView that has cards inside of it. Each card has a list of companies and prices.
In my onBindViewHolder I have a click event where I would like to get the price of the TextView in that row inside of the Cardview.
Every time I click I always get the value/price of the top item inside of the individual card and never get the price of the item I am clicking.
The data param of the bindData method is what I am using to create the list of items inside of the Cardview.
Any help would be greatly appreciated. I just need to get the value of the correct TextView where I am clicking.
public class StockCardAdapter extends RecyclerView.Adapter<StockCardAdapter.ViewHolder> {
public static class ViewHolder extends RecyclerView.ViewHolder {
protected RelativeLayout mCardBodyLayout;
protected TextView mTitleTextView;
public ViewHolder(View v) {
super(v);
mCardBodyLayout = (RelativeLayout) v.findViewById(R.id.card_body);
mTitleTextView = (TextView) v.findViewById(R.id.card_title);
}
public void bindData(StockCategoryModel data, Context ctx) {
this.mTitleTextView.setText(data.getCategoryName());
TableLayout tableLayout = new TableLayout(ctx);
int rows = data.getStockList().size();
for (int r = 0; r < rows; r++) {
TableRow row = new TableRow(ctx);
TableLayout.LayoutParams rowParams = new TableLayout.LayoutParams (TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
rowParams.setMargins(0, 0, 0, 16);
row.setLayoutParams(rowParams);
LinearLayout rl = new LinearLayout(ctx);
rl.setOrientation(LinearLayout.VERTICAL);
Integer priceColor = SharedUtilities.getColor(data.getStockList().get(r).priceChange, ctx);
//price row
LinearLayout priceLayout = new LinearLayout(ctx);
priceLayout.setOrientation(LinearLayout.HORIZONTAL);
priceLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
priceLayout.setWeightSum(4);
LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f);
final TextView price_text = new TextView(ctx);
price_text.setTag("priceTag");
price_text.setText(data.getStockList().get(r).price);
price_text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
price_text.setTextColor(Color.BLACK);
price_text.setLayoutParams(textViewParams);
priceLayout.addView(price_text);
//company row
final TextView name_text = new TextView(ctx);
name_text.setText(data.getStockList().get(r).company);
name_text.setTextColor(Color.GRAY);
name_text.setLayoutParams( new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT));
name_text.setMaxWidth(700);
name_text.setEllipsize(TextUtils.TruncateAt.END);
name_text.setMaxLines(1);
name_text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
rl.addView(priceLayout);
rl.addView(name_text);
row.addView(rl);
tableLayout.setStretchAllColumns(true);
tableLayout.addView(row);
}
mCardBodyLayout.addView(tableLayout);
}
}
private List<StockCategoryModel> mDataset;
private Context mContext;
// Constructor
public StockCardAdapter(List<StockCategoryModel> dataset, Context ctx) {
this.mDataset = dataset;
this.mContext = ctx;
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
// Create new views (invoked by the layout manager)
#Override
public StockCardAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup,
int viewType) {
// create a new view
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_layout, viewGroup, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v2) {
final TextView textViewName = (TextView) v2.findViewWithTag("priceTag"); ;
final String priceTag = textViewName.getText().toString();
}
});
holder.bindData(mDataset.get(position), mContext);
}
// Return the size of your dataset (invoked by the layout manager)
#Override
public int getItemCount() {
return mDataset.size();
}
}
What you need to do is to set a click listener to every row separately.
Why do you always get the first row value?
This code,
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v2) {
final TextView textViewName = (TextView) v2.findViewWithTag("priceTag"); ;
final String priceTag = textViewName.getText().toString();
}
});
Sets a click listener to every list item - the whole card. This means that every time the user clicks inside the card view bounds - this callback will be fired. BUT, who will be v2? It will always be the view to which we set the listener to - in this case - the whole card.
This means that every time you call v2.findViewWithTag("priceTag"); you are searching the first child of the entire card that has the tag "priceTag" - which is the "top" item in the card.
How to solve this issue?
If you want to identify which child is being clicked - you will have to set a click listener to each child directly.
As an example, try this code (see ADDED comments):
public class StockCardAdapter extends
RecyclerView.Adapter<StockCardAdapter.ViewHolder> {
public static class ViewHolder extends RecyclerView.ViewHolder {
protected RelativeLayout mCardBodyLayout;
protected TextView mTitleTextView;
public ViewHolder(View v) {
super(v);
mCardBodyLayout = (RelativeLayout) v.findViewById(R.id.card_body);
mTitleTextView = (TextView) v.findViewById(R.id.card_title);
}
public void bindData(StockCategoryModel data, Context ctx, View.OnClickListener listener) {
this.mTitleTextView.setText(data.getCategoryName());
TableLayout tableLayout = new TableLayout(ctx);
int rows = data.getStockList().size();
for (int r = 0; r < rows; r++) {
TableRow row = new TableRow(ctx);
TableLayout.LayoutParams rowParams = new TableLayout.LayoutParams (TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
rowParams.setMargins(0, 0, 0, 16);
row.setLayoutParams(rowParams);
LinearLayout rl = new LinearLayout(ctx);
rl.setOrientation(LinearLayout.VERTICAL);
Integer priceColor = SharedUtilities.getColor(data.getStockList().get(r).priceChange, ctx);
//price row
LinearLayout priceLayout = new LinearLayout(ctx);
priceLayout.setOrientation(LinearLayout.HORIZONTAL);
priceLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
priceLayout.setWeightSum(4);
LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f);
final TextView price_text = new TextView(ctx);
price_text.setTag("priceTag");
price_text.setText(data.getStockList().get(r).price);
price_text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
price_text.setTextColor(Color.BLACK);
price_text.setLayoutParams(textViewParams);
priceLayout.addView(price_text);
//company row
final TextView name_text = new TextView(ctx);
name_text.setText(data.getStockList().get(r).company);
name_text.setTextColor(Color.GRAY);
name_text.setLayoutParams( new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT));
name_text.setMaxWidth(700);
name_text.setEllipsize(TextUtils.TruncateAt.END);
name_text.setMaxLines(1);
name_text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
rl.addView(priceLayout);
rl.addView(name_text);
row.addView(rl);
tableLayout.setStretchAllColumns(true);
tableLayout.addView(row);
// *ADDED* set the listener directly to each row
row.setOnClickListener(listener);
}
mCardBodyLayout.addView(tableLayout);
}
}
private List<StockCategoryModel> mDataset;
private Context mContext;
// Constructor
public StockCardAdapter(List<StockCategoryModel> dataset, Context ctx) {
this.mDataset = dataset;
this.mContext = ctx;
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
// Create new views (invoked by the layout manager)
#Override
public StockCardAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup,
int viewType) {
// create a new view
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_layout, viewGroup, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
// *ADDED* Send the callback to the bind method
holder.bindData(mDataset.get(position), mContext, new View.OnClickListener() {
#Override public void onClick(View v2) {
final TextView textViewName = (TextView) v2.findViewWithTag("priceTag"); ;
final String priceTag = textViewName.getText().toString();
}
}));
}
// Return the size of your dataset (invoked by the layout manager)
#Override
public int getItemCount() {
return mDataset.size();
}
}
NOTE:
This is NOT the proper way to handle a RecyclerView - You never want to create a new object (in this case new View.OnClickListener() {}) inside the data binding - that will reduce performance. But this is another issue :)
The tag of textView is same in all the cases and that's why it is repeating, only the value of first item. Assign unique tag usually by concatenating "priceTag" with position. Make changes in your code as follows:
public void bindData(StockCategoryModel data, Context ctx, int position) {
//All your code
//..
//...
final TextView price_text = new TextView(ctx);
price_text.setTag("priceTag"+String.valueOf(position));
}
and in your onBindViewHolder:
final TextView textViewName = (TextView) v2.findViewWithTag("priceTag"+String.valueOf(position));
holder.bindData(mDataset.get(position), mContext,position);
Find child view from Tag
please call it.
TextView priceTagTextView =(TextView)getViewsByTag(mCardBodyLayout, "priceTag").get(0);
private ArrayList<View> getViewsByTag(ViewGroup root, String tag){
ArrayList<View> views = new ArrayList<View>();
final int childCount = root.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = root.getChildAt(i);
if (child instanceof ViewGroup) {
getViewsByTag((ViewGroup) child, tag));
}
final Object tagObj = child.getTag();
if (tagObj != null && tagObj.equals(tag)) {
views.add(child);
}
}
return views;
}

SnapHelper Item Position

I'm using vertical RecyclerView to list my items and SnapHelper to snap center item. The idea is to randomize selection, so user swipe screen or shake the device and it is scrolling to random position.
Number of items is 20, however I use Integer.MAX_VALUE for the number of elements in RecyclerView and initialize RecyclerView with position Integer.MAX_VALUE / 2 to create some kind of endless list.
To scroll to random position on device shake I need to know current snapped item position.
Is there any way to do it?
Here is my Fragment code:
public class PlaceListFragment extends Fragment {
private static final String TAG = "PlaceListFragment";
public static final String ARG_KEY1 = "key1";
private ArrayList<PlaceItem> places;
private RecyclerView recyclerView;
private SensorManager sensorManager;
private float accelValue;
private float accelLast;
private float shake;
SnapHelper snapHelper;
Vibrator vibe;
public static PlaceListFragment newInstance() {
Bundle args = new Bundle();
PlaceListFragment fragment = new PlaceListFragment();
fragment.setArguments(args);
return fragment;
}
public static PlaceListFragment newInstance(ArrayList<PlaceItem> places) {
Bundle args = new Bundle();
args.putParcelableArrayList(PlaceListActivity.KEY_PLACES, places);
PlaceListFragment fragment = new PlaceListFragment();
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate()");
super.onCreate(savedInstanceState);
places = getArguments().getParcelableArrayList(PlaceListActivity.KEY_PLACES);
accelValue = SensorManager.GRAVITY_EARTH;
accelLast = SensorManager.GRAVITY_EARTH;
shake = 0.00f;
vibe = (Vibrator) getActivity().getSystemService(Context.VIBRATOR_SERVICE);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_place_list, container, false);
recyclerView = (RecyclerView) v.findViewById(R.id.place_list);
snapHelper = new LinearSnapHelper();
snapHelper.attachToRecyclerView(recyclerView);
recyclerView.setOnFlingListener(snapHelper);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(new PlaceListAdapter(getActivity(), places));
recyclerView.scrollToPosition(PlaceListAdapter.MIDDLE);
sensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);
sensorManager.registerListener(sensorListener, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_NORMAL);
return v;
}
#Override
public void onResume() {
super.onResume();
}
#Override
public void onPause() {
super.onPause();
}
private final SensorEventListener sensorListener = new SensorEventListener() {
#Override
public void onSensorChanged(SensorEvent event) {
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
accelLast = accelValue;
accelValue = (float) Math.sqrt((double) (x*x + y*y + z*z));
float delta = accelValue - accelLast;
shake = shake * 0.9f + delta;
if (shake > 12) {
vibe.vibrate(200);
}
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
}
And here is adapter:
public class PlaceListAdapter extends RecyclerView.Adapter<PlaceListAdapter.PlaceAdapterHolder> {
private final FragmentActivity context;
public static final int HALF_MAX_VALUE = Integer.MAX_VALUE/2;
public static int MIDDLE;
private List<PlaceItem> placeItems;
public static class PlaceAdapterHolder extends RecyclerView.ViewHolder {
private ImageView image;
private TextView textMain;
private TextView textRating;
public PlaceAdapterHolder(View itemView) {
super(itemView);
image = (ImageView) itemView.findViewById(R.id.icon);
textMain = (TextView) itemView.findViewById(R.id.txt_main_line);
textRating = (TextView) itemView.findViewById(R.id.txt_right_field);
}
public void bindPlace(PlaceItem placeItem) {
String placeName = placeItem.getName() == null? "?":placeItem.getName();
String firstLetter = placeName.substring(0, 1);
ColorGenerator generator = ColorGenerator.MATERIAL; // or use DEFAULT
int color = generator.getColor(placeName);
TextDrawable drawable = TextDrawable.builder()
.beginConfig()
.toUpperCase()
.endConfig()
.buildRect(firstLetter, color);
image.setImageDrawable(drawable);
textMain.setText(placeItem.getName());
textRating.setText(placeItem.getRating());
}
}
public PlaceListAdapter(FragmentActivity context, List<PlaceItem> placeItems) {
this.context = context;
this.placeItems = placeItems;
MIDDLE = HALF_MAX_VALUE - HALF_MAX_VALUE % placeItems.size();
}
#Override
public PlaceListAdapter.PlaceAdapterHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.place_list_one_line_item, parent, false);
return new PlaceAdapterHolder(view);
}
#Override
public int getItemCount() {
return Integer.MAX_VALUE;
}
#Override
public void onBindViewHolder(PlaceListAdapter.PlaceAdapterHolder holder, final int position) {
final PlaceItem placeItem = getItem(position);
holder.bindPlace(placeItem);
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FragmentManager fm = context.getSupportFragmentManager();
PlaceDetailsFragment dialog = PlaceDetailsFragment.newInstance(getItem(position));
dialog.show(fm, "DETAILS_DIALOG");
}
});
}
private PlaceItem getItem(int position)
{
return placeItems.get(position % placeItems.size());
}
}
I used this on a project that had a RecyclerView with SnapHelper, not sure if it is what you want.
mRecyclerView.setHasFixedSize(true);
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
mRecyclerView.setLayoutManager(mLayoutManager);
// specify an adapter (see also next example)
mAdapter = new DemoSlidesAdapter(getApplicationContext());
mRecyclerView.setAdapter(mAdapter);
final SnapHelper snapHelper = new LinearSnapHelper();
snapHelper.attachToRecyclerView(mRecyclerView);
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if(newState == RecyclerView.SCROLL_STATE_IDLE) {
View centerView = snapHelper.findSnapView(mLayoutManager);
int pos = mLayoutManager.getPosition(centerView);
Log.e("Snapped Item Position:",""+pos);
}
}
});
I try to use this code with a PagerSnapHelper to mimic the pager behaviour and it was useful but i found some corner cases to solve, if you move fast from the last page to the first one and keep swapping until see the boundarie then the IDLE state doesnt happen and you lose your index. to solve that I move out the position from the IF and add a extra condition for this corner case.
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)
val centerView = snapHelper.findSnapView(mLayoutManager)
val pos = mLayoutManager.getPosition(centerView!!)
if (newState == RecyclerView.SCROLL_STATE_IDLE || (pos == 0 && newState == RecyclerView.SCROLL_STATE_DRAGGING)) {
Log.d("BINDING", "positionView SCROLL_STATE_IDLE: $pos")
}
}
Code is in kotlin hope it helps
private fun recyclerViewScrollListener() = object: RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
// layoutManager is LinearLayoutManager
val pos = layoutManager.findFirstCompletelyVisibleItemPosition()
Log.e(TAG, "onScrollStateChanged: $pos")
}
}
}
recyclerView.setOnScrollListener(recyclerViewScrollListener())
recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener(){
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
....
}
})

Popup gridview cell onTOuch

I have a grid View In which I am adding Button dynamically.
I am setting OnTouch listener to grid view.
I want when my finger move on the particular cell then that cell element should get popup
similar way our android keyboard do.
public class MainActivity extends Activity {
private ArrayList<Integer> data;
private GridView gv;
private TextView biggerView = null;
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createData();
gv = (GridView) findViewById(R.id.grid);
gv.setNumColumns(10);
gv.setAdapter(new FilterButtonAdapter(data, this));
gv.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
try {
int position = gv.pointToPosition((int) event.getX(),
(int) event.getY());
View v = (View) gv.getChildAt(position);
if (v != null) {
gv.requestFocus();
gv.setSelection(gv.pointToPosition( (int)
event.getX(), (int) event.getY()));
}
return true;
} catch (Exception e) {
return true;
}
}
if (event.getAction() == MotionEvent.ACTION_UP) {
int position = gv.pointToPosition((int) event.getX(),
(int) event.getY());
View v = (View) gv.getChildAt(position);
if (v != null) {
gv.clearFocus();
TextView tv = (TextView) v.findViewById(R.id.texttoadd);
Toast.makeText(MainActivity.this, tv.getText(),
Toast.LENGTH_SHORT).show();
}
return true;
}
return false;
}
});
}
private void createData() {
data = new ArrayList<Integer>();
for (int i = 0; i < 200; i++) {
data.add(i);
}
}
enter code here
i have write this code which is giving me the selected item but when item are more then grid is scrolled and after that the am not getting the item which i am selecting
i have figured out that the x and y position is getting change when grid is scrolled
i may be wrong
please help
I think that suggested in the question way of touch position detection might be not effective, because there's more high-level way to obtain scrolling position.
The main ideas of the implementation are the following:
Use onScrollChanged() to track scroll position at every moment;
Display selection as separate view above GridView;
Track if selected item is visible (using this question);
So, to obtain proper scroll callback, slightly customized GridView is needed:
public class ScrollAwareGridView extends GridView {
/** Callback interface to report immediate scroll changes */
public interface ImmediateScrollListener {
void onImmediateScrollChanged();
}
/** External listener for */
private ImmediateScrollListener mScrollListener = null;
public ScrollAwareGridView(final Context context) {
super(context);
}
public ScrollAwareGridView(final Context context, final AttributeSet attrs) {
super(context, attrs);
}
public ScrollAwareGridView(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onScrollChanged(final int l, final int t, final int oldl, final int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (null != mScrollListener) {
mScrollListener.onImmediateScrollChanged();
}
}
/**
* #param listener {#link ImmediateScrollListener}
*/
public void setImmediateScrollListener(final ImmediateScrollListener listener) {
mScrollListener = listener;
}
}
It will be placed in the xml the following way (main.xml):
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.TestApp.ScrollAwareGridView
android:id="#+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3" />
<!-- Selection view -->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/selectedImage"
android:visibility="gone" />
</RelativeLayout>
In above xml there's also selection view presented.
Activity will handle selection of items (however, it might be better to keep selection and scroll tracking logic in separate object (grid adapter or specific grid fragment) in order not to keep grid-specific logic in Activity code):
public class MyActivity extends Activity implements ScrollAwareGridView.ImmediateScrollListener, AdapterView.OnItemClickListener {
private static final String TAG = "MyActivity";
/** To start / pause music */
private ImageView mSelectedImage = null;
/** position of selected item in the adapter */
private int mSelectedPosition;
/** Main grid view */
private ScrollAwareGridView mGrid;
/** Adapter for grid view */
private ImageAdapter mAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Show the layout with the test view
setContentView(R.layout.main);
mSelectedImage = (ImageView) findViewById(R.id.selectedImage);
mGrid = (ScrollAwareGridView) findViewById(R.id.grid);
if (null != mGrid) {
mAdapter = new ImageAdapter(this);
mGrid.setAdapter(mAdapter);
mGrid.setImmediateScrollListener(this);
mGrid.setOnItemClickListener(this);
}
}
#Override
protected void onPause() {
super.onPause();
}
#Override
protected void onResume() {
super.onResume();
mSelectedImage.setImageBitmap(null);
mSelectedImage.setVisibility(View.GONE);
mSelectedPosition = -1;
}
#Override
public void onImmediateScrollChanged() {
if (mSelectedPosition >= 0) {
int firstPosition = mGrid.getFirstVisiblePosition(); // This is the same as child #0
int wantedChild = mSelectedPosition - firstPosition;
// Say, first visible position is 8, you want position 10, wantedChild will now be 2
// So that means your view is child #2 in the ViewGroup:
if (wantedChild < 0 || wantedChild >= mGrid.getChildCount()) {
Log.w(TAG, "Unable to get view for desired position, because it's not being displayed on screen.");
mSelectedImage.setVisibility(View.INVISIBLE);
return;
} else {
mSelectedImage.setVisibility(View.VISIBLE);
}
// Could also check if wantedPosition is between listView.getFirstVisiblePosition() and listView.getLastVisiblePosition() instead.
final View selectedView = mGrid.getChildAt(wantedChild);
if (null != selectedView && mSelectedImage.getVisibility() == View.VISIBLE) {
// Put selected view on new position
final ViewGroup.MarginLayoutParams zoomedImageLayoutParams = (ViewGroup.MarginLayoutParams) mSelectedImage.getLayoutParams();
// 200 is difference between zoomed and not zoomed images dimensions
// TODO: Avoid hardcoded values and use resources
final Integer thumbnailX = mGrid.getLeft() + selectedView.getLeft() - (ImageAdapter.HIGHLIGHTED_GRID_ITEM_DIMENSION - ImageAdapter.GRID_ITEM_DIMENSION) / 2;
final Integer thumbnailY = mGrid.getTop() + selectedView.getTop() - (ImageAdapter.HIGHLIGHTED_GRID_ITEM_DIMENSION - ImageAdapter.GRID_ITEM_DIMENSION) / 2;
zoomedImageLayoutParams.setMargins(thumbnailX,
thumbnailY,
0,
0);
mSelectedImage.setLayoutParams(zoomedImageLayoutParams);
}
}
}
#Override
public void onItemClick(final AdapterView<?> parent, final View view, final int position, final long id) {
mSelectedPosition = position;
final Bitmap bm = mAdapter.getImage(position);
// Obtain image from adapter, with check if image presented
if (bm != null) {
final ViewGroup.MarginLayoutParams zoomedImageLayoutParams = (ViewGroup.MarginLayoutParams) mSelectedImage.getLayoutParams();
// 200 is difference between zoomed and not zoomed images dimensions
// TODO: Avoid hardcoded values and use resources
final Integer thumbnailX = mGrid.getLeft() + view.getLeft() - (ImageAdapter.HIGHLIGHTED_GRID_ITEM_DIMENSION - ImageAdapter.GRID_ITEM_DIMENSION) / 2;
final Integer thumbnailY = mGrid.getTop() + view.getTop() - (ImageAdapter.HIGHLIGHTED_GRID_ITEM_DIMENSION - ImageAdapter.GRID_ITEM_DIMENSION) / 2;
zoomedImageLayoutParams.setMargins(thumbnailX,
thumbnailY,
0,
0);
zoomedImageLayoutParams.height = ImageAdapter.HIGHLIGHTED_GRID_ITEM_DIMENSION;
zoomedImageLayoutParams.width = ImageAdapter.HIGHLIGHTED_GRID_ITEM_DIMENSION;
mSelectedImage.setImageBitmap(bm);
mSelectedImage.setScaleType(ImageView.ScaleType.CENTER);
mSelectedImage.setLayoutParams(zoomedImageLayoutParams);
mSelectedImage.setVisibility(View.VISIBLE);
}
}
}
Below is GridViews adapter. However there's nothing specific in it which related to scrolling tracking (most of code I've reused from this answer):
public class ImageAdapter extends BaseAdapter {
private static final String TAG = "ImageAdapter";
/** For creation of child ImageViews */
private Context mContext;
public static final Integer[] IMAGES_RESOURCES = {
R.drawable.image001, R.drawable.image002, R.drawable.image003, R.drawable.image004,
R.drawable.image005, R.drawable.image006, R.drawable.image007, R.drawable.image008,
R.drawable.image009, R.drawable.image010, R.drawable.image011, R.drawable.image012,
R.drawable.image013, R.drawable.image014, R.drawable.image015, R.drawable.image016,
R.drawable.image017, R.drawable.image018, R.drawable.image019, R.drawable.image020,
R.drawable.image021, R.drawable.image022, R.drawable.image023, R.drawable.image024,
R.drawable.image025, R.drawable.image026, R.drawable.image027, R.drawable.image028,
R.drawable.image029, R.drawable.image030, R.drawable.image031, R.drawable.image032,
R.drawable.image033, R.drawable.image034, R.drawable.image035, R.drawable.image036,
R.drawable.image037, R.drawable.image038, R.drawable.image039, R.drawable.image040,
R.drawable.image041, R.drawable.image042, R.drawable.image043, R.drawable.image044,
R.drawable.image045, R.drawable.image046, R.drawable.image047, R.drawable.image048,
R.drawable.image049, R.drawable.image050
};
// TODO: use resources for that sizes, otherwise You'll GET PROBLEMS on other displays!
public final static int GRID_ITEM_DIMENSION = 300;
public final static int HIGHLIGHTED_GRID_ITEM_DIMENSION = 500;
private Bitmap mHolder = null;
private static final int CACHE_SIZE = 50 * 1024 * 1024; // 8 MiB cache
/** Cache to store all decoded images */
private LruCache<Integer, Bitmap> mBitmapsCache = new LruCache<Integer, Bitmap>(CACHE_SIZE) {
#Override
protected int sizeOf(final Integer key, final Bitmap value) {
return value.getByteCount();
}
#Override
protected void entryRemoved(final boolean evicted, final Integer key, final Bitmap oldValue, final Bitmap newValue) {
if (!oldValue.equals(mHolder)) {
oldValue.recycle();
}
}
};
// Constructor
public ImageAdapter(Context c){
mContext = c;
mHolder = BitmapFactory.decodeResource(c.getResources(), R.drawable.ic_launcher, null);
}
#Override
public int getCount() {
return IMAGES_RESOURCES.length;
}
#Override
public Object getItem(int position) {
return IMAGES_RESOURCES[position];
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(GRID_ITEM_DIMENSION, GRID_ITEM_DIMENSION));
} else {
imageView = (ImageView) convertView;
}
final Bitmap itemBitmap = mBitmapsCache.get(IMAGES_RESOURCES[position]);
if (itemBitmap == null || itemBitmap.isRecycled()) {
Log.e(TAG, position + " is missed, launch decode for " + IMAGES_RESOURCES[position]);
imageView.setImageBitmap(mHolder);
mBitmapsCache.put(IMAGES_RESOURCES[position], mHolder);
new BitmapWorkerTask(mBitmapsCache, mContext.getResources(), this).execute(IMAGES_RESOURCES[position]);
} else {
Log.e(TAG, position + " is here for " + IMAGES_RESOURCES[position]);
imageView.setImageBitmap(itemBitmap);
}
return imageView;
}
/**
* Obtains image at position (if there's only holder, then null to be returned)
*
* #param position int position in the adapter
*
* #return {#link Bitmap} image at position or null if image is not loaded yet
*/
public Bitmap getImage(final int position) {
final Bitmap bm = mBitmapsCache.get(IMAGES_RESOURCES[position]);
return ((mHolder.equals(bm) || bm == null) ? null : bm.copy(Bitmap.Config.ARGB_8888, false));
}
/** AsyncTask for decoding images from resources */
static class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
private int data = 0;
private final LruCache<Integer, Bitmap> mCache;
private final Resources mRes;
private final BaseAdapter mAdapter;
public BitmapWorkerTask(LruCache<Integer, Bitmap> cache, Resources res, BaseAdapter adapter) {
// nothing to do here
mCache = cache;
mRes = res;
mAdapter = adapter;
}
// Decode image in background.
#Override
protected Bitmap doInBackground(Integer... params) {
data = params[0];
// Use sizes for selected bitmaps for good up-scaling
return decodeSampledBitmapFromResource(mRes, data, GRID_ITEM_DIMENSION, GRID_ITEM_DIMENSION);
}
// Once complete, see if ImageView is still around and set bitmap.
#Override
protected void onPostExecute(Bitmap bitmap) {
mCache.put(data, bitmap);
mAdapter.notifyDataSetChanged();
}
}
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
options.outHeight = GRID_ITEM_DIMENSION;
options.outWidth = GRID_ITEM_DIMENSION;
return BitmapFactory.decodeResource(res, resId, options);
}
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and width
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
}

Categories

Resources