拖拽时,存根中的列表视图会变黑。

发布于 2024-12-11 14:36:00 字数 12781 浏览 0 评论 0原文

我正在制作一个应用程序,底部有一堆按钮,顶部有一堆 ViewStub。所有按钮都指向一个侦听器,并且该侦听器在按钮 id 之间切换以决定要扩充哪个存根。 因此,如果单击按钮 1,则先前的所有存根加载都将被撤消,并且存根 1 将会膨胀。

现在,在存根 4 上,我需要一个列表视图,但拖动列表时似乎出现问题。

我遇到的问题是,当我拖动列表视图时,它会变黑。

这是代码: 包 com.me.myapp; MainActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewStub;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends Activity {
    int stubNum;

    /** Called when the activity is first created. */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        if (savedInstanceState  == null) {
            doStuff((View) findViewById(R.id.button1));
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putInt("stubNum", stubNum);
        Log.d("Jason", "Saving stubNum is " + stubNum);
        // TODO Auto-generated method stub
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onRestoreInstanceState(savedInstanceState);
        stubNum = savedInstanceState.getInt("stubNum");
        Log.d("Jason", "Saved stubNum is " + stubNum);
        if (findViewById(stubNum) != null)
        {
            ViewStub stub = (ViewStub) findViewById(stubNum);
            View newView = stub.inflate();
            Button button1 = (Button) findViewById(R.id.Button01);
            button1.setVisibility(Button.INVISIBLE);

            Button button2 = (Button) findViewById(R.id.Button02);
            button2.setVisibility(Button.VISIBLE);
        }
    }

    // Implement the OnClickListener callback
    public void doStuff(View v) {
        setContentView(R.layout.main);
        switch (v.getId()) {
            case R.id.button1:
                try
                {
                    stubNum = 1;
                    ViewStub stub = (ViewStub) findViewById(R.id.stub1);
                    if (!(stub.getVisibility() == ViewStub.VISIBLE)) {
                        View newView = stub.inflate();
                        Button button1 = (Button) findViewById(R.id.Button01);
                        button1.setVisibility(Button.INVISIBLE);

                        Button button2 = (Button) findViewById(R.id.Button02);
                        button2.setVisibility(Button.VISIBLE);
                    }
                    stubNum = stub.getId();
                    Log.d("Jason", "stubNum is " + stubNum);
                }
                catch(NullPointerException e)
                {
                    e.printStackTrace();
                }
                break;
            case R.id.button2:
                try
                {
                    stubNum = 2;
                    ViewStub stub = (ViewStub) findViewById(R.id.stub2);
                    if (!(stub.getVisibility() == ViewStub.VISIBLE)) {
                        View newView = stub.inflate();
                        Button button1 = (Button) findViewById(R.id.Button01);
                        button1.setVisibility(Button.INVISIBLE);

                        Button button2 = (Button) findViewById(R.id.Button02);
                        button2.setVisibility(Button.INVISIBLE);
                    }
                    stubNum = stub.getId();
                    Log.d("Jason", "stubNum is " + stubNum);
                }
                catch(NullPointerException e)
                {
                    e.printStackTrace();
                }
                break;
            case R.id.button3:
                try
                {
                    stubNum = 3;
                    ViewStub stub = (ViewStub) findViewById(R.id.stub3);
                    if (!(stub.getVisibility() == ViewStub.VISIBLE)) {
                        View newView = stub.inflate();
                        Button button1 = (Button) findViewById(R.id.Button01);
                        button1.setVisibility(Button.INVISIBLE);

                        Button button2 = (Button) findViewById(R.id.Button02);
                        button2.setVisibility(Button.INVISIBLE);
                    }
                    stubNum = stub.getId();
                    Log.d("Jason", "stubNum is " + stubNum);
                }
                catch(NullPointerException e)
                {
                    e.printStackTrace();
                }
                break;
            case R.id.button4:
                try
                {
                    stubNum = 4;
                    ViewStub stub = (ViewStub) findViewById(R.id.stub4);            
                    if (!(stub.getVisibility() == ViewStub.VISIBLE)) {
                        View newView = stub.inflate();
                        Button button1 = (Button) findViewById(R.id.Button01);
                        button1.setVisibility(Button.INVISIBLE);

                        Button button2 = (Button) findViewById(R.id.Button02);
                        button2.setVisibility(Button.INVISIBLE);
                    }
                    stubNum = stub.getId();
                    Log.d("Jason", "stubNum is " + stubNum);


                }
                catch(NullPointerException e)
                {
                    e.printStackTrace();
                }
                break;
            case R.id.button5:
                try
                {
                    stubNum = 5;
                    ViewStub stub = (ViewStub) findViewById(R.id.stub5);
                    if (!(stub.getVisibility() == ViewStub.VISIBLE)) {
                        View newView = stub.inflate();
                        Button button1 = (Button) findViewById(R.id.Button01);
                        button1.setVisibility(Button.INVISIBLE);

                        Button button2 = (Button) findViewById(R.id.Button02);
                        button2.setVisibility(Button.INVISIBLE);
                    }
                    stubNum = stub.getId();
                    Log.d("Jason", "stubNum is " + stubNum);
                }
                catch(NullPointerException e)
                {
                    e.printStackTrace();
                }
                break;
            default:
                stubNum = 1;
                ViewStub stub = (ViewStub) findViewById(R.id.stub1);
                if (!(stub.getVisibility() == ViewStub.VISIBLE)) {
                    View newView = stub.inflate();
                }
                break;
        }
    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout android:layout_height="wrap_content"
        android:id="@+id/linearLayout1" android:layout_width="match_parent"
        android:layout_alignParentLeft="true"
        android:gravity="center" android:layout_alignParentBottom="true">
        <ImageButton android:src="@drawable/download" android:id="@+id/button1" android:layout_width="60dp" android:layout_height="40dp" android:layout_margin="2dip" android:onClick="doStuff"></ImageButton>
        <ImageButton android:src="@drawable/download" android:id="@+id/button2" android:layout_width="60dp" android:layout_height="40dp" android:layout_margin="2dip" android:onClick="doStuff"></ImageButton>
        <ImageButton android:src="@drawable/download" android:id="@+id/button3" android:layout_width="60dp" android:layout_height="40dp" android:layout_margin="2dip" android:onClick="doStuff"></ImageButton>
        <ImageButton android:src="@drawable/download" android:id="@+id/button4" android:layout_width="60dp" android:layout_height="40dp" android:layout_margin="2dip" android:onClick="doStuff"></ImageButton>
        <ImageButton android:src="@drawable/download" android:id="@+id/button5" android:layout_width="60dp" android:layout_height="40dp" android:layout_margin="2dip" android:onClick="doStuff"></ImageButton>
    </LinearLayout>
    <RelativeLayout android:id="@+id/relativeLayout2"
        android:layout_height="wrap_content" android:layout_width="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true" android:layout_above="@+id/linearLayout1"
        android:background="#ffffff" android:layout_below="@+id/relativeLayout3">
        <ViewStub android:id="@+id/stub1"
               android:inflatedId="@+id/subTree"
               android:layout="@layout/myvarsity"
               android:layout_width="fill_parent"
               android:layout_height="fill_parent" />

        <ViewStub android:id="@+id/stub2"
               android:inflatedId="@+id/subTree"
               android:layout="@layout/livestream"
               android:layout_width="fill_parent"
               android:layout_height="fill_parent" />

        <ViewStub android:id="@+id/stub3"
               android:inflatedId="@+id/subTree"
               android:layout="@layout/shows"
               android:layout_width="fill_parent"
               android:layout_height="fill_parent" />

        <ViewStub android:id="@+id/stub4"
               android:inflatedId="@+id/subTree"
               android:layout="@layout/more"
               android:layout_width="fill_parent"
               android:layout_height="fill_parent" />

        <ViewStub android:id="@+id/stub5"
               android:inflatedId="@+id/subTree"
               android:layout="@layout/settings"
               android:layout_width="fill_parent"
               android:layout_height="fill_parent" />
    </RelativeLayout>
    <RelativeLayout android:id="@+id/relativeLayout3" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_width="match_parent" android:gravity="center_horizontal">
            <Button android:id="@+id/Button02" android:text="Customize" android:layout_height="40dp" android:layout_width="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentRight="true"></Button>
            <Button android:id="@+id/Button01" android:text="Back" android:layout_height="40dp" android:layout_width="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentLeft="true"></Button>
            <TextView android:id="@+id/textView1" android:text="App" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:layout_width="wrap_content" android:layout_centerVertical="true" android:layout_centerHorizontal="true"></TextView>
        </RelativeLayout>
</RelativeLayout>

more.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:gravity="center_horizontal">
    <RelativeLayout android:id="@+id/relativeLayout2"
        android:layout_height="wrap_content" android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" android:layout_width="match_parent">
        <ListView android:id="@+id/moreList"
            android:layout_centerVertical="true" android:layout_width="match_parent"
            android:layout_alignParentLeft="true" android:layout_height="match_parent"></ListView>
    </RelativeLayout>
    <TextView android:id="@+id/textView1" android:textColor="#000000"
        android:text="Morelistview" android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="wrap_content" android:layout_below="@+id/relativeLayout2"
        android:layout_alignParentLeft="true"></TextView>
</RelativeLayout>

more_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    <TextView android:text="" android:id="@+id/moreItem" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>

</RelativeLayout>

I'm making an app that has a bunch of buttons at the bottom and a bunch of ViewStubs at the top. All the buttons point to one listener and that listener switches between the buttons id to decide on which stub to inflate.
So if button 1 is clicked, any previous stub loading is undone and stub 1 is inflated.

Now, on stub 4, I need a list view but there seems to be a problem when I drag the list.

The issue I'm having is that the list view goes black when I drag it.

Here is the code:
package com.me.myapp;
MainActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewStub;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends Activity {
    int stubNum;

    /** Called when the activity is first created. */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        if (savedInstanceState  == null) {
            doStuff((View) findViewById(R.id.button1));
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putInt("stubNum", stubNum);
        Log.d("Jason", "Saving stubNum is " + stubNum);
        // TODO Auto-generated method stub
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onRestoreInstanceState(savedInstanceState);
        stubNum = savedInstanceState.getInt("stubNum");
        Log.d("Jason", "Saved stubNum is " + stubNum);
        if (findViewById(stubNum) != null)
        {
            ViewStub stub = (ViewStub) findViewById(stubNum);
            View newView = stub.inflate();
            Button button1 = (Button) findViewById(R.id.Button01);
            button1.setVisibility(Button.INVISIBLE);

            Button button2 = (Button) findViewById(R.id.Button02);
            button2.setVisibility(Button.VISIBLE);
        }
    }

    // Implement the OnClickListener callback
    public void doStuff(View v) {
        setContentView(R.layout.main);
        switch (v.getId()) {
            case R.id.button1:
                try
                {
                    stubNum = 1;
                    ViewStub stub = (ViewStub) findViewById(R.id.stub1);
                    if (!(stub.getVisibility() == ViewStub.VISIBLE)) {
                        View newView = stub.inflate();
                        Button button1 = (Button) findViewById(R.id.Button01);
                        button1.setVisibility(Button.INVISIBLE);

                        Button button2 = (Button) findViewById(R.id.Button02);
                        button2.setVisibility(Button.VISIBLE);
                    }
                    stubNum = stub.getId();
                    Log.d("Jason", "stubNum is " + stubNum);
                }
                catch(NullPointerException e)
                {
                    e.printStackTrace();
                }
                break;
            case R.id.button2:
                try
                {
                    stubNum = 2;
                    ViewStub stub = (ViewStub) findViewById(R.id.stub2);
                    if (!(stub.getVisibility() == ViewStub.VISIBLE)) {
                        View newView = stub.inflate();
                        Button button1 = (Button) findViewById(R.id.Button01);
                        button1.setVisibility(Button.INVISIBLE);

                        Button button2 = (Button) findViewById(R.id.Button02);
                        button2.setVisibility(Button.INVISIBLE);
                    }
                    stubNum = stub.getId();
                    Log.d("Jason", "stubNum is " + stubNum);
                }
                catch(NullPointerException e)
                {
                    e.printStackTrace();
                }
                break;
            case R.id.button3:
                try
                {
                    stubNum = 3;
                    ViewStub stub = (ViewStub) findViewById(R.id.stub3);
                    if (!(stub.getVisibility() == ViewStub.VISIBLE)) {
                        View newView = stub.inflate();
                        Button button1 = (Button) findViewById(R.id.Button01);
                        button1.setVisibility(Button.INVISIBLE);

                        Button button2 = (Button) findViewById(R.id.Button02);
                        button2.setVisibility(Button.INVISIBLE);
                    }
                    stubNum = stub.getId();
                    Log.d("Jason", "stubNum is " + stubNum);
                }
                catch(NullPointerException e)
                {
                    e.printStackTrace();
                }
                break;
            case R.id.button4:
                try
                {
                    stubNum = 4;
                    ViewStub stub = (ViewStub) findViewById(R.id.stub4);            
                    if (!(stub.getVisibility() == ViewStub.VISIBLE)) {
                        View newView = stub.inflate();
                        Button button1 = (Button) findViewById(R.id.Button01);
                        button1.setVisibility(Button.INVISIBLE);

                        Button button2 = (Button) findViewById(R.id.Button02);
                        button2.setVisibility(Button.INVISIBLE);
                    }
                    stubNum = stub.getId();
                    Log.d("Jason", "stubNum is " + stubNum);


                }
                catch(NullPointerException e)
                {
                    e.printStackTrace();
                }
                break;
            case R.id.button5:
                try
                {
                    stubNum = 5;
                    ViewStub stub = (ViewStub) findViewById(R.id.stub5);
                    if (!(stub.getVisibility() == ViewStub.VISIBLE)) {
                        View newView = stub.inflate();
                        Button button1 = (Button) findViewById(R.id.Button01);
                        button1.setVisibility(Button.INVISIBLE);

                        Button button2 = (Button) findViewById(R.id.Button02);
                        button2.setVisibility(Button.INVISIBLE);
                    }
                    stubNum = stub.getId();
                    Log.d("Jason", "stubNum is " + stubNum);
                }
                catch(NullPointerException e)
                {
                    e.printStackTrace();
                }
                break;
            default:
                stubNum = 1;
                ViewStub stub = (ViewStub) findViewById(R.id.stub1);
                if (!(stub.getVisibility() == ViewStub.VISIBLE)) {
                    View newView = stub.inflate();
                }
                break;
        }
    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout android:layout_height="wrap_content"
        android:id="@+id/linearLayout1" android:layout_width="match_parent"
        android:layout_alignParentLeft="true"
        android:gravity="center" android:layout_alignParentBottom="true">
        <ImageButton android:src="@drawable/download" android:id="@+id/button1" android:layout_width="60dp" android:layout_height="40dp" android:layout_margin="2dip" android:onClick="doStuff"></ImageButton>
        <ImageButton android:src="@drawable/download" android:id="@+id/button2" android:layout_width="60dp" android:layout_height="40dp" android:layout_margin="2dip" android:onClick="doStuff"></ImageButton>
        <ImageButton android:src="@drawable/download" android:id="@+id/button3" android:layout_width="60dp" android:layout_height="40dp" android:layout_margin="2dip" android:onClick="doStuff"></ImageButton>
        <ImageButton android:src="@drawable/download" android:id="@+id/button4" android:layout_width="60dp" android:layout_height="40dp" android:layout_margin="2dip" android:onClick="doStuff"></ImageButton>
        <ImageButton android:src="@drawable/download" android:id="@+id/button5" android:layout_width="60dp" android:layout_height="40dp" android:layout_margin="2dip" android:onClick="doStuff"></ImageButton>
    </LinearLayout>
    <RelativeLayout android:id="@+id/relativeLayout2"
        android:layout_height="wrap_content" android:layout_width="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true" android:layout_above="@+id/linearLayout1"
        android:background="#ffffff" android:layout_below="@+id/relativeLayout3">
        <ViewStub android:id="@+id/stub1"
               android:inflatedId="@+id/subTree"
               android:layout="@layout/myvarsity"
               android:layout_width="fill_parent"
               android:layout_height="fill_parent" />

        <ViewStub android:id="@+id/stub2"
               android:inflatedId="@+id/subTree"
               android:layout="@layout/livestream"
               android:layout_width="fill_parent"
               android:layout_height="fill_parent" />

        <ViewStub android:id="@+id/stub3"
               android:inflatedId="@+id/subTree"
               android:layout="@layout/shows"
               android:layout_width="fill_parent"
               android:layout_height="fill_parent" />

        <ViewStub android:id="@+id/stub4"
               android:inflatedId="@+id/subTree"
               android:layout="@layout/more"
               android:layout_width="fill_parent"
               android:layout_height="fill_parent" />

        <ViewStub android:id="@+id/stub5"
               android:inflatedId="@+id/subTree"
               android:layout="@layout/settings"
               android:layout_width="fill_parent"
               android:layout_height="fill_parent" />
    </RelativeLayout>
    <RelativeLayout android:id="@+id/relativeLayout3" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_width="match_parent" android:gravity="center_horizontal">
            <Button android:id="@+id/Button02" android:text="Customize" android:layout_height="40dp" android:layout_width="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentRight="true"></Button>
            <Button android:id="@+id/Button01" android:text="Back" android:layout_height="40dp" android:layout_width="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentLeft="true"></Button>
            <TextView android:id="@+id/textView1" android:text="App" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:layout_width="wrap_content" android:layout_centerVertical="true" android:layout_centerHorizontal="true"></TextView>
        </RelativeLayout>
</RelativeLayout>

more.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:gravity="center_horizontal">
    <RelativeLayout android:id="@+id/relativeLayout2"
        android:layout_height="wrap_content" android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" android:layout_width="match_parent">
        <ListView android:id="@+id/moreList"
            android:layout_centerVertical="true" android:layout_width="match_parent"
            android:layout_alignParentLeft="true" android:layout_height="match_parent"></ListView>
    </RelativeLayout>
    <TextView android:id="@+id/textView1" android:textColor="#000000"
        android:text="Morelistview" android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="wrap_content" android:layout_below="@+id/relativeLayout2"
        android:layout_alignParentLeft="true"></TextView>
</RelativeLayout>

more_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    <TextView android:text="" android:id="@+id/moreItem" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>

</RelativeLayout>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心作怪 2024-12-18 14:36:00
android:cacheColorHint = "#00000000"

将此标签用于您的列表视图。或者您也可以使用

listview.setCacheColorHint()

以编程方式设置它。

android:cacheColorHint = "#00000000"

use this tag for the your listView. or you can also use

listview.setCacheColorHint()

to set it programatically.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文