Android:如何在onResume中的CardView上设置setEnabled(true)? [英] Android: how do I setEnabled(true) on a CardView in onResume?

查看:111
本文介绍了Android:如何在onResume中的CardView上设置setEnabled(true)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有CardViews的RecyclerView列表.我在下面添加了以下代码以启动一个活动(ActActivity),该活动允许用户编辑CardView.如果用户在CardView上快速连续单击多次,则setEnabled(false)代码用于防止打开活动的多个实例.我只希望一次打开一个活动实例,以便用户仅编辑他们单击的单个CardView.

I have a RecyclerView list with CardViews. I added the following code below to launch an activity (ActActivity) that allows the user to edit the CardView. The setEnabled(false) code is used to keep multiple instances of the activity from opening if the user clicks multiple times in rapid succession on the CardView. I only want one instance of the activity to be open at one time so that the user is only editing the single CardView that they clicked on.

我的问题是,当我添加onResume()部分以将setEnabled()重置为"true"时,应用程序崩溃.当我删除onResume()部分时,setEnabled(false)代码通过不允许打开活动的多个实例来正常工作,但是问题是,对CardView的任何双击都会禁用将来的单击以正确启动ActActivity.

My problem is that when I add the onResume() section to re-set setEnabled() to "true" the app crashes. When I remove the onResume() section then the setEnabled(false) code works correctly by not allowing multiple instances of the activity to open, but the problem is that any doubleclicks on the CardView disable a future single-click to correctly launch the ActActivity.

我在这里想念什么?

MainActivity.java

public class MainActivity extends AppCompatActivity implements
    RecyclerItemClickListener {

    lvContact = (RecyclerView) findViewById(R.id.lvContact);
    assert lvContact != null;
    lvContact.setHasFixedSize(true);

    contactListAdapter = new ContactListAdapter(this);
    contactListAdapter.setOnItemClickListener(this);

    LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
    lvContact.setLayoutManager(layoutManager);
    lvContact.setAdapter(contactListAdapter);  
...
@Override
public void onItemClick(int position, View view) {
    CardView c = (CardView) view;
    c.setEnabled(false);
    ActActivity.start(this, contactListAdapter.getItem(position));
}

...
Override
protected void onResume() {
    super.onResume();

    CardView cardView1 = (CardView) findViewById(R.id.singlecard_view1);
    cardView1.setEnabled(true);
}

RecyclerView的

xml文件:

xml file for the RecyclerView:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
tools:context="com.v050.MainActivity">

<include
    android:id="@+id/toolbar"
    layout="@layout/toolbar" >
</include>

<LinearLayout
    android:id="@+id/todoListLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/toolbar"
    android:layout_above="@+id/s4"
    android:background="@color/background4main"
    android:layout_marginTop="6dp"
    android:layout_marginStart="6dp"
    android:layout_marginLeft="6dp"
    android:layout_marginEnd="6dp"
    android:layout_marginRight="6dp"
    android:layout_marginBottom="6dp"
    android:orientation="vertical"  >

<android.support.v7.widget.RecyclerView
    android:id="@+id/lvContact"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical"  />

</LinearLayout>

<TextView
    android:id="@+id/s4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:gravity="center"
    android:textStyle="bold"
    android:textColor="#FFFFFF"
    android:background="@color/colorPrimary"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:paddingTop="15dp"
    android:paddingBottom="15dp"
    android:clickable="true"  />

</RelativeLayout>

CardView的

xml文件:

xml file for the CardView:

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/singlecard_view1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="4dp"
    android:layout_marginBottom="4dp"
    card_view:cardBackgroundColor="@android:color/white"
    card_view:cardCornerRadius="6dp"
    card_view:cardElevation="4dp"
    android:foreground="?android:attr/selectableItemBackground"
    android:longClickable="true"  >

Logcat输出不喜欢onResume()部分中的"cardView1.setEnabled(true)"行:

Logcat output does not like the "cardView1.setEnabled(true)" line in the onResume() section:

11-01 23:22:54.814 1399-1399/com.example.v50 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                   java.lang.RuntimeException: Unable to resume activity {com.example.v50/com.v050.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2575)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2603)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2089)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.v050.MainActivity.onResume(MainActivity.java:279)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1184)
at android.app.Activity.performResume(Activity.java:5082)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2565)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2603) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2089) 
at android.app.ActivityThread.access$600(ActivityThread.java:130) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:4745) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
at dalvik.system.NativeStart.main(Native Method) 

一个可行的建议答案是:

One proposed answer that works is:

...     
@Override
public void onItemClick(int position, final View view) {
    view.setEnabled(false);
    ActActivity.start(this, contactListAdapter.getItem(position));
    view.post(new Runnable() {
        @Override
        public void run() {
            view.setEnabled(true);
        }
    });
 }

这与使用onResume的答案相比如何?

How does this compare to answers that use onResume?

推荐答案

这是问题.....

CardViewrecycle view中,您正在尝试从main layout而不是item layout....

CardView is in the recycle view and you are trying from the main layout instead of item layout ....

这是您解决问题的方法....

here is your solution for your problem....

cardview变量创建为global .......

create cardview variable as global .......

private CardView cardview;

item clicked ......

@Override
public void onItemClick(int position, View view) {
    cardview = (CardView) view;
    cardview.setEnabled(false);
    ActActivity.start(this, contactListAdapter.getItem(position));
}

注意:-始终尝试声明已在许多方法中使用过的全局变量.... .

-,尝试在您的onResume ...中首次运行.

- try this for the first run in your onResume....

并将条件放在Resume ...上.

and put condition onResume ....

 Override
    protected void onResume() {
        super.onResume();
        if(cardview!=null){
        cardview.setEnabled(true);

     }

以上问题将解决您的首次运行"问题....

这篇关于Android:如何在onResume中的CardView上设置setEnabled(true)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆