Why RecyclerView is not working setBackground function? - scroll

I tried many Times for setting backgroundColor on RecyclerView. but i try to scroll then background was removed.So I can fix backgroundColor in RecyclerView. Help me please.
Or I Want to change ForegroundColor.
My Issue Video
https://www.youtube.com/watch?v=C29qhPb44FE
I don't know the reason...

You need to first understand how RecyclerView works.
As you scroll through the cells, the views that gets out of the screen will be RECYCLED, and they will subsequently be reused to display the incoming views. Hence the name RecyclerView. This way, views will always be recycled and reused, thus saving memory.
What you need to do is something like this:
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
//mList and mSelectedObjects are array lists
View yourView = holder.itemView.findViewById(R.id.your_view);
Object object = mList.get(position);
yourView.setTag(object);
yourView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Object object = (Object) v.getTag();
if (mSelectedObjects.contains(object)) {
mSelectedObjects.remove(object);
v.setBackground(null);
} else {
mSelectedObjects.add(object);
v.setBackgroundColor(Color.GRAY);
}
}
});
}

If you set programmatically the background color. You have to set every time the normal color and the selected color.
RecyclerViews are reusing their views. When an item leaves the screen it will be reused to increase the performance of the recycler view.
In this case when one set programmatically a background color and the item leaves the screen. It will be reused in the new item and the background color is still the same as when the item has left the screen.

Related

Unity 3D: Changing sprite animation using UI button

I'm following this tutorial on youtube about changing sprite animation in code, and I was wondering if I could change this to changing sprite animation using UI button. does anyone knows how to do this. Thank you!
EDIT:
The script that I reposed kind of works thanks to your help, it changes the sprite image from image one to image two but what I'm basically trying to achieve is that each time that I click the UI button the sprite image will change from sprite image one (UI button click)> sprite image two (UI button click)> sprite image three (UI button click)> then repeat the process instead of the sprite image automatically changing itself.
Buttons have an OnClick event http://docs.unity3d.com/ScriptReference/UI.Button-onClick.html
You just create a method that gets called when the button is clicked, in your case the changing sprite code. Seen as you are using a timer though you will need to use something like a bool because onClick() only gets called once when clicked, not every frame.
Look https://www.youtube.com/watch?v=J5ZNuM6K27E
bool b_RunSpriteAnim;
public void onClick(){
b_RunSpriteAnim = true;
}
void Update(){
if (b_RunSpriteAnim)
//your anim sprite stuff
}
Then once the sprite anim has finished, just toggle b_RunSpriteAnim to false and reset the timer.
Edited:
You don't need a boolean. I only thought you wanted it because you were using a timer (as based on the Youtube link). If you just want to change the sprite immediately then you do not need it. As for Imagethree not working, it's because you have never included it in your code. It isn't clear what you are trying to achieve with Imagethree, if you included this in onClick as well it would just overwrite the image two that was just set, so I am not sure what you are looking to achieve.
public void onClick(){
this.gameObject.GetComponent<SpriteRenderer>().sprite = Imagetwo;
}
Second Edit:
public Sprite[] Images;
//Index starts at one because we are setting the first sprite in Start() method
private int _Index = 1;
void Start(){
//Set the image to the first one
this.gameObject.GetComponent<SpriteRenderer>().sprite = Images[0];
}
public void onClick(){
//Reset back to 0 so it can loop again if the last sprite has been shown
if (_Index >= Images.Length)
_Index = 0;
//Set the image to array at element index, then increment
this.gameObject.GetComponent<SpriteRenderer>().sprite = Images[_Index++];
}

WP7 - how to let user set app theme

My app has 10 pages and all of them have black background. I want to let user change background color of all pages using radioButton in my app. How can I do this in easiest way?
Go through these blogs
1. Theme Forcing for Windows Phone 7 or,
2. Windows Phone Mango Custom application Theme
these might prove helpful to you. You can study these and modify to put them in your settings page.
Thank You :)
Ok so you have 10 pages and on each page you want to change the background colour of those pages through the settings menu. What you can do is use the Windows Phone IsolatedStorageSettings.
Firstly you want to initialize the IsolatedStorageSettings. You can do that like this:
IsolatedStorageSettings MyAppSettings = IsolatedStorageSettings.ApplicationSettings;
Then you will have to set a default value for it so it doesn't throw an exception. You can do this:
MyAppSettings.Add("PageBackgroundColor", "#000000"); // you can set whatever the default colour you want here. i.e. Black
the best place i think would be is to add this code in:
private void Application_Launching(object sender, LaunchingEventArgs e)
{
if (IsolatedStorageSettings.ApplicationSettings.Contains("PageBackgroundColor"))
{
// Don't do anything because you've already set the default background colour for the pages
}
else
{
// add the default color
}
}
Now in your MainPage you can reinitialize the IsolatedStorageSettings. Once you've done that you will want to get the value of the setting and depending on the value you will want to change the background color. To read the value:
string Sortval = (string)MyAppSettings["PageBackgroundColor"];
You can add this in the:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
Or
public MainPage
{
InitializeComponent();
}
Remember that public MainPage will only run once and the OnNavigatedTo runs every time the page is loaded so if you want to update the background color right after adding the setting, OnNavigatedTo is the way to go but if you want to apply the changes after a restart, public Mainpage is it.
Now to read the value and change it you want to do something like:
string val = (string)MyAppSettings["PageBackgroundColor"];
if (val == "#000000")
{
//change to black
}
else if (val == "your hex color")
{
//change to whatever color
}
else if (val == "another hex color")
{
//...
}
Now to save the value you want to reinitialize the IsolatedStorageSettings in your settings page and to save the values it would be something like this:
MyAppSettings.Remove("PageBackgroundColor");
MyAppSettings.Add("PageBackgroundColor", "your hex color");
MyAppSettings.Save();
This is untested however It should give you the very basic idea on how to do it in terms of saving and loading setting and then applying it

Page navigation - keeping data on previous page

In a windows phone app,a page is navigated to another page and on pressing back button, goes back to previous page.Now in previous page the previous data has to be displayed. But its not displaying immediately and takes some time to load. How to solve this issue?
Overall the question is how to maintain the content of page(containing dynamic data) displayed in back navigation?
Use PhoneApplicationService class to keep the data while you navigate between the pages. Here is some samples. Actually, it's very easy:
protected override void OnNavigatedFrom(NavigationEventArgs args)
{
if (ContentPanel.Background is SolidColorBrush)
{
Color clr = (ContentPanel.Background as SolidColorBrush).Color;
if (args.Content is MainPage) (args.Content as MainPage).ReturnedColor = clr;
// save color
PhoneApplicationService.Current.State["Color"] = clr;
}
base.OnNavigatedFrom(args);
}
protected override void OnNavigatedTo(NavigationEventArgs args)
{
// restore color
if (PhoneApplicationService.Current.State.ContainsKey("Color"))
{
Color clr = (Color)PhoneApplicationService.Current.State["Color"];
ContentPanel.Background = new SolidColorBrush(clr);
}
base.OnNavigatedTo(args);
}
How to preserve and restore page state for Windows Phone
You can use single ton object of View Model to store data in a page provided all the controls are bind to a property in View Model.
Then if you do not clear the values of the controls when navigating away from the page, the data will be displayed in the page when you navigate back to that screen provided all the controls

JavaFX: Focusing textfield programmatically

I wrote an application with JavaFX which will only be usable with keyboard's arrows.
So I prevented MouseEvent on Scene's stage, and I "listen" to KeyEvents.
I also switched off focusability of all nodes :
for(Node n : children) {
n.setFocusTraversable(false);
Now I have some textfields, checkboxes, and buttons.
I would like to change the state of my input controls (textfield, checkbox,..) programatically: for example I would like to enter the textfield to edit the content programatically.
So my question is: how to enter in a non-focus-traversable textfield?
Because textfield.requestFocus(); doesn't work anymore since I set false to focustraversable property of my textfield.
Thanks
By
n.setFocusTraversable(false);
the node is made non-focus-traversable instead of non-focusable. It can still be focused for example by mouse or programmatically. Since you prevented mouse events, here the other option:
Platform.runLater(new Runnable() {
#Override
public void run() {
textfield.requestFocus();
}
});
Scene scene = new Scene(root);
EDIT: as per comment,
The javadoc of requestFocus states:
... To be eligible to receive the focus, the node must be part of a scene,
it and all of its ancestors must be visible, and it must not be
disabled. ...
So this method should be called after construction of scene graph as follow:
Scene scene = new Scene(root);
textfield.requestFocus();
However, Platform.runLater in the above will run at the end, after the main method start(), which ensures the call of requestFocus will be after scene graph cosntruction.
There maybe other reasons depending on the requestFocus implementation code.
set the .requestFocus(); on initialize method to fire on .fxml file loading controller
#Override
public void initialize(URL url, ResourceBundle rb) {
/* the field defined on .fxml document
#FXML
private TextField txtYear;
*/
txtYear.requestFocus();
}

Group of buttons?

I have a question - is there any way to define a few buttons as a 1 group, and make 1 animation that will start for this group, instead of making an separate animation start for each of them?
Put them all in a ViewGroup and animate that. All children will be part of the animation. A ViewGroup is the base class for any layout class, that means you can use a LinearLayout, RelativeLayout or anything else that you find suitable.
You can try to set to all buttons same onTouchListener, like:
button1.setOnTouchListener(this);
button2.setOnTouchListener(this);
button3.setOnTouchListener(this);
Of course your app needs to implement onTouchListener, and then you can write your onTouchListener:
public void onClick(View v) {
// write your code what you want your buttons to do
// You can also write some different codes for buttons by using id
if(v.getId() == button1.getId())
{
//do something when button1 clicked...
}
}

Resources