小编use*_*977的帖子

从RecyclerView中删除所有项目

我试图从我RecyclerViewonRestart方法中删除所有元素,因此项目不会被加载两次:

@Override
protected void onRestart() {
    super.onRestart();

    // first clear the recycler view so items are not populated twice
    for (int i = 0; i < recyclerAdapter.getSize(); i++) {
        recyclerAdapter.delete(i);
    }

    // then reload the data
    PostCall doPostCall = new PostCall(); // my AsyncTask... 
    doPostCall.execute();
}
Run Code Online (Sandbox Code Playgroud)

但由于某种原因,delete我在其中创建的方法adapter无法正常运行:

在RecyclerAdapter.java中:

public void delete(int position){
    myList.remove(position);
    notifyItemRemoved(position);
}

public int getSize(){
    return myList.size();
}
Run Code Online (Sandbox Code Playgroud)

我认为列表中的每个其他项都会被删除而不是整个列表.

有一个listview它很容易,我只是打电话adapter.clear().

有人可以帮我修改一下代码吗?

我想我应该用,notifyItemRangeRemoved(...,...);但我不确定如何.TIA

android adapter android-recyclerview

41
推荐指数
4
解决办法
9万
查看次数

创建样式-v21.xml

在Android Studio中,我导入了一个不包含的项目styles.xml (v21).所以我styles-v21.xmlvalues目录中创建了一个文件.双方styles.xmlstyles-v21.xml都在values目录现在.

styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="AppTheme" parent="AppTheme.Base">
        <!-- Customize your theme here. -->
    </style>

    <style name="AppTheme.Base" parent="Theme.AppCompat.Light">

    </style>

</resources>
Run Code Online (Sandbox Code Playgroud)

款式 - v21.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="android:Theme.Material.Light.DarkActionBar">

    </style>

</resources>
Run Code Online (Sandbox Code Playgroud)

当我构建项目时,我得到一个构建错误:

错误:错误:资源重复:C:\ xxx\main\res\values\styles-v21.xml:style/AppTheme,C:\ xxx\main\res\values\styles.xml:style/AppTheme

错误是两个资源文件具有相同的style名称:AppTheme.但在我看过的其他项目和教程中,这些styles都有相同的名称.一个用于Android,兼容版本21,另一个用于Android,没有兼容性.

我想我想知道我是否应该简单地更改名称或者这些文件是否应该具有相同的名称 - 这意味着手头有更大的问题.我该怎么办?

xml resources android android-studio android-styles

34
推荐指数
2
解决办法
4万
查看次数

tkinter:方法后如何使用

嘿,我是python的新手,我正在使用tkinter为我的gui.我在使用"after"方法时遇到了麻烦.目标是每5秒钟出现一个随机字母.

这是我的代码:

import random
import time
from tkinter import *


root = Tk()

w = Label(root, text="GAME")
w.pack()

frame = Frame(root, width=300, height=300)
frame.pack()

L1 = Label(root, text="User Name")
L1.pack(side=LEFT)
E1 = Entry(root, bd =5)
E1.pack(side=LEFT)


tiles_letter = ['a', 'b', 'c', 'd', 'e']


while len(tiles_letter) > 0:
    rand = random.choice(tiles_letter)
    tile_frame = Label(frame, text=rand)
    tile_frame.pack()
    frame.after(500)
    tiles_letter.remove(rand)  # remove that tile from list of tiles

root.mainloop()
Run Code Online (Sandbox Code Playgroud)

有人可以帮助我 - 问题肯定是frame.after(500):我不确定使用"frame"是否正确,我不知道500后面的参数是什么.

谢谢

python user-interface tkinter pycharm

27
推荐指数
1
解决办法
10万
查看次数

将EditText添加到工具栏

ToolbarEditText这个样子:

在此输入图像描述

我尝试添加它作为一个ActionView但我得到了这个结果:

在此输入图像描述

它基本上只是添加了我的单词'Tap Below'作为标题而没有 TextView

这是我做的:

menu.xml文件

<menu>
    ... 
    ...
    <item
        android:id="@+id/edit_text_menu"
        android:orderInCategory="300"
        android:title="Tap Below"
        android:actionLayout="@layout/edit_text_layout"
        app:showAsAction="ifRoom|collapseActionView"
        />
</menu>
Run Code Online (Sandbox Code Playgroud)

edit_text_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/myEditText"
    android:background="#000000">

</EditText>
Run Code Online (Sandbox Code Playgroud)

我想通过这样做我将我的EditText布局添加到Toolbar...有人可以帮忙吗?

或者,有一个技巧,我可以覆盖EditTextToolbar

我尝试添加,<item name="windowActionModeOverlay">true</item>但它没有做任何事情.

android toolbar android-custom-view android-edittext

17
推荐指数
2
解决办法
4万
查看次数

更改按钮颜色onClick

我希望Button每次点击它都会改变颜色.但它只会在第一次点击时改变颜色.

我相信问题在于setColor功能.每次我点击时Button,count都会设置为1.所以即使我将其设置为0,它也会在下次点击时重置为1.我该如何解决?javascript/html中是否有全局变量可以轻松解决?

<!DOCTYPE html>
<html>
<head>

<script>
function setColor(btn, color){
    var count=1;
    var property = document.getElementById(btn);
    if (count == 0){
        property.style.backgroundColor = "#FFFFFF"
        count=1;        
    }
    else{
        property.style.backgroundColor = "#7FFF00"
        count=0;
    }

}
</script>
</head>

<body>

<input type="button" id="button" value = "button" style= "color:white" onclick="setColor('button', '#101010')";/>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

html javascript global onclick button

11
推荐指数
2
解决办法
13万
查看次数

semantic ui在下拉列表中反应默认选定的选项

我希望我的默认选择选项dropdown.当我添加所选选项但不使用默认选定选项渲染时,下面的代码有效:

render() {
        return (
            <Form onSubmit={this.handleFormSubmit}>
                <Form.Dropdown
                  placeholder="Select Options"
                  fluid multiple selection
                  options={this.state.options}
                  onChange={this.handleMultiChange}
                />
                <Button type="submit">Submit</Button>
            </Form>
        );
    }
Run Code Online (Sandbox Code Playgroud)

我尝试添加defaultSelectedLabel={this.state.selected}.

this.state.selected 是一个选项数组,默认情况下所选的值为true:

render() {
        return (
            <Form onSubmit={this.handleFormSubmit}>
                <Form.Dropdown
                  placeholder="Select Options"
                  fluid multiple selection
                  options={this.state.options}
                  onChange={this.handleMultiChange}
                  defaultSelectedLabel={this.state.selected}
                />
                <Button type="submit">Submit</Button>
            </Form>
        );
    }
Run Code Online (Sandbox Code Playgroud)

但我得到以下警告:

Warning: Failed prop type: Invalid prop defaultSelectedLabel supplied to Dropdown.

我对defaultValue道具做了同样的事情但得到了同样的错误

如何获取默认选项dropdown

javascript reactjs semantic-ui semantic-ui-react

9
推荐指数
1
解决办法
1万
查看次数

在与ES6深度嵌套的javascript对象数组中查找值

在对象的数组,我需要找到一个value-这里keyactivity:但是,activity key可以深度嵌套像这样的数组:

const activityItems = [
    {
        name: 'Sunday',
        items: [
            {
                name: 'Gym',
                activity: 'weights',
            },
        ],
    },
    {
        name: 'Monday',
        items: [
            {
                name: 'Track',
                activity: 'race',
            },
            {
                name: 'Work',
                activity: 'meeting',
            },
            {
                name: 'Swim',
                items: [
                    {
                        name: 'Beach',
                        activity: 'scuba diving',
                    },
                    {
                        name: 'Pool',
                        activity: 'back stroke',
                    },
                ],
            },
        ],    
    },
    {} ...
    {} ...
];
Run Code Online (Sandbox Code Playgroud)

所以我编写了一个递归算法来查明某个活动是否在数组中:

let match = false;
const findMatchRecursion = …
Run Code Online (Sandbox Code Playgroud)

javascript arrays algorithm es6-modules

7
推荐指数
1
解决办法
7845
查看次数

处理触摸事件 - onInterceptTouchEvent和onTouchEvent

我希望能够在屏幕上的任何位置滑动以调用某个功能.但我也有Buttons,Linear Layouts因为我希望能够点击.如果我刷上Button我想 onInterceptTouchEventIntercept在调用ButtononTouchEvent和进行刷卡操作.如果我只是点击一个Button我不希望onInterceptTouchEvent被调用.相反,我希望ButtononTouchEvent要调用和执行Button click

但是当我尝试实现时,我会遇到错误onInterceptTouchEvent.

这是我的代码:

public class Game extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.game_activity);

   //other code....
}
}

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            break;
        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_UP:
            swipeScreen(); //if action recognized as swipe then swipe
            break;
        case MotionEvent.ACTION_MOVE:
            float x = event.getX();
            float …
Run Code Online (Sandbox Code Playgroud)

android onclick touch touch-event

6
推荐指数
1
解决办法
1万
查看次数

Cors,无法加载Fetch API

在我的React应用程序中,我试图GET从我的heroku服务器发出请求以获取用户列表:

https://blablabla.heroku.com/users
Run Code Online (Sandbox Code Playgroud)

使用以下请求:

const sendSearch = fetch('/https://blablabla.heroku.com/users', {credentials: 'same-origin'})

function loadMyUsers(data) {
  data.json().then((jsonData) => {
    // console.log(jsonData)

  })
}

sendSearch.then(loadMyUsers)}
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误:

Fetch API cannot load https://blablabla.heroku.com/users.
Response to preflight request doesn't pass access control check: No
'Access-Control-Allow-Origin' header is present on the requested
resource. Origin 'http://localhost:8080' is therefore not allowed
access. If an opaque response serves your needs, set the request's mode
to 'no-cors' to fetch the resource with CORS disabled.
Run Code Online (Sandbox Code Playgroud)

我尝试将我的Fetch方法更改为类似于以下内容的许多内容:

const sendSearch = fetch('https://blablabla.heroku.com/users',{ …
Run Code Online (Sandbox Code Playgroud)

get fetch cors reactjs

5
推荐指数
1
解决办法
7632
查看次数

window.onpopstate - 我需要删除这个事件处理程序吗?

我正在使用window.onpopstate事件处理程序来侦听后退和前进浏览器按钮事件。我的代码看起来像这样:

componentDidMount() {
    window.onpopstate = this.onBackOrForwardButtonEvent;
}

onBackOrForwardButtonEvent = (e) => {
    e.preventDefault();
    log.info('back or forward button pressed');
    if (this.props.route.path !== '/app') {
       // ... do something
    }
};
Run Code Online (Sandbox Code Playgroud)

我的问题是:我是否需要删除此事件侦听器 - 也许在componentWillUnmount

就像是:

componentWillUnmount() {
    window.removeEventListener('onpopstate', this.onBackOrForwardButtonEvent, false)
}
Run Code Online (Sandbox Code Playgroud)

我见过onPopState像上面那样使用的示例,但从未在组件卸载时删除侦听器。

示例:https : //github.com/ReactTraining/react-router/issues/967

browser event-handling reactjs react-router

5
推荐指数
1
解决办法
8670
查看次数