Tappable items within recyclerview with OnItemTouchListener - android

inception sounds
It might not be the best idea, but here's a 4 second gif of what I'm trying to do and the problem I've run into.
https://i.imgur.com/Z3iAyNc.gifv (sorry, embedding wasn't allowed).
I have a RecyclerView with in each item a LinearLayout with category details and a hidden LinearLayout with another recyclerView. The idea is you see the categories, and once you tap on it, it expands to show the relevant posts. Another tap on the category should open the category detail page and a tap on a post should open the relevant post detail page.
I have it working, but as you can see in the gif, I have to tap at least two times, even though I never wrote it like that. It's the same for the LinearLayout's onClickListener It's like the parent RecyclerView hijacks the first tap.
Here's a schematic rundown of my code
adapter.setOnItemTouchListener(new RecyclerItemListener.RecyclerTouchListener() {
#Override
public void onClickItem(View v, int position) {
final LinearLayout postView = (LinearLayout) v.findViewById(R.id.profileCheckinDetailsView);
// The IF statement prevents reloading the view when it is already open.
if (postView.getVisibility()==View.GONE){
RelativeLayout itemView = (RelativeLayout) v.findViewById(R.id.ItemView);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
[... etc]
And beneath it there's another setOnItemTouchListener for the child RecyclerView.
Am I doing this right, and if so, how to make sure both items register the first tap as well?
Thanks in advance!

Sometimes typing out a question helps you think differently. I removed the top recyclerview's touch listener and replaced the item layout with this. Both layouts get their own onClickListener. With ((LinearLayout)v.getParent()) I managed to have them control eachother.
Much easier and more effective I believe.

Related

Repeatable process where image can be added when a button is click

I am trying to attempt the below :
What i am trying to do is whenever a user click on a button, it will add a image to the layout(which the user can scale and move to any position within the layout). The user can repeat this process hence if the user click 6 times on the button, there should be 6 image in the layout.
i have google around, looking for some sample but ain't sure if its the correct one to use. maybe i did not use the correct keyword to search. So far i have seen is endless adapter and RecyclerView but both seen to be mainly for "ListView" type.
Anyone can suggest or share a link/example that i can read up and learn from? It will be a great help.
If you want to add image on button click set on-click listener on it. You need to get how many time the user have clicked the button.
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//get adapter and view
//get image to add
addImage(i); //to desire view
}
});

How to get button inside of RecyclerView item

How do i get a specific view inside of a RecyclerView item? For example I have a floating action button inside of a recyclerview item. The recyclerview card can swipe to trigger an event (new activity) and when the user swipes i am making the fab invisible. I want so that when the user returns back to the activity with the recyclerview, the fab is visible again.
I've tried to implement this a few ways, but for some reason it's taking the fab from the NEXT card/item and placing it on the original card/item that was swiped. This is a problem because the next card might have a different colored fab or no fab at all. What is happening is that it's taking the most recent viewholder item, even if the previous one is the one I want to deal with.
So i need a way to reference the fab in the current item. I'm currently setting it to currentFab = holder.mFab (but again, it's taking the most recent holder item even though it's not the one I pressed on). I need a way to reference the fab in a specific item.
I've tried something similar in the past, I've added the Swipe function in "onBindViewHolder ", Using a Swipe Library.
What I have made is, A CardView, Inside it there are 2 Strings, And a Button, this Button will be invisible according to specific conditions, So because it's inside onBindVewHolder, it was easy to make some edits on the button.
Here's a sample :
holder.Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.Button.setVisibility(View.GONE);
}
});
Ofcourse this isn't a "fully working code" it's just an example, Plus you didn't put any code that you have tried.

Handling multiple clickable areas in listview

I'm using a ListView to show some images and names in my application. I can make the ListView tiles clickable and start new activities as well . But now i want to do different actions for the different items that are on one listview tile. If user clicks on image, image opens and the textView will show some toast or something. The ImageView and TextView are on the same ListView Tile. How can i achieve this? Any Help?
Define OnClickListener events in your adapter for the button and textview as required and you are done. They will precede the click event for the list item by default.
if the contained views in your custom row layout does not have a click event it will fall back to the click event of the list view item.
You can implement click event in adapter, but you also implement it in activity.
You can find solution in :http://www.migapro.com/click-events-listview-gridview/
hope it can help you.
Some note: should not use view holder in gridview like above tuturial, it can make some bug, can implement direct like this:
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
((ListView) parent).performItemClick(v, position, 0); // Let the event be handled in onItemClick()
}
});
Another way to implement this elegantly is to add secondary views called 'accessories' to the item views associated with your ListView. This is explaned quite well in the link below by Cyril Mottier: A Google Developer Expert on the Android platform:
http://cyrilmottier.com/2011/11/23/listview-tips-tricks-4-add-several-clickable-areas/

setFocus() causing two pushes to select imageview

I'm developing a game Android app and I have a HorizontalScrollView that is being populated with ImageViews. Each ImageView is clickable to select the level you want to play. I'm using a SavedPreference to keep track of the last played level, and using that to determine which image is on screen when you load the menu. That way, if you're on level 30, you don't have to scroll all the way from level 1 every time.
I'm using a requestFocus() to bring that ImageView onto the screen, and it works fine, except for two relatively minor (but annoying) issues.
Here is what I'm doing in my for loop that populates the { HorizontalScrollView`:
final ImageView iv = new ImageView(LevelSelect.this);
iv.setImageResource(levelImages[i]);
iv.setFocusable(true);
iv.setFocusableInTouchMode(true);
iv.setId(i);
LinearLayout ll= (LinearLayout)findViewById(R.id.levelSelectGallery);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(300, LinearLayout.LayoutParams.MATCH_PARENT);
and I make it clickable:
String str = getString(R.string.level) + i + getString(R.string.avail);
if (storage.getBoolean(str,false)) { //check if level is available (unlocked)
iv.setClickable(true);
final int finalI = i;
iv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editor.putInt(getString(R.string.Current_Level_savepreferences), finalI); //set current level to clicked image
Intent intent = new Intent(LevelSelect.this, LevelStart.class);
LevelSelect.this.startActivity(intent); // begin LevelStart activity
}
});
} else {
iv.setImageResource(lockedImages[i]); // show locked image
}
and outside the loop, I set the focus:
//check if there is a saved level
focusLevel = storage.getInt(getString(R.string.Saved_Level), 5);
//set focus on saved level
findViewById(focusLevel).requestFocus();
This brings the focused item onto the screen, but just at the edge. I would prefer it to be centered if possible. I can live with that though. The main issue is that it seems that by setting the focus, it's now requiring two presses on the image to trigger the onClickListener. I assume the first press is changing the focus and the second is clicking. If I remove the focus related code, it only requires one press.
Is there a better way to scroll the view to the desired image, or something different I should be doing with the focusing?
Why don't you just put focusLevel inside the onClickListener? That way tapping the image focuses it automatically.
So, I haven't found a way around the double press issue when the item is focused, but I did discover a different solution that works for my situation. Instead of using the HorizontalScrollView with a LinearLayout, I've now implemented a ViewPager which centers my ImageViews for me, and also allows me to scroll to an item programatically using setCurrentItem(). I don't have to use focusing at all.
For anyone looking to do the same, Philipp's example here helped me a lot: How to implement a ViewPager with different Fragments / Layouts

How to scroll views whithn view in android?

What I want to do is to scroll view after listening a button.
Simply user taps button and view is scrolled to specific id below.
I tried to use scrollto and scrollby passing as an argument reference to object to which I want to scroll with no effect.
Anyone solved this problem yet?
I have spent many hours on this.
Try something like this. It makes sure that the item selected it the one in view:
ListView listView;
int positionSelected = listView.getSelectedItemPosition();
listView.setSelection(positionSelected);

Categories

Resources