Android: Changing Visibility of an item inside all listview rows - android

I have a ListView in my application which can, after user requests it, be re-arrangeable via the drag of an imageview at the start of each listview's row. I present the user with the listview in a small window, and in order to maximize the space, I set the visibility of the drag view as GONE, and plan on only setting it to Visible when user selects a menu item that calls for Edit List Items.
Here's the layout for a row item:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/activatedBackgroundIndicator"
android:orientation="horizontal" >
<ImageView
android:id="#id/drag_handle"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_margin="8dp"
android:visibility="gone"
android:src="#drawable/ic_drag_dots_holo_dark" />
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp"
android:background="?android:attr/dividerVertical" />
<TextView
android:id="#+id/drag_n_drop_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:maxLines="1"
android:minHeight="24dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="?android:attr/textColorPrimary" />
</LinearLayout>
Here's the code for the menu selection:
#Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_edit_playlist:
if (inEditMode) {
//If we are already showing the drag view, hide it.
inEditMode = false;
findViewById(R.id.drag_handle).setVisibility(View.GONE);
}
else {
//If the drag view is hidden, make it visible.
inEditMode = true;
findViewById(R.id.drag_handle).setVisibility(View.VISIBLE);
}
return true;
default:
return super.onContextItemSelected(item);
}
}
The problem with this code, is that when I call setVisibility() it only changes the visibility of the drag view on the firstmost item of the listview, and the dragview on all other rows remains hidden.
What would I do to make ALL dragViews on all rows visible in this case?
EDIT
Here's an image of how I want it to look after pressing edit, and here's an image of how it looks right now, with the code above.
Thanks!

The problem with this code, is that when I call setVisibility() it
only changes the visibility of the drag view on the firstmost item of
the listview, and the dragview on all other rows remains hidden.
That behavior it's normal as you're trying to change the visibility of a View from a row using findViewById(), method which will return the first occurrence of a View with that id from the layout. That first occurrence will be the situated in the first visible row of the list(anyway the findViewById method will not work, as you can probably see the views in the ListView's rows will all have the same ids).
What would I do to make ALL dragViews on all rows visible in this
case?
The proper way is to let the adapter of the ListView know in which state it's currently in(inEditMode) and show/hide the drag handle in the getView method based on that. Once you do this all you have to do in the onContextItemSelected is to update the inEditMode state variable and call notifyDataSetChanged() method on the adapter.

Related

How to make recyclerView_items unclickable, while one of them is clicked and working?

Premise
Each of my RecyclerView_items displays an image, and changes its image_resource when tapped.
One of them is the right answer which has additional function: navigation to another fragment 5-second after changing its image.
Basically, the function of clickListener of RecyclerView_items is
wrong item
click -> change image_resource
right item
click -> change image_resource -> delay(5000) -> navigate to another fragment
Problem
Then, my problem is that after the right_item is tapped, other items are clickable during delay(5000).
I don't want them to change their images during delay(5000).
How to do it?! Thanks in advance.
You can achieve this by adding a flag at time when user click right image then make that variable false. As such after the delay before moving to the next fragment make it true again.
boolean flag = true;
then in your item click method
view.setOnClickListner {
if (flag) {
if (rightimageclicked) {
flag = false;
delay {
flag = true;
// Move to other fragment
}
} else {
// change the image for wrong click
}
}
}
The simple trick you can use is make a FrameLayout or View that overlaps on the RecyclerView. Set ists visibility to GONE. Then inside activity or fragment where you can access this FrameLayout or View which is overlapping, add an empty OnClickListener on this view.
*viewId*.setOnClickListener { }
Now set its visibility to VISIBLE when you call delay. When delay finished again set its visibility to GONE
This is what I understood, you want to lock the entire screen (make all items or buttons unclickable) when you are in delay(5000).
This is what I would do, give a CLICKABLE overlay view on top of recycler view and toggle its visibility. I used the same for locking the entire screen while making an API call.
view_overlay.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rl_progress"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:clickable="true"
android:focusable="true"
android:elevation="5dp"
android:visibility="invisible">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#android:color/transparent"
android:indeterminateTint="#android:color/transparent" />
</RelativeLayout>
Obviously you can replace ProgressBar with just View to get a transaprent view.
To include it in your main view, give this at the bottom of your parent layout or below recycler view. Whichever you choose make sure you have the overlay view on top of recycler view.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
...
...
...
<include layout="#layout/view_overlay" />
</RelativeLayout>

Disable response of root view in android

I've got a ListView with several rows. Every row consists of 7 TextViews which react to onClick() events.
This all works perfectly fine but when the user clicks on the margins of the row, where no TextView catches the onClick() event, the root view - a LinearLayout - get's highlighted.
This is normal behaviour of course but as nothing happens by clicking there I don't want the Linear Layout to be highlighted.
Is there any way of disabling this behaviour but keep catching the onClick() events on the TextViews?
(The onClick listener is set inside the adapters getView() method)
Here some extract of the xml file. As one can see I've tried some things but they don't work.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:longClickable="false"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false">
<TextView
android:id="#+id/listelement_weekoverview_tv_mo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:layout_weight="1"
android:background="#drawable/weeklylist_rndrectangle"
android:gravity="center_horizontal"
android:singleLine="false"
android:textColor="#color/appcolor_red_base" />
...
</LinearLayout>
unclicked version
clicked version
I've found a solution.
Simply overwrite the isEnabled (int position) method in your custom Adapter like this:
#Override
public boolean isEnabled (int position) {
return false;
}

Android: How to make Grid view with clickable grid items and nested views (buttons,Image) by remote(For Android TV)

How to make Grid view with click able grid items and nested views (buttons,Image) by remote(For Android TV)
I just want to click able image and button in grid view item should add to play my list if I navigate on button by Remote navigation not by touch
Thank you!!!
How to navigate through d pad in grid view sub items if grid view item have two sub items image button and another button
It is no difference between touch screen or remote control for methods like onClickListener or etc.
For remote control, you need to focus on the element first, than you can click the element.
I have not use the gridView on TV before, but I have used the recyclerView and scrollView. I think they have same situation.
I've try the default simpleAdapter to create the gridView, and it's no problem with the nested views that I can focus and click each grid items after I set the onItemClickListener to the gridView.
I guest your problem is that the grid item conflicts with the button in the grid item.
To avoid this situation, you need to set android:descendantFocusability="blocksDescendants" for the container of your custom grid item view. It will block the items statement in that parent layout.
If you want the button change in the different statement, you can give the button this parameter: android:duplicateParentState="true".
That will let the button follow it's parent's statement.
By the way, don't forget to set some changes to different statement for the grid items (background changes or etc), or you will not know which grid item you have focused :P.
Hope this will help you :)
PS. The following is the sample code for the custom grid item layout. Maybe the example can help you to understand what I mentioned easily.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants" >
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:duplicateParentState="true"/>
</LinearLayout>
2015.01.06 23:11 update:
How to navigate through d pad in grid view sub items if grid view item
have two sub items image button and another button
For this case you should not use OnItemClickListener. Instead, you should make your grid item unfocusable and unclickable, and than set OnClickListener for both two buttons.
You can set the parameters in your customize adapter for the gridView. In getView method you can set each of the grid item view just like convertView.focusable(false) and convertView.clickable(false).
After that, you can set the click listener and give the method you want to do for the buttons in adapter.
Don't care about the D-pad action. Actually, D-pad will work automatically if there are elements can be focused.
The key point for this question is the conflict between grid item and its child elements. You could have just one part to be focused: grid item (the parent view), or the buttons (the child views).

How to disable click listener of button if there is overlay

I am using frame layout and linear layout overlay, when click on overlay so button click listener is also invoked, kindly let me know how to handle this.
<LinearLayout
android:id="#+id/logLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#color/transparentBlack"
android:orientation="vertical" >
<com.valspals.classes.ButtonStyleM700
android:id="#+id/logDone"
android:layout_width="325dp"
android:layout_height="35dp"
android:layout_gravity="center"
android:layout_marginTop="25sp"
android:background="#drawable/btn_confirm_normal"
android:text="#string/iamdone"
android:textColor="#color/White"
android:textSize="#dimen/normalTextSize" />
<com.valspals.classes.ButtonStyleM700
android:id="#+id/woops"
android:layout_width="325dp"
android:layout_height="35dp"
android:layout_gravity="center"
android:layout_marginTop="15sp"
android:layout_marginBottom="25dp"
android:background="#drawable/btn_confirm_normal"
android:text="#string/woops"
android:textColor="#color/White"
android:textSize="#dimen/normalTextSize" />
</LinearLayout>
above this there is frame layout, basically its a parent layout. behind this layout i have two buttons and problem is that they are click able from this linear layout overlay
There are several options you could use for this. The easiest would be setting a global variable and checking it when onClick is called.
i.e. you have a boolean overlayShowing = false;
In your event listener, just check for !overlayShowing
However, that will still allow the button's click animation to show. If you want to prevent that as well, you could intercept the touch events on the top layout (your FrameLayout) and prevent them from being passed to the below layouts if the overlay is visible.
e.g. In your overridden FrameLayout:
#Override
public boolean onInterceptTouchEvent(MotionEvent e){
return overlayShowing;
}
This will intercept the touch event and prevent it from passing it on the chain when the overlay is showing.
If your overlay is only shown by toggling the View's visibility, you could simplify that even further by checking the FrameLayout's visibility in the onInterceptTouchEvent() method. e.g.
#Override
public boolean onInterceptTouchEvent(MotionEvent e){
return getVisibility() == View.VISIBLE;
}

Adding CheckBox to ListView row view makes the row not clickable

I am trying to create a ListView similar to the Gmail app, where each row has a CheckBox on the left to facilitate multiple selection and actions via the contextual action bar, while at the same time, allow clicking each row to transition to another activity.
Originally, before adding the CheckBox the rows would indicate their selection with a light blue background color from the Holo theme I'm using and call onListItemClick in my ListFragment when clicked and onItemLongClick in my OnItemLongClickListener when long clicked.
When I add the CheckBox to my layout .xml file for the row view, the background no longer changes color and I no longer receive click or long click events. If I add android:longClickable="true" to the top ViewGroup in my view .xml then I start getting long click events again. But android:clickable="true" does not get me click events. And neither allows the background blue selection indication to return. Only the CheckBox itself provides visual touch indication with a blue background when touched.
What do I need to do to get the normal visual selection indication as well as click events on the ListView rows with the CheckBox in the view?
Here's the relevant portion of my view .xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:longClickable="true"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingRight="12dp"
android:paddingTop="5dp" >
...
</LinearLayout>
</LinearLayout>
I wrote a blog post on this subject awhile back that you may find helpful. The problem is, in fact, that adding focusable elements (like CheckBox or Button) disables the ability to click the overall list item and their focusability has to be disabled. You should not add any clickable flags to the main layout either.
While adding checkbox in CAB please make it focusable="false", this will solved the problem of only one time called onItemCheckedStateChanged.

Categories

Resources